0.11 - AdminMenuPage settings + options
This commit is contained in:
parent
cfb9233dac
commit
ab65b0e8db
@ -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))
|
|
||||||
|
|
||||||
@ -1,17 +1,29 @@
|
|||||||
(function($,document){
|
(function($,document){
|
||||||
var button = $( '#meta-link-notices' ),
|
let button = $( '#meta-link-notices' ),
|
||||||
panel = $( '#meta-link-notices-wrap' ),
|
panel = $( '#meta-link-notices-wrap' ),
|
||||||
haveClosed = false,
|
haveClosed = false;
|
||||||
haveDismissed = 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(){
|
$(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 meta-links-notices if no notices on page
|
||||||
* Remove screen-meta-links wrapper if no panels 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 ){
|
if ( ! notices.length ){
|
||||||
console.log('NO NOTICES');
|
console.log('NO NOTICES');
|
||||||
$('#meta-link-notices-link-wrap').detach();
|
$('#meta-link-notices-link-wrap').detach();
|
||||||
@ -23,23 +35,55 @@
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* auto-open notices panel
|
||||||
|
*/
|
||||||
if (button.length){
|
if (button.length){
|
||||||
panel.toggle();
|
panel.toggle();
|
||||||
button.addClass( 'screen-meta-active' );
|
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' );
|
dismissNoticesButton.on('click',function(){
|
||||||
dismiss.on('click',function(){
|
|
||||||
screenMeta.close(panel,button);
|
screenMeta.close(panel,button);
|
||||||
$( 'div.updated, div.error, div.notice, div.update-nag' ).not( '.inline, .below-h2' ).appendTo('.notice_container').eq(0);
|
collectNotices();
|
||||||
$('.notice_container').removeClass('empty');
|
|
||||||
dismiss.hide();
|
|
||||||
haveDismissed = true;
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
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
|
//original wp focus on click function
|
||||||
/* button.on( 'focus.scroll-into-view', function(e){
|
/* button.on( 'focus.scroll-into-view', function(e){
|
||||||
if ( e.target.scrollIntoView )
|
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) )
|
}(jQuery,document) )
|
||||||
@ -2,83 +2,67 @@
|
|||||||
/**
|
/**
|
||||||
* Plugin Name: abuyoyo / Notice Manager
|
* Plugin Name: abuyoyo / Notice Manager
|
||||||
* Description: Manage notices on WordPress admin pages. Adds 'Notices' screen-meta-link.
|
* Description: Manage notices on WordPress admin pages. Adds 'Notices' screen-meta-link.
|
||||||
* Version: 0.10
|
* Version: 0.11
|
||||||
* Author: abuyoyo
|
* Author: abuyoyo
|
||||||
* Author URI: https://github.com/abuyoyo/
|
* Author URI: https://github.com/abuyoyo/
|
||||||
* Plugin URI: https://github.com/abuyoyo/notice-manager
|
* Plugin URI: https://github.com/abuyoyo/notice-manager
|
||||||
*/
|
*/
|
||||||
if ( ! defined('ABSPATH') )
|
if ( ! defined('ABSPATH') ) wp_die( 'No soup for you!' );
|
||||||
wp_die( 'No soup for you!' );
|
|
||||||
|
|
||||||
/**
|
use WPHelper\PluginCore;
|
||||||
* @todo If no notices on page - don't show Notice Manager panel
|
use WPHelper\AdminMenuPage;
|
||||||
*/
|
|
||||||
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' ] );
|
|
||||||
|
|
||||||
// we don't want to update wp-plugin registered with same name
|
new PluginCore(
|
||||||
add_filter( 'site_transient_update_plugins', [ $this, 'remove_update_notifications' ] );
|
__FILE__,
|
||||||
}
|
[
|
||||||
|
'update_checker' => ['auth'=> $github_oauth],
|
||||||
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;
|
|
||||||
|
|
||||||
add_screen_meta_link(
|
new AdminMenuPage([
|
||||||
'meta-link-notices', // $id
|
'slug' => 'notice-manager',
|
||||||
'Notices', // $text
|
'title' => 'Notice Manager',
|
||||||
'', // $href - not used
|
'parent' => 'options',
|
||||||
'*', // $page - string or array of page/screen IDs
|
'render' => 'settings-page', // built-in settings page
|
||||||
[ 'aria-controls' => 'meta-link-notices-wrap' ], // $attributes - Additional attributes for the link tag.
|
'settings' => [
|
||||||
[ $this , 'print_notice_manager_panel' ] // $panel callback - cb echoes its output
|
'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' => [
|
||||||
wp_enqueue_script( 'notice_manager_panel', plugin_dir_url( __FILE__ ) . 'js/notice_manager_panel.js' , null, false , true );
|
[
|
||||||
}
|
'id' => 'notice_manager',
|
||||||
|
// 'title' => 'N',
|
||||||
/* not JSON-safe - unescaped quotes */
|
'description' => 'Setup How notice manager functions.',
|
||||||
function print_notice_manager_panel(){
|
'fields' => [
|
||||||
|
[
|
||||||
// NOTE:
|
'id' => 'screen_panel',
|
||||||
// button is a copy of is-dismissible button - for styling purposes only
|
'title' => 'Notices Panel',
|
||||||
// js functionality and listener - js/notice_manager_meta_panel.js
|
'type' => 'checkbox',
|
||||||
echo '<div class="notice_container empty"></div>';
|
'description' => 'Enable screen-meta-links \'Notices\' panel.',
|
||||||
echo '<button type="button" class="notice-dismiss"><span class="screen-reader-text">' . __( 'Dismiss' ) . '</span><strong> Dismiss Notices</strong></button><div></div>' ;
|
],
|
||||||
}
|
[
|
||||||
|
'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.',
|
||||||
|
],
|
||||||
|
]
|
||||||
|
]
|
||||||
|
]
|
||||||
|
]
|
||||||
|
]);
|
||||||
|
|
||||||
/**
|
require_once 'src/NoticeManager.php';
|
||||||
* 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;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
global $notice_manager;
|
add_action('plugins_loaded', function(){
|
||||||
$notice_manager = new NoticeManager();
|
new NoticeManager();
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
add_filter( 'wds_required_plugins', function($required){
|
add_filter( 'wds_required_plugins', function($required){
|
||||||
|
|||||||
87
src/NoticeManager.php
Normal file
87
src/NoticeManager.php
Normal 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user