helgatheviking / Nav-Menu-Roles

Display / Hide wp_nav_menu() items by role
66 stars 32 forks source link

Move main class to its own class file #53

Closed szepeviktor closed 2 years ago

szepeviktor commented 4 years ago

plus some PHPCS auto-correction.

szepeviktor commented 4 years ago

Here is a diff of after&before.

--- 1/nav-menu-roles.php        2020-08-29 18:22:46.000000000 +0000
+++ 2/class-nav-menu-roles.php  2020-08-29 18:12:11.000000000 +0000
@@ -1,41 +1,14 @@
 <?php
-/*
-Plugin Name: Nav Menu Roles
-Plugin URI: http://www.kathyisawesome.com/449/nav-menu-roles/
-Description: Hide custom menu items based on user roles.
-Version: 1.10.2
-Author: Kathy Darling
-Author URI: http://www.kathyisawesome.com
-License: GPL-3.0
-Text Domain: nav-menu-roles
-
-Copyright 2020 Kathy Darling
-
-This program is free software; you can redistribute it and/or modify
-it under the terms of the GNU General Public License, version 2, as
-published by the Free Software Foundation.
-
-This program is distributed in the hope that it will be useful,
-but WITHOUT ANY WARRANTY; without even the implied warranty of
-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
-GNU General Public License for more details.
-
-You should have received a copy of the GNU General Public License
-along with this program; if not, write to the Free Software
-Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA02110-1301USA
+/**
+ * Nav Menu Roles
+ *
+ * @author      Kathy Darling
+ * @since       2.0
 */

-
-// Don't load directly.
-if ( ! function_exists( 'is_admin' ) ) {
-       header( 'Status: 403 Forbidden' );
-       header( 'HTTP/1.1 403 Forbidden' );
-       exit();
-}
-
-// Quit if this exists already.
-if ( class_exists( 'Nav_Menu_Roles' ) ) {
-       return;
+// Exit if accessed directly
+if ( ! defined( 'ABSPATH' ) ) {
+       exit;
 }

 class Nav_Menu_Roles {
@@ -47,6 +20,12 @@
        protected static $_instance = null;

        /**
+        * @var string Path to main plugin file.
+        * @since 2.0
+        */
+       protected $main_file;
+
+       /**
        * @constant string donate url
        * @since 1.9.1
        */
@@ -66,11 +45,12 @@
         * @since 1.5
         * @static
         * @see Nav_Menu_Roles()
-        * @return Nav_Menu_Roles - Main instance
+        * @param string Path to main plugin file
+        * @return Nav_Menu_Roles Main instance
         */
-       public static function instance() {
+       public static function instance( $file ) {
                if ( is_null( self::$_instance ) ) {
-                       self::$_instance = new self();
+                       self::$_instance = new self( $file );
                }
                return self::$_instance;
        }
@@ -96,12 +76,15 @@
        /**
         * Nav_Menu_Roles Constructor.
         * @access public
+        * @param string Path to main plugin file
         * @return Nav_Menu_Roles
         * @since  1.0
         */
-       public function __construct() {
+       public function __construct( $file ) {

-               require_once( 'inc/customizer.php' );
+               $this->main_file = $file;
+
+               require_once plugin_dir_path( __FILE__ ) . 'customizer.php';

                // Admin functions.
                add_action( 'admin_init', array( $this, 'admin_init' ) );
@@ -168,7 +151,7 @@
                // Register the new importer.
                if ( defined( 'WP_LOAD_IMPORTERS' ) ) {

-                       include_once( plugin_dir_path( __FILE__ ) . 'inc/class-nav-menu-roles-import.php' );
+                       include_once plugin_dir_path( __FILE__ ) . 'class-nav-menu-roles-import.php';
                        // Register the custom importer we've created.
                        $roles_import = new Nav_Menu_Roles_Import();

@@ -184,7 +167,7 @@
         * @since 1.0
         */
        public function load_text_domain() {
-               load_plugin_textdomain( 'nav-menu-roles', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
+               load_plugin_textdomain( 'nav-menu-roles', false, dirname( plugin_basename( $this->main_file ) ) . '/languages/' );
        }

        /**
@@ -281,7 +264,7 @@
         * @param string $plugin_file
         */
        public function add_action_links( $plugin_meta, $plugin_file ) {
-               if ( plugin_basename( __FILE__ ) === $plugin_file ) {
+               if ( plugin_basename( $this->main_file ) === $plugin_file ) {
                        $plugin_meta[] = sprintf( '<a class="dashicons-before dashicons-welcome-learn-more" href="https://wordpress.org/plugins/nav-menu-roles/faq/#conflict">%s</a>', __( 'FAQ', 'nav-menu-roles' ) );
                        $plugin_meta[] = '<a class="dashicons-before dashicons-admin-generic" href="' . self::DONATE_URL . '" target="_blank">' . __( 'Donate', 'nav-menu-roles' ) . '</a>';
                }
@@ -298,11 +281,11 @@
                if ( ! class_exists( 'Walker_Nav_Menu_Edit_Roles' ) ) {

                        if ( self::is_wp_gte( '4.7' ) ) {
-                               require_once( plugin_dir_path( __FILE__ ) . 'inc/class-walker-nav-menu-edit-roles-4.7.php' );
+                               require_once plugin_dir_path( __FILE__ ) . 'class-walker-nav-menu-edit-roles-4.7.php';
                        } else if ( self::is_wp_gte( '4.5' ) ) {
-                               require_once( plugin_dir_path( __FILE__ ) . 'inc/class-walker-nav-menu-edit-roles-4.5.php' );
+                               require_once plugin_dir_path( __FILE__ ) . 'class-walker-nav-menu-edit-roles-4.5.php';
                        } else {
-                               require_once( plugin_dir_path( __FILE__ ) . 'inc/class-walker-nav-menu-edit-roles.php' );
+                               require_once plugin_dir_path( __FILE__ ) . 'class-walker-nav-menu-edit-roles.php';
                        }
                }
                return 'Walker_Nav_Menu_Edit_Roles';
@@ -424,7 +407,7 @@
        public function enqueue_scripts( $hook ) {
                if ( 'nav-menus.php' === $hook ) {
                        $suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
-                       wp_enqueue_script( 'nav-menu-roles', plugins_url( 'js/nav-menu-roles' . $suffix . '.js', __FILE__ ), array( 'jquery' ), self::VERSION, true );
+                       wp_enqueue_script( 'nav-menu-roles', plugins_url( 'js/nav-menu-roles' . $suffix . '.js', $this->main_file ), array( 'jquery' ), self::VERSION, true );
                }
        }

@@ -591,7 +574,6 @@
                return $items;
        }

-
        /**
         * Maybe upgrade
         *
@@ -619,18 +601,4 @@
                return version_compare( strtolower( $wp_version ), strtolower( $version ), '>=' );
        }

-} // end class
-
-/**
- * Launch the whole plugin
- * Returns the main instance of Nav Menu Roles to prevent the need to use globals.
- *
- * @since  1.5
- * @return Nav_Menu_Roles
- */
-function Nav_Menu_Roles() {
-       return Nav_Menu_Roles::instance();
 }
-
-// Global for backwards compatibility.
-$GLOBALS['Nav_Menu_Roles'] = Nav_Menu_Roles();
szepeviktor commented 2 years ago

Thank you.