0.11 - AdminMenuPage settings + options

This commit is contained in:
abuyoyo 2020-04-08 11:18:51 +03:00
parent cfb9233dac
commit ab65b0e8db
4 changed files with 194 additions and 111 deletions

View File

@ -1,21 +0,0 @@
/**
* Move all notices "above the fold"
*
* Used for plugins that print their notices after 'admin_notice'
*/
(function ($,document){
$(document).ready(function(){
//alert ('move_notices.js');
//$( 'div.updated, div.error, div.notice' ).not( '.inline, .below-h2' ).insertAfter( $headerEnd );
//alert('plugin move notices');
$( 'div.updated, div.error, div.notice, div.update-nag' ).not( '.inline, .below-h2' ).insertBefore('.wrap:first'); //alert('plugin move notices');
// $( 'div.updated, div.error, div.notice, div.update-nag' ).not( '.inline, .below-h2' ).each(function(){
// $(this).insertBefore('.wrap'); //alert('plugin move notices');
// });
});
}(jQuery, document))

View File

@ -1,17 +1,29 @@
(function($,document){
var button = $( '#meta-link-notices' ),
let button = $( '#meta-link-notices' ),
panel = $( '#meta-link-notices-wrap' ),
haveClosed = false,
haveDismissed = false;
haveClosed = false;
let notices = $( 'div.updated, div.error, div.notice, div.update-nag' ).not( '.inline, .below-h2' );
// $( '#meta-link-notices-wrap button' );
let dismissNoticesButton = $( '#meta-link-notices-wrap button.notice-dismiss' );
//auto-open notice-panel for quick dismiss
$(document).ready(function(){
console.log(noticeManager);
/**
* Move ALL notices above page title. ALWAYS!
*/
notices.insertBefore('.wrap:first');
/**
* Remove meta-links-notices if no notices on page
* Remove screen-meta-links wrapper if no panels on page
*/
var notices = $( 'div.updated, div.error, div.notice, div.update-nag' ).not( '.inline, .below-h2' );
if ( ! notices.length ){
console.log('NO NOTICES');
$('#meta-link-notices-link-wrap').detach();
@ -23,23 +35,55 @@
return;
}
/**
* auto-open notices panel
*/
if (button.length){
panel.toggle();
button.addClass( 'screen-meta-active' );
screenMeta.open(panel,button);
screenMeta.open(panel,button);
}
if (noticeManager.autoCollect){
collectNotices();
}
if (noticeManager.autoCollapse){
wait = window.wait || function(ms){
var dfd = $.Deferred();
setTimeout(dfd.resolve, ms); //callback, timeout till callback
return dfd.promise();
};
// auto-close notices panel after short delay
// only auto-close if we have not interacted (opened/closed) with panel previously
wait(20000).then(function(){
if ( ! haveClosed )
screenMeta.close(panel,button);
});
}
});
var dismiss = $( '#meta-link-notices-wrap button' );
dismiss.on('click',function(){
dismissNoticesButton.on('click',function(){
screenMeta.close(panel,button);
$( 'div.updated, div.error, div.notice, div.update-nag' ).not( '.inline, .below-h2' ).appendTo('.notice_container').eq(0);
$('.notice_container').removeClass('empty');
dismiss.hide();
haveDismissed = true;
collectNotices();
});
function collectNotices(){
$( 'div.updated, div.error, div.notice, div.update-nag' ).not( '.inline, .below-h2' )
.appendTo('.notice_container').eq(0);
$('.notice_container').removeClass('empty');
dismissNoticesButton.detach();
}
//original wp focus on click function
/* button.on( 'focus.scroll-into-view', function(e){
if ( e.target.scrollIntoView )
@ -60,17 +104,6 @@
});
wait = window.wait || function(ms){
var dfd = $.Deferred();
setTimeout(dfd.resolve, ms); //callback, timeout till callback
return dfd.promise();
};
// auto-close notices panel after short delay
// only auto-close if we have not interacted (opened/closed) with panel previously
wait(20000).then(function(){
if ( ! haveClosed )
screenMeta.close(panel,button);
});
}(jQuery,document) )

View File

@ -2,83 +2,67 @@
/**
* Plugin Name: abuyoyo / Notice Manager
* Description: Manage notices on WordPress admin pages. Adds 'Notices' screen-meta-link.
* Version: 0.10
* Version: 0.11
* Author: abuyoyo
* Author URI: https://github.com/abuyoyo/
* Plugin URI: https://github.com/abuyoyo/notice-manager
*/
if ( ! defined('ABSPATH') )
wp_die( 'No soup for you!' );
if ( ! defined('ABSPATH') ) wp_die( 'No soup for you!' );
/**
* @todo If no notices on page - don't show Notice Manager panel
*/
class NoticeManager{
function __construct(){
// exit early if not admin page
if ( !is_admin() )
return;
add_action( 'admin_enqueue_scripts' , [ $this , 'admin_enqueues' ] );
add_action( 'admin_init' , [ $this , 'register_notice_manager_panel' ] );
use WPHelper\PluginCore;
use WPHelper\AdminMenuPage;
// we don't want to update wp-plugin registered with same name
add_filter( 'site_transient_update_plugins', [ $this, 'remove_update_notifications' ] );
}
function admin_enqueues(){
wp_enqueue_script( 'move_notices', plugin_dir_url( __FILE__ ) . 'js/move_notices.js' , null, false , true ); //default script - moves all notices above wrap h1/h2
wp_enqueue_style( 'admin_notices', plugin_dir_url( __FILE__ ) . 'css/admin_notices.css' );
}
function register_notice_manager_panel(){
if ( ! function_exists( 'add_screen_meta_link' ) )
return;
new PluginCore(
__FILE__,
[
'update_checker' => ['auth'=> $github_oauth],
]
);
add_screen_meta_link(
'meta-link-notices', // $id
'Notices', // $text
'', // $href - not used
'*', // $page - string or array of page/screen IDs
[ 'aria-controls' => 'meta-link-notices-wrap' ], // $attributes - Additional attributes for the link tag.
[ $this , 'print_notice_manager_panel' ] // $panel callback - cb echoes its output
);
wp_enqueue_script( 'notice_manager_panel', plugin_dir_url( __FILE__ ) . 'js/notice_manager_panel.js' , null, false , true );
}
/* not JSON-safe - unescaped quotes */
function print_notice_manager_panel(){
// NOTE:
// button is a copy of is-dismissible button - for styling purposes only
// js functionality and listener - js/notice_manager_meta_panel.js
echo '<div class="notice_container empty"></div>';
echo '<button type="button" class="notice-dismiss"><span class="screen-reader-text">' . __( 'Dismiss' ) . '</span><strong> Dismiss Notices</strong></button><div></div>' ;
}
new AdminMenuPage([
'slug' => 'notice-manager',
'title' => 'Notice Manager',
'parent' => 'options',
'render' => 'settings-page', // built-in settings page
'settings' => [
'option_name' => 'notice_manager', // option_name used in wp_options table
// 'option_group' => 'wp_head_cleaner_settings' . '_settings_group', // Optional - Settings group used in register_setting() and settings_fields()
'sections' => [
[
'id' => 'notice_manager',
// 'title' => 'N',
'description' => 'Setup How notice manager functions.',
'fields' => [
[
'id' => 'screen_panel',
'title' => 'Notices Panel',
'type' => 'checkbox',
'description' => 'Enable screen-meta-links \'Notices\' panel.',
],
[
'id' => 'auto_collect',
'title' => 'Auto-Collect Notices',
'type' => 'checkbox',
'description' => 'Automatic collection of notices into panel.',
],
[
'id' => 'auto_collapse',
'title' => 'Auto-collapse Panel',
'type' => 'checkbox',
'description' => 'Notices panel will stay open for a few seconds on page load, and then close automatically.',
],
]
]
]
]
]);
/**
* A plugin 'notice-manager' exists now on @link https://wordpress.org/plugins/
* Disable update notifications completely for our plugin.
*
* @todo - use update_checker to only upload our plugin from github
*/
function remove_update_notifications($value) {
if ( isset( $value ) && is_object( $value ) ) {
unset( $value->response[ plugin_basename(__FILE__) ] );
}
return $value;
}
}
require_once 'src/NoticeManager.php';
global $notice_manager;
$notice_manager = new NoticeManager();
add_action('plugins_loaded', function(){
new NoticeManager();
});
add_filter( 'wds_required_plugins', function($required){

87
src/NoticeManager.php Normal file
View File

@ -0,0 +1,87 @@
<?php
/**
* NoticeManager class
*/
if ( ! defined('ABSPATH') ) wp_die( 'No soup for you!' );
/**
* @todo If no notices on page - don't show Notice Manager panel
*/
class NoticeManager{
/**
* Options
*
* @var Array $options
*/
private $options;
function __construct(){
// exit early if not admin page
if ( !is_admin() )
return;
$this->options = get_option( 'notice_manager');
add_action( 'admin_enqueue_scripts' , [ $this , 'admin_enqueues' ] );
if ( empty( $this->options['screen_panel'] ) ){
array_walk($this->options,function(&$item){$item=0;});
}else{
add_action( 'admin_init' , [ $this , 'register_notice_manager_panel' ] );
}
// we don't want to update wp-plugin registered with same name
// add_filter( 'site_transient_update_plugins', [ $this, 'remove_update_notifications' ] );
}
function admin_enqueues(){
wp_enqueue_script( 'notice_manager_panel', NOTICE_MANAGER_URL . 'js/notice_manager_panel.js' , null, false , true );
wp_localize_script( 'notice_manager_panel', 'noticeManager', camelCaseKeys($this->options) );
wp_enqueue_style( 'admin_notices', NOTICE_MANAGER_URL . 'css/admin_notices.css' );
}
function register_notice_manager_panel(){
if ( ! function_exists( 'add_screen_meta_link' ) )
return;
add_screen_meta_link(
'meta-link-notices', // $id
'Notices', // $text
'', // $href - not used
'*', // $page - string or array of page/screen IDs
[ 'aria-controls' => 'meta-link-notices-wrap' ], // $attributes - Additional attributes for the link tag.
[ $this , 'print_notice_manager_panel' ] // $panel callback - cb echoes its output
);
}
/* not JSON-safe - unescaped quotes */
function print_notice_manager_panel(){
// NOTE:
// button is a copy of is-dismissible button - for styling purposes only
// js functionality and listener - js/notice_manager_meta_panel.js
echo '<div class="notice_container empty"></div>';
echo '<button type="button" class="notice-dismiss"><span class="screen-reader-text">' . __( 'Dismiss' ) . '</span><strong> Dismiss Notices</strong></button><div></div>' ;
}
/**
* A plugin 'notice-manager' exists now on @link https://wordpress.org/plugins/
* Disable update notifications completely for our plugin.
*
* @todo - use update_checker to only upload our plugin from github
*/
function remove_update_notifications($value) {
if ( isset( $value ) && is_object( $value ) ) {
unset( $value->response[ NOTICE_MANAGER_BASENAME ] );
}
return $value;
}
}