From 8e34e860dae056e74bc71683df599322293c0fbc Mon Sep 17 00:00:00 2001 From: Mohammed Saduzzaman Sadi <64192420+zamans78@users.noreply.github.com> Date: Wed, 21 Apr 2021 17:20:04 +0600 Subject: [PATCH 1/5] Changed jQuery.fn.change() event shorthand which was deprecated. --- js/wpsite_limit_posts_admin.js | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/js/wpsite_limit_posts_admin.js b/js/wpsite_limit_posts_admin.js index d39fa71..601cdb6 100644 --- a/js/wpsite_limit_posts_admin.js +++ b/js/wpsite_limit_posts_admin.js @@ -1,21 +1,21 @@ -jQuery(document).ready(function($) { - - $("input:radio[name=wpsite_limit_posts_settings_all_users]").change(function(){ - if ($(this).val() == 'capability') { - $(".wpsite_limit_posts_users").hide(); - $(".wpsite_limit_posts_roles").show(); - } else { - $(".wpsite_limit_posts_users").show(); - $(".wpsite_limit_posts_roles").hide(); +jQuery(document).ready(function ($) { + + $("input:radio[name=wpsite_limit_posts_settings_all_users]").on("change", function () { + if ($(this).val() == "capability") { + $(".wpsite_limit_posts_users").hide(); + $(".wpsite_limit_posts_roles").show(); + } else { + $(".wpsite_limit_posts_users").show(); + $(".wpsite_limit_posts_roles").hide(); + } } - }); + ); - if ($("input:radio[name=wpsite_limit_posts_settings_all_users]:checked").val() == 'capability') { + if ($("input:radio[name=wpsite_limit_posts_settings_all_users]:checked").val() == "capability") { $(".wpsite_limit_posts_users").hide(); $(".wpsite_limit_posts_roles").show(); } else { $(".wpsite_limit_posts_users").show(); $(".wpsite_limit_posts_roles").hide(); } - -}); \ No newline at end of file +}); From 7ebfc9b99361c4eeaef279d878de2a3096f98cd2 Mon Sep 17 00:00:00 2001 From: Mohammed Saduzzaman Sadi <64192420+zamans78@users.noreply.github.com> Date: Wed, 21 Apr 2021 17:22:53 +0600 Subject: [PATCH 2/5] Changed limited role, made public false. Changed Version Name. --- wpsite-limit-posts.php | 824 +++++++++++++++++++++-------------------- 1 file changed, 422 insertions(+), 402 deletions(-) diff --git a/wpsite-limit-posts.php b/wpsite-limit-posts.php index 5d90087..b48bbc3 100644 --- a/wpsite-limit-posts.php +++ b/wpsite-limit-posts.php @@ -1,428 +1,447 @@ 'capability', - 'all_limit' => array(), - 'user_limit' => array(), - ); - - /** - * Cloning is forbidden. - */ - public function __clone() { - wc_doing_it_wrong( __FUNCTION__, __( 'Cheatin’ huh?', 'wpsite-limit-posts' ), $this->version ); - } - - /** - * Unserializing instances of this class is forbidden. - */ - public function __wakeup() { - wc_doing_it_wrong( __FUNCTION__, __( 'Cheatin’ huh?', 'wpsite-limit-posts' ), $this->version ); - } - - /** - * Main WPsite_Limit_Posts instance. - * - * Ensure only one instance is loaded or can be loaded. - * - * @return WPsite_Limit_Posts - */ - public static function instance() { - - if ( is_null( self::$_instance ) && ! ( self::$_instance instanceof WPsite_Limit_Posts ) ) { - self::$_instance = new WPsite_Limit_Posts(); - self::$_instance->hooks(); - } - - return self::$_instance; - } - - /** - * WPsite_Limit_Posts constructor. - */ - private function __construct() { - - } - - /** - * Add hooks to begin. - * @return void - */ - private function hooks() { - - add_action( 'plugins_loaded', array( $this, 'load_plugin_textdomain' ) ); - add_action( 'init', array( $this, 'register_post_status' ) ); - add_action( 'wp_insert_post_data', array( $this, 'stop_publish_post' ), '99', 2 ); - - if ( is_admin() ) { - - $plugin = plugin_basename( __FILE__ ); - add_filter( "plugin_action_links_$plugin", array( $this, 'plugin_links' ) ); - - add_action( 'admin_menu', array( $this, 'register_pages' ) ); - add_action( 'admin_notices', array( $this, 'posts_notice' ) ); - - foreach ( array( 'post', 'post-new' ) as $hook ) { - add_action( "admin_footer-{$hook}.php", array( $this, 'extend_submitdiv_post_status' ) ); - } - } - } - - /** - * Load the plugin text domain for translation. - * @return void - */ - public function load_plugin_textdomain() { - - $locale = apply_filters( 'plugin_locale', get_locale(), 'wpsite-limit-posts' ); - - load_textdomain( - 'wpsite-limit-posts', - WP_LANG_DIR . '/wpsite-limit-posts/wpsite-limit-posts-' . $locale . '.mo' - ); - - load_plugin_textdomain( - 'wpsite-limit-posts', - false, - $this->plugin_dir() . '/languages/' - ); - } - - /** - * Hooks to 'plugin_action_links_' filter - * - * @since 1.0.0 - */ - public function plugin_links( $links ) { - - $settings_link = 'Settings'; - array_unshift( $links, $settings_link ); - - return $links; - } - - /** - * Hooks to 'init' and resgisters new post status type - * - * @since 1.0.0 - */ - public function register_post_status() { - - register_post_status( 'limited', array( - 'label' => esc_html__( 'Limited', 'wpsite-limit-posts' ), - 'public' => true, - 'exclude_from_search' => false, - 'show_in_admin_all_list' => true, - 'show_in_admin_status_list' => true, - 'label_count' => _n_noop( 'Limited (%s)', 'Limited (%s)', 'wpsite-limit-posts' ), - )); - } - - /** - * Hooks to 'admin_menu' - * - * @since 1.0.0 - */ - public function register_pages() { - - $settings_page_load = add_submenu_page( - 'options-general.php', - esc_html__( 'Limit Posts', 'wpsite-limit-posts' ), - esc_html__( 'Limit Posts', 'wpsite-limit-posts' ), - 'manage_options', - self::$settings_page, - array( $this, 'page_settings' ) - ); - add_action( "load-$settings_page_load", array( $this, 'admin_scripts' ) ); - } - - /** - * Displays the HTML for the 'wpsite-limit-posts-admin-menu-settings' admin page - * - * @since 1.0.0 - */ - public function page_settings() { - - global $wp_roles; - $settings = $this->get_settings(); - - // Save data nd check nonce - if ( isset( $_POST['submit'] ) && check_admin_referer( 'wpsite_limit_posts_admin_settings' ) ) { - - $limited_roles = array(); - $settings['all'] = isset( $_POST['wpsite_limit_posts_settings_all_users'] ) ? $_POST['wpsite_limit_posts_settings_all_users'] : 'capability'; - - foreach ( $wp_roles->roles as $role ) { - - $role_name = strtolower( $role['name'] ); - - if ( isset( $role['capabilities'] ) && isset( $role['capabilities']['publish_posts'] ) && ! isset( $role['capabilities']['update_core'] ) && !isset($role['capabilities']['install_themes']) && !isset($role['capabilities']['install_plugins']) ) { - - if ( '' === stripcslashes( sanitize_text_field( $_POST[ 'wpsite_limit_posts_settings_post_num_' . $role_name ] ) ) ) { - $settings['all_limit'][ $role_name ] = -1; - $limited_roles[] = $role['name']; - } else { - $settings['all_limit'][ $role_name ] = isset( $_POST[ 'wpsite_limit_posts_settings_post_num_' . $role_name ] ) ? (int) stripcslashes( sanitize_text_field( $_POST[ 'wpsite_limit_posts_settings_post_num_' . $role_name ] ) ) : '-1'; - $limited_roles[] = $role['name']; - } - } - } - - $users = array(); - $all_users = get_users(); - - foreach ( $all_users as $user ) { - if ( user_can( $user->ID, 'publish_posts' ) && ! user_can( $user->ID, 'update_core' ) && ! user_can( $user->ID, 'install_themes' ) && ! user_can( $user->ID, 'install_plugins' ) ) { - $users[] = $user; - } - } - - foreach ( $users as $user ) { - - if ( '' === stripcslashes( sanitize_text_field( $_POST[ 'wpsite_limit_posts_settings_user_' . $user->ID ] ) ) ) { - $settings['user_limit'][ $user->ID ] = -1; - } else { - $settings['user_limit'][ $user->ID ] = isset( $_POST[ 'wpsite_limit_posts_settings_user_' . $user->ID ] ) ? (int) stripcslashes( sanitize_text_field( $_POST[ 'wpsite_limit_posts_settings_user_' . $user->ID ] ) ) : '-1'; - } - } - - update_option( 'wpsite_limit_posts_settings', $settings ); - } - - require_once( 'admin/dashboard.php' ); - } - - /** - * Hooks to 'admin_print_scripts-$page' - * - * @since 1.0.0 - */ - public function admin_scripts() { - - // Styles - wp_enqueue_style( 'wpsite_limit_posts_settings_css', wpsite_lps()->plugin_url() . 'css/settings.css' ); - wp_enqueue_style( 'wpsite_limit_posts_bootstrap_css', wpsite_lps()->plugin_url() . 'css/nnr-bootstrap.min.css' ); - - // Scripts - wp_enqueue_script( 'wpsite_limit_posts_admin_js', wpsite_lps()->plugin_url() . 'js/wpsite_limit_posts_admin.js' ); - } - - /** - * Hooks to 'admin_notices' - * - * @since 1.0.0 - */ - public function posts_notice() { - - global $pagenow; - - if ( 'post.php' === $pagenow && isset( $_GET['post'] ) ) { - - $post = get_post( $_GET['post'] ); - - if ( isset( $post ) && 'limited' === $post->post_status ) { - - $author_data = get_userdata( $post->post_author ); - - if ( isset( $author_data ) && get_current_user_id() !== $post->post_author ) { - echo '
' . sprintf( esc_html__( 'Author: %s is at his or her post limit.', 'wpsite-limit-posts' ), $author_data->user_login ) . '
+class WPsite_Limit_Posts +{ + + /** + * WPsite_Limit_Posts version. + * @var string + */ + public $version = '2.1.1'; + + /** + * The single instance of the class. + * @var WPsite_Limit_Posts + */ + protected static $_instance = null; + + /** + * Plugin url. + * @var string + */ + private $plugin_url = null; + + /** + * Plugin path. + * @var string + */ + private $plugin_dir = null; + + /** + * Setting page id. + * @var string + */ + private static $settings_page = 'wpsite-limit-posts-admin-menu-settings'; + + /** + * Default settings. + * @var array + */ + private static $default = array( + 'all' => 'capability', + 'all_limit' => array(), + 'user_limit' => array(), + ); + + /** + * Cloning is forbidden. + */ + public function __clone() + { + wc_doing_it_wrong(__FUNCTION__, __('Cheatin’ huh?', 'wpsite-limit-posts'), $this->version); + } + + /** + * Unserializing instances of this class is forbidden. + */ + public function __wakeup() + { + wc_doing_it_wrong(__FUNCTION__, __('Cheatin’ huh?', 'wpsite-limit-posts'), $this->version); + } + + /** + * Main WPsite_Limit_Posts instance. + * + * Ensure only one instance is loaded or can be loaded. + * + * @return WPsite_Limit_Posts + */ + public static function instance() + { + + if (is_null(self::$_instance) && !(self::$_instance instanceof WPsite_Limit_Posts)) { + self::$_instance = new WPsite_Limit_Posts(); + self::$_instance->hooks(); + } + + return self::$_instance; + } + + /** + * WPsite_Limit_Posts constructor. + */ + private function __construct() + { + + } + + /** + * Add hooks to begin. + * @return void + */ + private function hooks() + { + + add_action('plugins_loaded', array($this, 'load_plugin_textdomain')); + add_action('init', array($this, 'register_post_status')); + add_action('wp_insert_post_data', array($this, 'stop_publish_post'), '99', 2); + + if (is_admin()) { + + $plugin = plugin_basename(__FILE__); + add_filter("plugin_action_links_$plugin", array($this, 'plugin_links')); + + add_action('admin_menu', array($this, 'register_pages')); + add_action('admin_notices', array($this, 'posts_notice')); + + foreach (array('post', 'post-new') as $hook) { + add_action("admin_footer-{$hook}.php", array($this, 'extend_submitdiv_post_status')); + } + } + } + + /** + * Load the plugin text domain for translation. + * @return void + */ + public function load_plugin_textdomain() + { + + $locale = apply_filters('plugin_locale', get_locale(), 'wpsite-limit-posts'); + + load_textdomain( + 'wpsite-limit-posts', + WP_LANG_DIR . '/wpsite-limit-posts/wpsite-limit-posts-' . $locale . '.mo' + ); + + load_plugin_textdomain( + 'wpsite-limit-posts', + false, + $this->plugin_dir() . '/languages/' + ); + } + + /** + * Hooks to 'plugin_action_links_' filter + * + * @since 1.0.0 + */ + public function plugin_links($links) + { + + $settings_link = 'Settings'; + array_unshift($links, $settings_link); + + return $links; + } + + /** + * Hooks to 'init' and resgisters new post status type + * + * @since 1.0.0 + */ + public function register_post_status() + { + + register_post_status('limited', array( + 'label' => esc_html__('Limited', 'wpsite-limit-posts'), + 'public' => false, + 'exclude_from_search' => false, + 'show_in_admin_all_list' => true, + 'show_in_admin_status_list' => true, + 'label_count' => _n_noop('Limited (%s)', 'Limited (%s)', 'wpsite-limit-posts'), + )); + } + + /** + * Hooks to 'admin_menu' + * + * @since 1.0.0 + */ + public function register_pages() + { + + $settings_page_load = add_submenu_page( + 'options-general.php', + esc_html__('Limit Posts', 'wpsite-limit-posts'), + esc_html__('Limit Posts', 'wpsite-limit-posts'), + 'manage_options', + self::$settings_page, + array($this, 'page_settings') + ); + add_action("load-$settings_page_load", array($this, 'admin_scripts')); + } + + /** + * Displays the HTML for the 'wpsite-limit-posts-admin-menu-settings' admin page + * + * @since 1.0.0 + */ + public function page_settings() + { + + global $wp_roles; + $settings = $this->get_settings(); + + // Save data nd check nonce + if (isset($_POST['submit']) && check_admin_referer('wpsite_limit_posts_admin_settings')) { + + $limited_roles = array(); + $settings['all'] = isset($_POST['wpsite_limit_posts_settings_all_users']) ? $_POST['wpsite_limit_posts_settings_all_users'] : 'capability'; + + foreach ($wp_roles->roles as $role) { + + $role_name = strtolower($role['name']); + + if (isset($role['capabilities']) && isset($role['capabilities']['publish_posts']) && !isset($role['capabilities']['update_core']) && !isset($role['capabilities']['install_themes']) && !isset($role['capabilities']['install_plugins'])) { + + if ('' === stripcslashes(sanitize_text_field($_POST['wpsite_limit_posts_settings_post_num_' . $role_name]))) { + $settings['all_limit'][$role_name] = -1; + $limited_roles[] = $role['name']; + } else { + $settings['all_limit'][$role_name] = isset($_POST['wpsite_limit_posts_settings_post_num_' . $role_name]) ? (int) stripcslashes(sanitize_text_field($_POST['wpsite_limit_posts_settings_post_num_' . $role_name])) : '-1'; + $limited_roles[] = $role['name']; + } + } + } + + $users = array(); + $all_users = get_users(); + + foreach ($all_users as $user) { + if (user_can($user->ID, 'publish_posts') && !user_can($user->ID, 'update_core') && !user_can($user->ID, 'install_themes') && !user_can($user->ID, 'install_plugins')) { + $users[] = $user; + } + } + + foreach ($users as $user) { + + if ('' === stripcslashes(sanitize_text_field($_POST['wpsite_limit_posts_settings_user_' . $user->ID]))) { + $settings['user_limit'][$user->ID] = -1; + } else { + $settings['user_limit'][$user->ID] = isset($_POST['wpsite_limit_posts_settings_user_' . $user->ID]) ? (int) stripcslashes(sanitize_text_field($_POST['wpsite_limit_posts_settings_user_' . $user->ID])) : '-1'; + } + } + + update_option('wpsite_limit_posts_settings', $settings); + } + + require_once 'admin/dashboard.php'; + } + + /** + * Hooks to 'admin_print_scripts-$page' + * + * @since 1.0.0 + */ + public function admin_scripts() + { + + // Styles + wp_enqueue_style('wpsite_limit_posts_settings_css', wpsite_lps()->plugin_url() . 'css/settings.css'); + wp_enqueue_style('wpsite_limit_posts_bootstrap_css', wpsite_lps()->plugin_url() . 'css/nnr-bootstrap.min.css'); + + // Scripts + wp_enqueue_script('wpsite_limit_posts_admin_js', wpsite_lps()->plugin_url() . 'js/wpsite_limit_posts_admin.js'); + } + + /** + * Hooks to 'admin_notices' + * + * @since 1.0.0 + */ + public function posts_notice() + { + + global $pagenow; + + if ('post.php' === $pagenow && isset($_GET['post'])) { + + $post = get_post($_GET['post']); + + if (isset($post) && 'limited' === $post->post_status) { + + $author_data = get_userdata($post->post_author); + + if (isset($author_data) && get_current_user_id() !== $post->post_author) { + echo '' . sprintf(esc_html__('Author: %s is at his or her post limit.', 'wpsite-limit-posts'), $author_data->user_login) . '
' . esc_html__( 'You are at or you have exceeded your post limit.', 'wpsite-limit-posts' ) . '
+ } else { + echo '' . esc_html__('You are at or you have exceeded your post limit.', 'wpsite-limit-posts') . '