From ab65b0e8dbf5b64e1b7b86447fcd58a6eccf392c Mon Sep 17 00:00:00 2001 From: abuyoyo Date: Wed, 8 Apr 2020 11:18:51 +0300 Subject: [PATCH] 0.11 - AdminMenuPage settings + options --- js/move_notices.js | 21 ------- js/notice_manager_panel.js | 79 +++++++++++++++++-------- notice-manager.php | 118 ++++++++++++++++--------------------- src/NoticeManager.php | 87 +++++++++++++++++++++++++++ 4 files changed, 194 insertions(+), 111 deletions(-) delete mode 100644 js/move_notices.js create mode 100644 src/NoticeManager.php diff --git a/js/move_notices.js b/js/move_notices.js deleted file mode 100644 index a18a35b..0000000 --- a/js/move_notices.js +++ /dev/null @@ -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)) - diff --git a/js/notice_manager_panel.js b/js/notice_manager_panel.js index 4d1aa6d..3f72499 100644 --- a/js/notice_manager_panel.js +++ b/js/notice_manager_panel.js @@ -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) ) \ No newline at end of file diff --git a/notice-manager.php b/notice-manager.php index cb1bd90..c8157ad 100644 --- a/notice-manager.php +++ b/notice-manager.php @@ -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 '
'; - echo '
' ; - } +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){ diff --git a/src/NoticeManager.php b/src/NoticeManager.php new file mode 100644 index 0000000..b5ae74a --- /dev/null +++ b/src/NoticeManager.php @@ -0,0 +1,87 @@ +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 '
'; + echo '
' ; + } + + /** + * 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; + } + +} \ No newline at end of file