├── languages └── plugin-name.pot ├── index.php ├── assets ├── index.php ├── admin │ ├── index.php │ ├── css │ │ ├── plugin-name-admin.css │ │ └── plugin-name-admin-ver-1.css │ └── js │ │ ├── plugin-name-admin.js │ │ └── plugin-name-admin-ver-1.js ├── css │ ├── plugin-name-public.css │ ├── plugin-name-public-ver-1.css │ └── custom.css ├── js │ ├── custom.js │ ├── plugin-name-public.js │ └── plugin-name-public-ver-1.js └── images │ └── wp-logo.png ├── templates ├── index.php ├── front │ ├── footer │ │ ├── footer.php │ │ └── first-page-footer.php │ ├── header │ │ ├── menu.php │ │ ├── head.php │ │ └── first-page-head.php │ ├── login-error.php │ ├── second-page-sample.php │ └── first-page-sample.php └── admin │ ├── plugin-page │ ├── primary-section.php │ ├── welcome-panel.php │ ├── dashboard-widgets.php │ └── second-section.php │ ├── options-page │ ├── simple-option-page1.php │ ├── sample-option-page3.php │ └── option-page1.php │ └── index.php ├── logs ├── execution-log.txt ├── deactivator-logs.txt └── activator-logs.txt ├── includes ├── pagehandlers │ ├── contracts │ │ └── class-page-handler.php │ ├── class-second-page-handler.php │ └── class-first-page-handler.php ├── interfaces │ ├── class-filter-hook-interface.php │ ├── class-action-hook-interface.php │ ├── class-action-hook-with-args-interface.php │ └── custom-admin-columns │ │ └── class-manage-post-columns.php ├── uninstall │ ├── class-uninstall.php │ └── class-deactivator.php ├── functions │ ├── class-check-woocommerce.php │ ├── class-current-user.php │ ├── class-activation-issue.php │ ├── class-logger.php │ ├── class-log-in-footer.php │ ├── class-check-type.php │ ├── class-template-builder.php │ ├── class-init-functions.php │ └── class-utility.php ├── parts │ ├── custom-taxonomies │ │ └── class-custom-taxonomy1.php │ ├── shortcodes │ │ ├── class-shortcode1.php │ │ ├── class-content-for-login-user-shortcode.php │ │ └── class-complete-shortcode.php │ ├── other │ │ └── class-remove-post-column.php │ └── custom-posts │ │ └── class-custom-post1.php ├── init │ ├── class-i18n.php │ ├── class-admin-hook.php │ ├── class-public-hook.php │ ├── class-router.php │ ├── class-loader.php │ ├── class-activator.php │ └── class-constant.php ├── admin │ ├── notices │ │ ├── class-woocommerce-deactive-notice.php │ │ └── class-admin-notice1.php │ ├── class-admin-menu1.php │ ├── class-option-menu1.php │ ├── class-option-menu2.php │ ├── class-admin-sub-menu2.php │ ├── class-setting-page1.php │ ├── class-simple-setting-in-reading-page1.php │ ├── class-meta-box3.php │ ├── class-admin-sub-menu1.php │ ├── class-simple-setting-page1.php │ └── class-meta-box4.php ├── abstracts │ ├── class-admin-notice.php │ ├── class-custom-taxonomy.php │ ├── class-ajax.php │ ├── class-shortcode.php │ ├── class-custom-post-type.php │ ├── class-admin-sub-menu.php │ ├── class-option-menu.php │ ├── class-admin-menu.php │ ├── class-simple-setting-page.php │ └── class-meta-box.php ├── hooks │ └── filters │ │ └── class-custom-cron-schedule.php ├── class-autoloader.php ├── database │ └── class-table.php └── config │ └── class-info.php ├── README.md └── plugin-name.php /languages/plugin-name.pot: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /index.php: -------------------------------------------------------------------------------- 1 | "> 4 | 5 | -------------------------------------------------------------------------------- /templates/front/footer/first-page-footer.php: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /templates/front/header/menu.php: -------------------------------------------------------------------------------- 1 | 2 |
3 | 4 | 5 |

6 | Add your menu here! 7 |

8 |
9 | -------------------------------------------------------------------------------- /assets/js/plugin-name-public.js: -------------------------------------------------------------------------------- 1 | (function ($) { 2 | $(document).ready(function () { 3 | var $window = $(window); 4 | $window.on("load", function () { 5 | 6 | }); 7 | 8 | }); 9 | 10 | })(jQuery); -------------------------------------------------------------------------------- /assets/admin/js/plugin-name-admin.js: -------------------------------------------------------------------------------- 1 | //admin JS 2 | (function ($) { 3 | $(document).ready(function () { 4 | var $window = $(window); 5 | $window.on("load", function () { 6 | 7 | }); 8 | 9 | }); 10 | 11 | })(jQuery); -------------------------------------------------------------------------------- /templates/front/login-error.php: -------------------------------------------------------------------------------- 1 |

2 | Login Error: You can not access to this page! 3 |

" -------------------------------------------------------------------------------- /logs/execution-log.txt: -------------------------------------------------------------------------------- 1 | Log message of : Test sample 1 [on 2020-04-09 18:46:16] 2 | Sample to test log class during plugin execution 3 | 4 | Log message of : Test sample 2 [on 2020-04-09 18:46:16] 5 | Sample to test log class during plugin execution 6 | 7 | -------------------------------------------------------------------------------- /assets/admin/js/plugin-name-admin-ver-1.js: -------------------------------------------------------------------------------- 1 | //admin JS - Version 1 2 | (function ($) { 3 | $(document).ready(function () { 4 | var $window = $(window); 5 | $window.on("load", function () { 6 | 7 | }); 8 | 9 | }); 10 | 11 | })(jQuery); -------------------------------------------------------------------------------- /assets/js/plugin-name-public-ver-1.js: -------------------------------------------------------------------------------- 1 | /*Public JS - Version 1*/ 2 | (function ($) { 3 | $(document).ready(function () { 4 | var $window = $(window); 5 | $window.on("load", function () { 6 | 7 | }); 8 | 9 | }); 10 | 11 | })(jQuery); -------------------------------------------------------------------------------- /templates/admin/plugin-page/primary-section.php: -------------------------------------------------------------------------------- 1 | 4 |
5 | 9 |
-------------------------------------------------------------------------------- /templates/admin/options-page/simple-option-page1.php: -------------------------------------------------------------------------------- 1 |
2 |

MSN plugin options page

3 |
4 | 9 |
10 |
-------------------------------------------------------------------------------- /assets/css/custom.css: -------------------------------------------------------------------------------- 1 | /*This is custom CSS file*/ 2 | .pluginprefix-menu-style { 3 | width: 80%; 4 | text-align: center !important; 5 | padding: 20px; 6 | border: 2px solid dodgerblue; 7 | margin: 20px auto; 8 | border-radius: 5px; 9 | font-size: 20px; 10 | } 11 | 12 | .pluginprefix-red { 13 | color: red; 14 | } 15 | 16 | .pluginprefix-blue { 17 | color: dodgerblue; 18 | } 19 | 20 | .pluginprefix-text-center { 21 | text-align: center; 22 | } 23 | -------------------------------------------------------------------------------- /logs/deactivator-logs.txt: -------------------------------------------------------------------------------- 1 | Log message of : De-Activator User [on 2020-04-03 16:27:08] 2 | The user with login of: "mehdi" and display name of: "Mehdi Soltani" de-activated this plugin 3 | 4 | Log message of : De-Activator User [on 2020-04-03 16:28:33] 5 | The user with login of: "mehdi" and display name of: "Mehdi Soltani" de-activated this plugin 6 | 7 | Log message of : De-Activator User [on 2020-05-01 13:26:11] 8 | The user with login of: "mehdi" and display name of: "Mehdi Soltani" de-activated this plugin 9 | 10 | -------------------------------------------------------------------------------- /templates/admin/plugin-page/welcome-panel.php: -------------------------------------------------------------------------------- 1 |
2 |
3 |

Welcome to Msn Plugin

4 |
5 |
6 |
7 | The section 1 8 |
9 |
10 | The section 2 11 |
12 |
13 | The section 3 14 |
15 | 16 |
17 |
-------------------------------------------------------------------------------- /templates/admin/index.php: -------------------------------------------------------------------------------- 1 | 10 | * @license https://www.gnu.org/licenses/gpl-3.0.txt GNU/GPLv3 11 | * @link https://yoursite.com 12 | * @since 1.0.2 13 | */ 14 | 15 | if ( ! defined( 'ABSPATH' ) ) { 16 | exit; 17 | } 18 | 19 | 20 | -------------------------------------------------------------------------------- /templates/front/header/head.php: -------------------------------------------------------------------------------- 1 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | Title for second page sample 12 | 13 | 14 | 18 | 19 | -------------------------------------------------------------------------------- /templates/front/header/first-page-head.php: -------------------------------------------------------------------------------- 1 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | Title for first page sample 12 | 13 | 14 | 18 | 19 | -------------------------------------------------------------------------------- /templates/front/second-page-sample.php: -------------------------------------------------------------------------------- 1 | 11 | 12 | 13 |
14 |
15 |

This is sample for second page

16 |
17 |

Now we want to get current user information in the following:

18 |
Hello, my name is
19 |
20 |
21 |
22 | 23 | -------------------------------------------------------------------------------- /includes/pagehandlers/contracts/class-page-handler.php: -------------------------------------------------------------------------------- 1 | 10 | * @license https://www.gnu.org/licenses/gpl-3.0.txt GNU/GPLv3 11 | * @link https://github.com/msn60/oop-wordpress-plugin-boilerplate 12 | * @since 1.0.2 13 | */ 14 | 15 | namespace Plugin_Name_Name_Space\Includes\PageHandlers\Contracts; 16 | 17 | if ( ! defined( 'ABSPATH' ) ) { 18 | exit; 19 | } 20 | 21 | /** 22 | * Interface Page_Handler 23 | * 24 | * @package Plugin_Name_Name_Space 25 | * @author Mehdi Soltani 26 | */ 27 | interface Page_Handler { 28 | 29 | /** 30 | * Render method to render a page with router 31 | * 32 | * This method must be implement by children who implement this interface. 33 | * 34 | * @since 1.0.2 35 | * @access public 36 | */ 37 | public function render(); 38 | } 39 | -------------------------------------------------------------------------------- /includes/interfaces/class-filter-hook-interface.php: -------------------------------------------------------------------------------- 1 | 10 | * @license https://www.gnu.org/licenses/gpl-3.0.txt GNU/GPLv3 11 | * @link https://wpwebmaster.ir 12 | * @since 1.0.2 13 | */ 14 | 15 | namespace Plugin_Name_Name_Space\Includes\Interfaces; 16 | 17 | if ( ! defined( 'ABSPATH' ) ) { 18 | exit; 19 | } 20 | 21 | /** 22 | * Filter_Hook_Interface interface File 23 | * 24 | * This file contains Filter_Hook_Interface. If you to use add_action in your class, 25 | * you must use from this contract to implement it. 26 | * 27 | * @package Plugin_Name_Name_Space 28 | * @author Mehdi Soltani 29 | */ 30 | interface Filter_Hook_Interface { 31 | 32 | /** 33 | * Register filters that the object needs to be subscribed to. 34 | * 35 | */ 36 | public function register_add_filter(); 37 | } -------------------------------------------------------------------------------- /includes/interfaces/class-action-hook-interface.php: -------------------------------------------------------------------------------- 1 | 10 | * @license https://www.gnu.org/licenses/gpl-3.0.txt GNU/GPLv3 11 | * @link https://wpwebmaster.ir 12 | * @since 1.0.2 13 | */ 14 | 15 | namespace Plugin_Name_Name_Space\Includes\Interfaces; 16 | 17 | if ( ! defined( 'ABSPATH' ) ) { 18 | exit; 19 | } 20 | 21 | /** 22 | * Action_Hook_Interface interface File 23 | * 24 | * This file contains Action_Hook_Interface_Interface. If you to use add_action in your class, 25 | * you must use from this contract to implement it. 26 | * 27 | * @package Plugin_Name_Name_Space 28 | * @author Mehdi Soltani 29 | */ 30 | interface Action_Hook_Interface { 31 | 32 | /** 33 | * Register actions that the object needs to be subscribed to. 34 | * 35 | */ 36 | public function register_add_action(); 37 | } -------------------------------------------------------------------------------- /includes/uninstall/class-uninstall.php: -------------------------------------------------------------------------------- 1 | 9 | * @license https://www.gnu.org/licenses/gpl-3.0.txt GNU/GPLv3 10 | * @link https://github.com/msn60/oop-wordpress-plugin-boilerplate 11 | * @since 1.0.2 12 | */ 13 | 14 | namespace Plugin_Name_Name_Space\Includes\Uninstall; 15 | 16 | if ( ! defined( 'ABSPATH' ) ) { 17 | exit; 18 | } 19 | 20 | /** 21 | * Class Uninstall 22 | * 23 | * @package Plugin_Name_Name_Space 24 | * @author Mehdi Soltani 25 | */ 26 | class Uninstall { 27 | /** 28 | * Destroy Config 29 | * Drop Database 30 | * Delete options 31 | * Removing Settings 32 | */ 33 | public static function uninstall() { 34 | 35 | // TODO: delete_option for option values that need them again 36 | // TODO: delete_option for post types ('has_rewrite_for_plugin_name_new_post_types') 37 | // TODO: unregister_setting + delete_option for Clean de-registration of registered setting in settings page 38 | 39 | } 40 | } 41 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /templates/front/first-page-sample.php: -------------------------------------------------------------------------------- 1 | 9 | * @license https://www.gnu.org/licenses/gpl-3.0.txt GNU/GPLv3 10 | * @link https://yoursite.com 11 | * @since 1.0.2 12 | */ 13 | 14 | use Plugin_Name_Name_Space\Includes\Functions\Utility; 15 | 16 | /*get header*/ 17 | Utility::load_template( 'header.first-page-head', array(), 'front' ); 18 | Utility::load_template( 'header.menu', array(), 'front' ); 19 | 20 | $current_user = wp_get_current_user(); 21 | ?> 22 | 23 | 24 | 25 |
26 |
27 |

This is sample for first page

28 |
29 |

Now we want to get current user information in the following:

30 | 31 |
32 |
33 |
34 | 35 | 41 | 42 | -------------------------------------------------------------------------------- /includes/interfaces/class-action-hook-with-args-interface.php: -------------------------------------------------------------------------------- 1 | 10 | * @license https://www.gnu.org/licenses/gpl-3.0.txt GNU/GPLv3 11 | * @link https://wpwebmaster.ir 12 | * @since 1.0.2 13 | */ 14 | 15 | namespace Plugin_Name_Name_Space\Includes\Interfaces; 16 | 17 | if ( ! defined( 'ABSPATH' ) ) { 18 | exit; 19 | } 20 | 21 | /** 22 | * Action_Hook_With_Args_Interface interface File 23 | * 24 | * This file contains Action_Hook_With_Args_Interface_Interface. If you to use add_action in your class with arguments, 25 | * you must use from this contract to implement it. 26 | * 27 | * @package Plugin_Name_Name_Space 28 | * @author Mehdi Soltani 29 | */ 30 | interface Action_Hook_With_Args_Interface { 31 | 32 | /** 33 | * Register actions that the object needs to be subscribed to. 34 | * 35 | */ 36 | public function register_add_action_with_arguments( $args ); 37 | } -------------------------------------------------------------------------------- /includes/functions/class-check-woocommerce.php: -------------------------------------------------------------------------------- 1 | 9 | * @license https://www.gnu.org/licenses/gpl-3.0.txt GNU/GPLv3 10 | * @link https://wpwebmaster.ir 11 | * @since 1.0.2 12 | */ 13 | 14 | namespace Plugin_Name_Name_Space\Includes\Functions; 15 | 16 | if ( ! defined( 'ABSPATH' ) ) { 17 | exit; 18 | } 19 | 20 | /** 21 | * Check_Woocommerce trait File 22 | * 23 | * This class contains methods that check is woocommerce activated or not 24 | * 25 | * @package Plugin_Name_Name_Space 26 | * @author Mehdi Soltani 27 | * @license https://www.gnu.org/licenses/gpl-3.0.txt GNU/GPLv3 28 | * @link https://github.com/msn60/oop-wordpress-plugin-boilerplate 29 | * @since 1.0.2 30 | */ 31 | trait Check_Woocommerce { 32 | 33 | /** 34 | * Method to check is Woocommerce activated or not 35 | * 36 | * 37 | * @access public 38 | * 39 | * @return bool 40 | */ 41 | public function is_woocommerce_active( ) { 42 | if ( in_array('woocommerce/woocommerce.php', apply_filters('plugin_name_active_plugins',get_option('active_plugins')))) { 43 | return true; 44 | } else { 45 | return false; 46 | } 47 | } 48 | 49 | } 50 | -------------------------------------------------------------------------------- /includes/functions/class-current-user.php: -------------------------------------------------------------------------------- 1 | 9 | * @license https://www.gnu.org/licenses/gpl-3.0.txt GNU/GPLv3 10 | * @link https://github.com/msn60/oop-wordpress-plugin-boilerplate 11 | * @since 1.0.2 12 | */ 13 | 14 | namespace Plugin_Name_Name_Space\Includes\Functions; 15 | 16 | if ( ! defined( 'ABSPATH' ) ) { 17 | exit; 18 | } 19 | 20 | /** 21 | * Current_User Class File 22 | * 23 | * This class contains functions to return current user 24 | * 25 | * @package Plugin_Name_Name_Space 26 | * @author Mehdi Soltani 27 | */ 28 | trait Current_User { 29 | 30 | /** 31 | * Add pluggable.php before it runs to get current user 32 | * 33 | * After using this function, you can access to all property of WP_User class like: 34 | * ID 35 | * user_login 36 | * user_pass 37 | * user_nicename 38 | * user_email 39 | * user_url 40 | * user_registered 41 | * user_activation_key 42 | * user_status 43 | * display_name 44 | * 45 | * @access public 46 | * 47 | * @return \WP_User 48 | */ 49 | public function get_this_login_user() { 50 | include_once( ABSPATH . 'wp-includes/pluggable.php' ); 51 | return wp_get_current_user(); 52 | } 53 | 54 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # OOP WordPress Plugin Boilerplate 2 | 3 | * Contributors: [Mehdi Soltani](https://wpwebmaster.ir/author/mehdi-soltani/) 4 | 5 | ## Description 6 | 7 | This repository contains a plugin boilerplate that its structure is based on OOP. 8 | So if you feel better with writing code in OOP way, it's perfect choice to use it. 9 | Everything in this boilerplate is being created by OOP concepts and then combined with WordPress concepts. 10 | 11 | 12 | ## 🚦 Explanation about plugin structure 13 | 14 | ### Plugin directories and files: 15 | 16 | > The primary plugin file is being located in root directory. 17 | 18 | In the root directory, you can see these subdirectories: 19 | 20 | #### 1. assets 21 | All assets files locates in this directory. For admin assets, I consider a separated directory. 22 | It contains all of CSS, JS and images files which the plugin needs to use them. 23 | 24 | #### 2. includes 25 | All classes related to this plugin is located in this directory. 26 | 27 | #### 3. languages 28 | Files that the plugin needs for internationalization is located in language directory. 29 | 30 | #### 4. logs 31 | For many reasons, we need to log some processes like errors or doing repeating tasks. 32 | For this reason, I decided to have a separeted directory. 33 | 34 | #### 5. templates 35 | For separating view part, I use template directory. So if I need to render any HTML files in front end or back end, I ues this directory. 36 | 37 | 38 | 39 | 40 | 41 | -------------------------------------------------------------------------------- /logs/activator-logs.txt: -------------------------------------------------------------------------------- 1 | 2 | 3 | Log message of : Activator User [on 2020-04-03 12:24:17] 4 | The user with login of: "mehdi" and display name of: "Mehdi Soltani" activated this plugin 5 | 6 | Log message of : Activator Last Log [on 2020-04-03 12:24:17] 7 | Sample to test logger class when plugin is activated 8 | 9 | Log message of : Activator User [on 2020-04-03 12:25:36] 10 | The user with login of: "mehdi" and display name of: "Mehdi Soltani" activated this plugin 11 | 12 | Log message of : Activator Last Log [on 2020-04-03 12:25:36] 13 | Sample to test logger class when plugin is activated 14 | 15 | Log message of : Activator User [on 2020-04-03 16:27:57] 16 | The user with login of: "mehdi" and display name of: "Mehdi Soltani" activated this plugin 17 | 18 | Log message of : Activator Last Log [on 2020-04-03 16:27:57] 19 | Sample to test logger class when plugin is activated 20 | 21 | Log message of : Activator User [on 2020-04-03 16:29:04] 22 | The user with login of: "mehdi" and display name of: "Mehdi Soltani" activated this plugin 23 | 24 | Log message of : Activator Last Log [on 2020-04-03 16:29:04] 25 | Sample to test logger class when plugin is activated 26 | 27 | Log message of : Activator User [on 2020-05-01 13:26:21] 28 | The user with login of: "mehdi" and display name of: "Mehdi Soltani" activated this plugin 29 | 30 | Log message of : Activator Last Log [on 2020-05-01 13:26:21] 31 | Sample to test logger class when plugin is activated 32 | 33 | -------------------------------------------------------------------------------- /includes/parts/custom-taxonomies/class-custom-taxonomy1.php: -------------------------------------------------------------------------------- 1 | 10 | * @license https://www.gnu.org/licenses/gpl-3.0.txt GNU/GPLv3 11 | * @link https://github.com/msn60/oop-wordpress-plugin-boilerplate 12 | * @since 1.0.2 13 | */ 14 | 15 | namespace Plugin_Name_Name_Space\Includes\Parts\Custom_Taxonomies; 16 | 17 | use Plugin_Name_Name_Space\Includes\Abstracts\Custom_Taxonomy; 18 | 19 | if ( ! defined( 'ABSPATH' ) ) { 20 | exit; 21 | } 22 | 23 | /** 24 | * Class Custom_Taxonomy1. 25 | * This file contains contract for Custom_Taxonomy1 class. 26 | * 27 | * @package Plugin_Name_Name_Space 28 | * @author Mehdi Soltani 29 | * 30 | */ 31 | class Custom_Taxonomy1 extends Custom_Taxonomy { 32 | 33 | /** 34 | * The singular name of Taxonomy1 35 | * 36 | * @var string $singular_name 37 | */ 38 | private $singular_name; 39 | 40 | /** 41 | * Custom_Taxonomy1 constructor. 42 | * 43 | * @param array $initial_values 44 | */ 45 | public function __construct( array $initial_values ) { 46 | parent::__construct( $initial_values ); 47 | $this->singular_name = $initial_values['args']['labels']['singular_name']; 48 | } 49 | 50 | 51 | } 52 | -------------------------------------------------------------------------------- /includes/functions/class-activation-issue.php: -------------------------------------------------------------------------------- 1 | 9 | * @license https://www.gnu.org/licenses/gpl-3.0.txt GNU/GPLv3 10 | * @link https://github.com/msn60/oop-wordpress-plugin-boilerplate 11 | * @since 1.0.2 12 | */ 13 | 14 | namespace Plugin_Name_Name_Space\Includes\Functions; 15 | 16 | if ( ! defined( 'ABSPATH' ) ) { 17 | exit; 18 | } 19 | 20 | /** 21 | * Activation_Issue trait File 22 | * 23 | * This class contains functions to log activation issues when plugin is activated 24 | * 25 | * @package Plugin_Name_Name_Space 26 | * @author Mehdi Soltani 27 | * @since 1.0.2 28 | */ 29 | trait Activation_Issue { 30 | 31 | /** 32 | * Register 'activated_plugin' add_action to call related method to log error 33 | */ 34 | public function register_error_activation_add_action() { 35 | add_action( 'activated_plugin', [ $this, 'save_plugin_activation_error' ] ); 36 | } 37 | 38 | /** 39 | * Save activation errors or warnings or notices in option table 40 | */ 41 | public function save_plugin_activation_error() { 42 | update_option( 'msn_plugin_activation_error', ob_get_contents() ); 43 | } 44 | 45 | /** 46 | * Show plugin activation errors or warnings or notices by echoing it 47 | */ 48 | public function show_plugin_activation_error() { 49 | echo get_option( 'msn_plugin_activation_error' ); 50 | } 51 | 52 | } -------------------------------------------------------------------------------- /includes/functions/class-logger.php: -------------------------------------------------------------------------------- 1 | 9 | * @license https://www.gnu.org/licenses/gpl-3.0.txt GNU/GPLv3 10 | * @link https://github.com/msn60/oop-wordpress-plugin-boilerplate 11 | * @since 1.0.2 12 | */ 13 | 14 | namespace Plugin_Name_Name_Space\Includes\Functions; 15 | 16 | if ( ! defined( 'ABSPATH' ) ) { 17 | exit; 18 | } 19 | 20 | /** 21 | * Logger Class File 22 | * 23 | * This class contains functions to log needed parts 24 | * 25 | * @package Plugin_Name_Name_Space 26 | * @author Mehdi Soltani 27 | */ 28 | trait Logger { 29 | 30 | /** 31 | * Write log file in 32 | * 33 | * @access public 34 | * 35 | * @param string $log_message Message which needs to log in text file 36 | * @param string $file_name File name of log file 37 | */ 38 | public function append_log_in_text_file( $log_message, $file_name, $type = 'not type' ) { 39 | date_default_timezone_set('Asia/Tehran'); 40 | $type = 'not type' !== $type ? 'Log message of : ' . $type: 'This log is generated'; 41 | $data = $type . ' [on ' . date( 'Y-m-d H:i:s' ) . ']'. PHP_EOL; 42 | $data .= $log_message . PHP_EOL . PHP_EOL; 43 | if ( file_exists( $file_name ) ) { 44 | $file_content = file_get_contents( $file_name ); 45 | file_put_contents( $file_name, $data, FILE_APPEND | LOCK_EX ); 46 | 47 | } else { 48 | file_put_contents( $file_name, $data ); 49 | } 50 | 51 | } 52 | 53 | } 54 | -------------------------------------------------------------------------------- /includes/init/class-i18n.php: -------------------------------------------------------------------------------- 1 | 10 | * @license https://www.gnu.org/licenses/gpl-3.0.txt GNU/GPLv3 11 | * @link https://github.com/msn60/oop-wordpress-plugin-boilerplate 12 | * @since 1.0.2 13 | */ 14 | 15 | namespace Plugin_Name_Name_Space\Includes\Init; 16 | 17 | use Plugin_Name_Name_Space\Includes\Interfaces\Action_Hook_Interface; 18 | 19 | if ( ! defined( 'ABSPATH' ) ) { 20 | exit; 21 | } 22 | 23 | /** 24 | * Define the internationalization functionality. 25 | * 26 | * Loads and defines the internationalization files for this plugin 27 | * so that it is ready for translation. 28 | * 29 | * @since 1.0.2 30 | * @package Plugin_Name_Name_Space 31 | * @author Mehdi Soltani 32 | */ 33 | class I18n implements Action_Hook_Interface { 34 | /** 35 | * Load the plugin text domain for translation. 36 | * 37 | * @since 1.0.2 38 | * @access public 39 | */ 40 | public function load_plugin_textdomain() { 41 | 42 | load_plugin_textdomain( 43 | PLUGIN_NAME_TEXTDOMAIN, 44 | false, 45 | dirname( dirname( plugin_basename( __FILE__ ) ) ) . '/languages/' 46 | ); 47 | } 48 | 49 | /** 50 | * Register actions that the object needs to be subscribed to. 51 | * 52 | */ 53 | public function register_add_action() { 54 | add_action( 'plugins_loaded', array( $this, 'load_plugin_textdomain' ) ); 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /includes/functions/class-log-in-footer.php: -------------------------------------------------------------------------------- 1 | 9 | * @license https://www.gnu.org/licenses/gpl-3.0.txt GNU/GPLv3 10 | * @link https://github.com/msn60/oop-wordpress-plugin-boilerplate 11 | * @since 1.0.2 12 | */ 13 | 14 | namespace Plugin_Name_Name_Space\Includes\Functions; 15 | 16 | use Plugin_Name_Name_Space\Includes\Interfaces\Action_Hook_With_Args_Interface; 17 | 18 | if ( ! defined( 'ABSPATH' ) ) { 19 | exit; 20 | } 21 | 22 | /** 23 | * Log_In_Footer Class File 24 | * 25 | * This class contains functions to log when wp_footer hook initiates. 26 | * 27 | * @package Plugin_Name_Name_Space 28 | * @author Mehdi Soltani 29 | */ 30 | class Log_In_Footer implements Action_Hook_With_Args_Interface { 31 | use Logger; 32 | 33 | /** 34 | * Register actions that the object needs to be subscribed to. 35 | * 36 | * @see https://stackoverflow.com/questions/2843356/can-i-pass-arguments-to-my-function-through-add-action 37 | */ 38 | public function register_add_action_with_arguments( $args ) { 39 | if ( is_admin() ) { 40 | add_action( 'admin_footer', 41 | function () use ( $args ) { 42 | $this->append_log_in_text_file( $args['log_message'], $args['file_name'], $args['type'] ); 43 | } 44 | ); 45 | } else { 46 | add_action( 'wp_footer', 47 | function () use ( $args ) { 48 | $this->append_log_in_text_file( $args['log_message'], $args['file_name'], $args['type'] ); 49 | } 50 | ); 51 | } 52 | 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /includes/pagehandlers/class-second-page-handler.php: -------------------------------------------------------------------------------- 1 | 10 | * @license https://www.gnu.org/licenses/gpl-3.0.txt GNU/GPLv3 11 | * @link https://github.com/msn60/oop-wordpress-plugin-boilerplate 12 | * @since 1.0.2 13 | */ 14 | 15 | namespace Plugin_Name_Name_Space\Includes\PageHandlers; 16 | 17 | if ( ! defined( 'ABSPATH' ) ) { 18 | exit; 19 | } 20 | 21 | use Plugin_Name_Name_Space\Includes\Functions\Template_Builder; 22 | use Plugin_Name_Name_Space\Includes\PageHandlers\Contracts\Page_Handler; 23 | use Plugin_Name_Name_Space\Includes\Functions\Utility; 24 | 25 | /** 26 | * Class Second_Page_Handler. 27 | * This class is used to render a page in your project with a specific route or url. 28 | * 29 | * @package Plugin_Name_Name_Space 30 | * @author Mehdi Soltani 31 | * @see \Plugin_Name_Name_Space\Includes\Functions\Utility 32 | * @see \Plugin_Name_Name_Space\Includes\PageHandlers\Contracts\Page_Handler 33 | */ 34 | class Second_Page_Handler implements Page_Handler { 35 | use Template_Builder; 36 | 37 | /** 38 | * Method render in First_Page_Handler Class 39 | * 40 | * It calls when you need to render a view in your website. 41 | * 42 | * @access public 43 | */ 44 | public function render() { 45 | 46 | $sample_variable = 'Mehdi Soltani'; 47 | $this->load_template( 'second-page-sample', compact( 'sample_variable' ), 'front' ); 48 | exit; 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /includes/pagehandlers/class-first-page-handler.php: -------------------------------------------------------------------------------- 1 | 10 | * @license https://www.gnu.org/licenses/gpl-3.0.txt GNU/GPLv3 11 | * @link https://github.com/msn60/oop-wordpress-plugin-boilerplate 12 | * @since 1.0.2 13 | */ 14 | 15 | namespace Plugin_Name_Name_Space\Includes\PageHandlers; 16 | 17 | if ( ! defined( 'ABSPATH' ) ) { 18 | exit; 19 | } 20 | 21 | use Plugin_Name_Name_Space\Includes\PageHandlers\Contracts\Page_Handler; 22 | use Plugin_Name_Name_Space\Includes\Functions\{ 23 | Utility, Template_Builder 24 | }; 25 | 26 | /** 27 | * Class First_Page_Handler. 28 | * This class is used to render a page in your project with a specific route or url. 29 | * 30 | * @package Plugin_Name_Name_Space 31 | * @author Mehdi Soltani 32 | * @see \Plugin_Name_Name_Space\Includes\Functions\Utility 33 | * @see \Plugin_Name_Name_Space\Includes\PageHandlers\Contracts\Page_Handler 34 | */ 35 | class First_Page_Handler implements Page_Handler { 36 | use Template_Builder; 37 | 38 | /** 39 | * Method render in First_Page_Handler Class 40 | * 41 | * It calls when you need to render a view in your website. 42 | * 43 | * @access public 44 | */ 45 | public function render() { 46 | if ( is_user_logged_in() ) { 47 | $this->load_template( 'first-page-sample', [], 'front' ); 48 | exit; 49 | } else { 50 | $this->load_template( 'login-error', [], 'front' ); 51 | exit(); 52 | } 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /includes/parts/shortcodes/class-shortcode1.php: -------------------------------------------------------------------------------- 1 | 8 | * @license https://www.gnu.org/licenses/gpl-3.0.txt GNU/GPLv3 9 | * @link https://github.com/msn60/oop-wordpress-plugin-boilerplate 10 | * @since 1.0.2 11 | */ 12 | 13 | namespace Plugin_Name_Name_Space\Includes\Parts\Shortcodes; 14 | 15 | use Plugin_Name_Name_Space\Includes\Abstracts\Shortcode; 16 | 17 | if ( ! defined( 'ABSPATH' ) ) { 18 | exit; 19 | } 20 | 21 | /** 22 | * Shortcode1 Class File 23 | * 24 | * Simple self-closing tag shortcode sample class 25 | * 26 | * @package Plugin_Name_Name_Space 27 | * @author Mehdi Soltani 28 | * @link https://github.com/msn60/oop-wordpress-plugin-boilerplate 29 | */ 30 | class Shortcode1 extends Shortcode { 31 | 32 | /** 33 | * define_shortcode method in Shortcode Class 34 | * 35 | * For each each defined shortcode, you must define callable function 36 | * for that. This method has this role as a shortcode callable function 37 | * sample for define this shortcode: 38 | * [msnshortcode1] OR [msnshortcode1 name="Mehdi Soltani"] 39 | * 40 | * @param array $atts attributes which can pass throw shortcode in front end 41 | * @param string $content The content between starting and closing shortcode tag 42 | * @param string $tag The name of the shortcode tag 43 | * 44 | * @return string 45 | */ 46 | public function define_shortcode_handler( $atts = [], $content = null, $tag = '' ) { 47 | 48 | $args = shortcode_atts( [ 49 | "name" => $this->default_atts['name'], 50 | ] 51 | , $atts ); 52 | 53 | return '
Hi ' . $args["name"] . '
'; 54 | } 55 | 56 | 57 | } 58 | -------------------------------------------------------------------------------- /includes/admin/notices/class-woocommerce-deactive-notice.php: -------------------------------------------------------------------------------- 1 | 9 | * @license https://www.gnu.org/licenses/gpl-3.0.txt GNU/GPLv3 10 | * @link https://github.com/msn60/oop-wordpress-plugin-boilerplate 11 | * @since 1.0.2 12 | */ 13 | 14 | namespace Plugin_Name_Name_Space\Includes\Admin\Notices; 15 | 16 | 17 | use Plugin_Name_Name_Space\Includes\Abstracts\Admin_Notice; 18 | 19 | if ( ! defined( 'ABSPATH' ) ) { 20 | exit; 21 | } 22 | 23 | /** 24 | * Woocommerce_Deactive_Notice Class File 25 | * 26 | * This file contains admin notices to show that Woocommerce is deactivated in admin panel 27 | * 28 | * @package Plugin_Name_Name_Space 29 | * @author Mehdi Soltani 30 | * @link https://github.com/msn60/oop-wordpress-plugin-boilerplate 31 | * 32 | * @see https://code.tutsplus.com/series/persisted-wordpress-admin-notices--cms-1252 33 | * @see https://code.tutsplus.com/tutorials/persisted-wordpress-admin-notices-part-1--cms-30134 34 | */ 35 | class Woocommerce_Deactive_Notice extends Admin_Notice { 36 | 37 | 38 | /** 39 | * Method to show admin notice which is Woocommerce is not activated. 40 | * 41 | * @param array $args Arguments which are needed to show on notice 42 | */ 43 | public function show_admin_notice() { 44 | ?> 45 |
46 |

47 | 51 |

52 |
53 | 54 | 10 | * @license https://www.gnu.org/licenses/gpl-3.0.txt GNU/GPLv3 11 | * @link https://github.com/msn60/oop-wordpress-plugin-boilerplate 12 | * @since 1.0.2 13 | */ 14 | 15 | namespace Plugin_Name_Name_Space\Includes\Admin; 16 | 17 | use Plugin_Name_Name_Space\Includes\Abstracts\Admin_Menu; 18 | 19 | if ( ! defined( 'ABSPATH' ) ) { 20 | exit; 21 | } 22 | 23 | /** 24 | * Class Admin_Menu. 25 | * If you want create an admin page inside admin panel of WordPress, 26 | * you can use from this class. 27 | * 28 | * @package Plugin_Name_Name_Space 29 | * @author Mehdi Soltani 30 | * 31 | * @see wp-admin/includes/plugin.php 32 | * @see https://developer.wordpress.org/reference/functions/add_menu_page/ 33 | */ 34 | class Admin_Menu1 extends Admin_Menu{ 35 | 36 | 37 | /** 38 | * Admin_Menu constructor. 39 | * This constructor gets initial values to send to add_menu_page function to 40 | * create admin menu. 41 | * 42 | * @access public 43 | * 44 | * @param array $initial_value Initial value to pass to add_menu_page function. 45 | */ 46 | public function __construct( array $initial_values ) { 47 | parent::__construct($initial_values); 48 | } 49 | 50 | /** 51 | * Method management_panel_handler in Admin_Menu Class 52 | * 53 | * For each admin menu page, we must have callable function that render and 54 | * handle this menu page. For each menu page, you must have its own function. 55 | * 56 | * @access public 57 | */ 58 | public function management_panel_handler() { 59 | 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /templates/admin/plugin-page/dashboard-widgets.php: -------------------------------------------------------------------------------- 1 |
2 |
3 |
4 |
5 |
6 | 9 |

First Widget

10 |
11 |
12 | This is Msn First Widget 13 |
14 |
15 |
16 | 17 |
18 |
19 |
20 |
21 |
22 | 25 |

Second Widget

26 |
27 |
28 | This is Msn Second Widget 29 |
30 |
31 |
32 | 33 |
34 |
35 |
36 |
-------------------------------------------------------------------------------- /includes/parts/shortcodes/class-content-for-login-user-shortcode.php: -------------------------------------------------------------------------------- 1 | 8 | * @license https://www.gnu.org/licenses/gpl-3.0.txt GNU/GPLv3 9 | * @link https://github.com/msn60/oop-wordpress-plugin-boilerplate 10 | * @since 1.0.2 11 | */ 12 | 13 | namespace Plugin_Name_Name_Space\Includes\Parts\Shortcodes; 14 | 15 | use Plugin_Name_Name_Space\Includes\Abstracts\Shortcode; 16 | 17 | if ( ! defined( 'ABSPATH' ) ) { 18 | exit; 19 | } 20 | 21 | /** 22 | * Content_For_Login_User_Shortcode Class File 23 | * 24 | * Simple enclosing tag shortcode to show content only for login users 25 | * 26 | * @package Plugin_Name_Name_Space 27 | * @author Mehdi Soltani 28 | * @link https://github.com/msn60/oop-wordpress-plugin-boilerplate 29 | */ 30 | class Content_For_Login_User_Shortcode extends Shortcode { 31 | 32 | /** 33 | * define_shortcode method in Shortcode Class 34 | * 35 | * For each each defined shortcode, you must define callable function 36 | * for that. This method has this role as a shortcode callable function 37 | * sample for define this shortcode: 38 | * [msn_content_for_login_user]$content[/msn_content_for_login_user] 39 | * 40 | * @param array $atts attributes which can pass throw shortcode in front end 41 | * @param string $content The content between starting and closing shortcode tag 42 | * @param string $tag The name of the shortcode tag 43 | * 44 | * @return string 45 | */ 46 | public function define_shortcode_handler( $atts = [], $content = null, $tag = '' ) { 47 | 48 | if ( is_user_logged_in() ) { 49 | return $content; 50 | } else { 51 | return '
'.__( 'This section only shows for login users', PLUGIN_NAME_TEXTDOMAIN ).'
'; 52 | } 53 | } 54 | 55 | } 56 | -------------------------------------------------------------------------------- /includes/abstracts/class-admin-notice.php: -------------------------------------------------------------------------------- 1 | 10 | * @license https://www.gnu.org/licenses/gpl-3.0.txt GNU/GPLv3 11 | * @link https://github.com/msn60/oop-wordpress-plugin-boilerplate 12 | * @since 1.0.2 13 | */ 14 | 15 | namespace Plugin_Name_Name_Space\Includes\Abstracts; 16 | 17 | 18 | use Plugin_Name_Name_Space\Includes\Functions\Utility; 19 | use Plugin_Name_Name_Space\Includes\Interfaces\Action_Hook_Interface; 20 | 21 | if ( ! defined( 'ABSPATH' ) ) { 22 | exit; 23 | } 24 | 25 | /** 26 | * Admin_Notice abstract Class File 27 | * 28 | * This file contains contract for Admin_Notice class. 29 | * If you want to create a Admin_Notice, you must use from this contract. 30 | * 31 | * @package Plugin_Name_Name_Space 32 | * @author Mehdi Soltani 33 | * @link https://github.com/msn60/oop-wordpress-plugin-boilerplate 34 | * 35 | * @see https://code.tutsplus.com/series/persisted-wordpress-admin-notices--cms-1252 36 | * @see https://code.tutsplus.com/tutorials/persisted-wordpress-admin-notices-part-1--cms-30134 37 | */ 38 | abstract class Admin_Notice implements Action_Hook_Interface { 39 | use Utility; 40 | 41 | /** 42 | * call 'admin_notice' add_action to show notice on admin panel 43 | * 44 | * @access public 45 | */ 46 | public function register_add_action() { 47 | add_action( 'admin_notices', [$this , 'show_admin_notice'] ); 48 | } 49 | 50 | 51 | /** 52 | * Abstract Method show admin notice 53 | * 54 | * For each each defined notice, you must generate it 55 | * 56 | * @param array $args Arguments which are needed to show on notice 57 | */ 58 | abstract public function show_admin_notice(); 59 | 60 | } 61 | -------------------------------------------------------------------------------- /includes/admin/class-option-menu1.php: -------------------------------------------------------------------------------- 1 | 10 | * @license https://www.gnu.org/licenses/gpl-3.0.txt GNU/GPLv3 11 | * @link https://github.com/msn60/oop-wordpress-plugin-boilerplate 12 | * @since 1.0.2 13 | */ 14 | 15 | namespace Plugin_Name_Name_Space\Includes\Admin; 16 | 17 | use Plugin_Name_Name_Space\Includes\Abstracts\Option_Menu; 18 | use Plugin_Name_Name_Space\Includes\Functions\Template_Builder; 19 | 20 | 21 | if ( ! defined( 'ABSPATH' ) ) { 22 | exit; 23 | } 24 | 25 | /** 26 | * Class Option_Menu1. 27 | * If you want create an admin page inside admin panel of WordPress, 28 | * you can use from this class. 29 | * 30 | * @package Plugin_Name_Name_Space 31 | * @author Mehdi Soltani 32 | * 33 | * @see wp-admin/includes/plugin.php 34 | * @see https://developer.wordpress.org/reference/functions/add_menu_page/ 35 | */ 36 | class Option_Menu1 extends Option_Menu { 37 | 38 | /** 39 | * Option_Menu1 constructor. 40 | * This constructor gets initial values to send to add_menu_page function to 41 | * create admin menu. 42 | * 43 | * @access public 44 | * 45 | * @param array $initial_value Initial value to pass to add_option_page function. 46 | */ 47 | public function __construct( array $initial_values ) { 48 | parent::__construct( $initial_values ); 49 | } 50 | 51 | /** 52 | * Method handle_option_panel in Option_Menu Class 53 | * 54 | * For each option menu page, we must have callable function that render and 55 | * handle this option menu page. For each option menu page, you must implement it. 56 | * 57 | * @access public 58 | */ 59 | public function handle_option_panel( $arg = null ) { 60 | $this->load_template( 'options-page.simple-option-page1', [] ); 61 | } 62 | 63 | 64 | } 65 | -------------------------------------------------------------------------------- /includes/admin/class-option-menu2.php: -------------------------------------------------------------------------------- 1 | 10 | * @license https://www.gnu.org/licenses/gpl-3.0.txt GNU/GPLv3 11 | * @link https://github.com/msn60/oop-wordpress-plugin-boilerplate 12 | * @since 1.0.2 13 | */ 14 | 15 | namespace Plugin_Name_Name_Space\Includes\Admin; 16 | 17 | use Plugin_Name_Name_Space\Includes\Abstracts\Option_Menu; 18 | use Plugin_Name_Name_Space\Includes\Functions\Template_Builder; 19 | 20 | 21 | if ( ! defined( 'ABSPATH' ) ) { 22 | exit; 23 | } 24 | 25 | /** 26 | * Class Option_Menu1. 27 | * If you want create an admin page inside admin panel of WordPress, 28 | * you can use from this class. 29 | * 30 | * @package Plugin_Name_Name_Space 31 | * @author Mehdi Soltani 32 | * 33 | * @see wp-admin/includes/plugin.php 34 | * @see https://developer.wordpress.org/reference/functions/add_menu_page/ 35 | */ 36 | class Option_Menu2 extends Option_Menu { 37 | 38 | /** 39 | * Option_Menu1 constructor. 40 | * This constructor gets initial values to send to add_menu_page function to 41 | * create admin menu. 42 | * 43 | * @access public 44 | * 45 | * @param array $initial_value Initial value to pass to add_option_page function. 46 | */ 47 | public function __construct( array $initial_values ) { 48 | parent::__construct( $initial_values ); 49 | } 50 | 51 | /** 52 | * Method handle_option_panel in Option_Menu Class 53 | * 54 | * For each option menu page, we must have callable function that render and 55 | * handle this option menu page. For each option menu page, you must implement it. 56 | * 57 | * @access public 58 | */ 59 | public function handle_option_panel( $extra_args = null ) { 60 | $this->load_template( 'options-page.option-page1', $extra_args ); 61 | } 62 | 63 | 64 | } 65 | -------------------------------------------------------------------------------- /includes/admin/class-admin-sub-menu2.php: -------------------------------------------------------------------------------- 1 | 10 | * @license https://www.gnu.org/licenses/gpl-3.0.txt GNU/GPLv3 11 | * @link https://github.com/msn60/oop-wordpress-plugin-boilerplate 12 | * @since 1.0.2 13 | */ 14 | 15 | namespace Plugin_Name_Name_Space\Includes\Admin; 16 | 17 | use Plugin_Name_Name_Space\Includes\Abstracts\Admin_Sub_Menu; 18 | use Plugin_Name_Name_Space\Includes\Functions\Template_Builder; 19 | 20 | if ( ! defined( 'ABSPATH' ) ) { 21 | exit; 22 | } 23 | 24 | /** 25 | * Class Admin_Sub_Menu2. 26 | * If you want create an sub menu page under an admin page 27 | * (inside Admin panel of WordPress), you can use from this class. 28 | * 29 | * @package Plugin_Name_Name_Space 30 | * @author Mehdi Soltani 31 | * @see wp-admin/includes/plugin.php 32 | * @see https://developer.wordpress.org/reference/functions/add_submenu_page/ 33 | */ 34 | class Admin_Sub_Menu2 extends Admin_Sub_Menu{ 35 | use Template_Builder; 36 | /** 37 | * Admin_Sub_Menu constructor. 38 | * This constructor gets initial values to send to add_submenu_page function to 39 | * create admin submenu. 40 | * 41 | * @access public 42 | * 43 | * @param array $initial_value Initial value to pass to add_submenu_page function. 44 | */ 45 | public function __construct( array $initial_value ) { 46 | parent::__construct($initial_value); 47 | } 48 | 49 | /** 50 | * Method sub_menu1_panel_handler in Admin_Sub_Menu Class 51 | * 52 | * For each admin submenu page, we must have callable function that render and 53 | * handle this menu page. For each menu page, you must have its own function. 54 | * 55 | * @access public 56 | */ 57 | public function render_sub_menu_panel() { 58 | $this->load_template( 'plugin-page.second-section', [] ); 59 | } 60 | 61 | } 62 | -------------------------------------------------------------------------------- /includes/functions/class-check-type.php: -------------------------------------------------------------------------------- 1 | 9 | * @license https://www.gnu.org/licenses/gpl-3.0.txt GNU/GPLv3 10 | * @link https://github.com/msn60/oop-wordpress-plugin-boilerplate 11 | * @since 1.0.2 12 | */ 13 | 14 | namespace Plugin_Name_Name_Space\Includes\Functions; 15 | 16 | if ( ! defined( 'ABSPATH' ) ) { 17 | exit; 18 | } 19 | 20 | /** 21 | * Check_Type trait File 22 | * 23 | * This class contains methods that check type of inside arrays. 24 | * 25 | * @package Plugin_Name_Name_Space 26 | * @author Mehdi Soltani 27 | * @since 1.0.2 28 | */ 29 | trait Check_Type { 30 | 31 | /** 32 | * Method to check type of each item in an array and return them 33 | * 34 | * 35 | * @access public 36 | * 37 | * @param array $items Passed array to check type of each items inside it 38 | * @param string $type type to check 39 | */ 40 | public function check_array_by_parent_type( array $items, $type ): array { 41 | $result['valid'] = []; 42 | $result['invalid'] = []; 43 | foreach ( $items as $item ) { 44 | if ( get_parent_class( $item ) == $type ) { 45 | $result['valid'][] = $item; 46 | } else { 47 | $result['invalid'][] = $item; 48 | } 49 | 50 | } 51 | 52 | return $result; 53 | 54 | } 55 | 56 | /** 57 | * Method to check type of each item in an array and return them for associative arrays 58 | * 59 | * 60 | * @access public 61 | * 62 | * @param array $items Passed array to check type of each items inside it 63 | * @param string $type type to check 64 | */ 65 | public function check_array_by_parent_type_assoc( array $items, $type ): array { 66 | $result['valid'] = []; 67 | $result['invalid'] = []; 68 | foreach ( $items as $key => $item ) { 69 | if ( get_parent_class( $item ) == $type ) { 70 | $result['valid'][ $key ] = $item; 71 | } else { 72 | $result['invalid'][ $key ] = $item; 73 | } 74 | 75 | } 76 | 77 | return $result; 78 | 79 | } 80 | 81 | } 82 | -------------------------------------------------------------------------------- /includes/hooks/filters/class-custom-cron-schedule.php: -------------------------------------------------------------------------------- 1 | 9 | * @license https://www.gnu.org/licenses/gpl-3.0.txt GNU/GPLv3 10 | * @link https://github.com/msn60/oop-wordpress-plugin-boilerplate 11 | * @since 1.0.2 12 | */ 13 | 14 | namespace Plugin_Name_Name_Space\Includes\Hooks\Filters; 15 | 16 | use Plugin_Name_Name_Space\Includes\Interfaces\Filter_Hook_Interface; 17 | 18 | if ( ! defined( 'ABSPATH' ) ) { 19 | exit; 20 | } 21 | 22 | /** 23 | * Class Custom_Cron_Schedule. 24 | * This file contains custom recurrence schedules for cron jobs in WordPress. 25 | * 26 | * @package Plugin_Name_Name_Space 27 | * @author Mehdi Soltani 28 | * @link https://github.com/msn60/oop-wordpress-plugin-boilerplate 29 | * 30 | * @see https://developer.wordpress.org/reference/functions/wp_get_schedules/ 31 | */ 32 | class Custom_Cron_Schedule implements Filter_Hook_Interface { 33 | 34 | /** 35 | * Array of custom event recurrence schedules 36 | * 37 | * @var array $custom_cron_schedules Array of custom event recurrence schedules 38 | */ 39 | private $custom_cron_schedules; 40 | 41 | 42 | /** 43 | * Custom_Cron_Schedule constructor. 44 | * 45 | * @param array $initial_values 46 | */ 47 | public function __construct( array $initial_values ) { 48 | $this->custom_cron_schedules = $initial_values; 49 | } 50 | 51 | /** 52 | * Callable function for 'cron_schedule' filter to dd custom cron schedule. 53 | * 54 | * @access public 55 | * @return array 56 | */ 57 | public function add_custom_cron_schedule($schedules) { 58 | $schedules['weekly'] = $this->custom_cron_schedules['weekly']; 59 | $schedules['twiceweekly'] = $this->custom_cron_schedules['twiceweekly']; 60 | return $schedules; 61 | } 62 | 63 | /** 64 | * Register filters that the object needs to be subscribed to. 65 | * 66 | */ 67 | public function register_add_filter() { 68 | add_filter( 'cron_schedules', [ $this, 'add_custom_cron_schedule' ] ); 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /includes/admin/class-setting-page1.php: -------------------------------------------------------------------------------- 1 | 10 | * @license https://www.gnu.org/licenses/gpl-3.0.txt GNU/GPLv3 11 | * @link https://github.com/msn60/oop-wordpress-plugin-boilerplate 12 | * @since 1.0.2 13 | */ 14 | 15 | namespace Plugin_Name_Name_Space\Includes\Admin; 16 | 17 | use Plugin_Name_Name_Space\Includes\Abstracts\Setting_Page; 18 | 19 | 20 | if ( ! defined( 'ABSPATH' ) ) { 21 | exit; 22 | } 23 | 24 | /** 25 | * Class Setting_Page2. 26 | * This file contains contract for Setting_Page2 class. If you want create an settings page 27 | * inside admin panel of WordPress, you must to use this contract. 28 | * 29 | * @package Plugin_Name_Name_Space 30 | * @author Mehdi Soltani 31 | * @link https://github.com/msn60/oop-wordpress-plugin-boilerplate 32 | * 33 | */ 34 | class Setting_Page1 extends Setting_Page{ 35 | 36 | 37 | /** 38 | * Setting_Page constructor. 39 | * This constructor gets initial values to create a full settings page in admin panel 40 | * 41 | * @access public 42 | * 43 | * @param array $initial_value Initial value to pass to add_menu_page function. 44 | */ 45 | public function __construct( array $initial_values, $admin_menu) { 46 | parent::__construct($initial_values, $admin_menu); 47 | } 48 | 49 | 50 | /** 51 | * Sample method to sanitize text fields 52 | * @param string $field_value A field value which is needed to sanitize 53 | * 54 | * @return string 55 | */ 56 | public function sample_sanitize_text_field( $field_value ) { 57 | $result = array(); 58 | $result = preg_replace( 59 | '/[^a-zA-Z\s]/', 60 | '', 61 | $field_value ); 62 | 63 | return $result; 64 | } 65 | 66 | /** 67 | * Method to create admin menu to show sections and related fields in setting page 68 | */ 69 | public function add_admin_menu() { 70 | $this->admin_menu->register_add_action_with_arguments( $this->settings_sections); 71 | } 72 | 73 | 74 | } 75 | -------------------------------------------------------------------------------- /includes/functions/class-template-builder.php: -------------------------------------------------------------------------------- 1 | 10 | * @license https://www.gnu.org/licenses/gpl-3.0.txt GNU/GPLv3 11 | * @link https://github.com/msn60/oop-wordpress-plugin-boilerplate 12 | * @since 1.0.2 13 | */ 14 | 15 | namespace Plugin_Name_Name_Space\Includes\Functions; 16 | 17 | if ( ! defined( 'ABSPATH' ) ) { 18 | exit; 19 | } 20 | 21 | /** 22 | * Trait Template_Builder. 23 | * This class contains functions that help you to create HTML template by 24 | * including depending files 25 | * 26 | * @package Plugin_Name_Name_Space 27 | * @author Mehdi Soltani 28 | */ 29 | trait Template_Builder { 30 | 31 | /** 32 | * Method load_template in Template_Builder Class 33 | * 34 | * This method calls to render Admin or Front HTML templates from templates/admin 35 | * or templates/front directories. You can use from dot (.) to separate nested 36 | * directories and this method will include your desire file for your plugin. 37 | * 38 | * @access public 39 | * @static 40 | * 41 | * @param string $template Path of template file which is separated by dot. 42 | * @param array $params Related parameters that must be extracted to use inside your template. 43 | * @param string $type To detect admin or front directory to use related constant path. 44 | */ 45 | public function load_template( $template, $params = array(), $type = 'admin' ) { 46 | $template = str_replace( '.', '/', $template ); 47 | $base_path = 'admin' === $type ? PLUGIN_NAME_TPL_ADMIN : PLUGIN_NAME_TPL_FRONT; 48 | $view_file_path = $base_path . $template . '.php'; 49 | if ( file_exists( $view_file_path ) && is_readable( $view_file_path ) ) { 50 | ! empty( $params ) ? extract( $params ) : null; 51 | /** 52 | * Include template file path which will be rendered by your plugin. 53 | */ 54 | include $view_file_path; 55 | } else { 56 | echo '

Your file does not exist.

'; 57 | echo '

Check this file: ' . $view_file_path . '

'; 58 | exit; 59 | } 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /includes/admin/class-simple-setting-in-reading-page1.php: -------------------------------------------------------------------------------- 1 | 9 | * @license https://www.gnu.org/licenses/gpl-3.0.txt GNU/GPLv3 10 | * @link https://github.com/msn60/oop-wordpress-plugin-boilerplate 11 | * @since 1.0.2 12 | */ 13 | 14 | namespace Plugin_Name_Name_Space\Includes\Admin; 15 | 16 | use Plugin_Name_Name_Space\Includes\Abstracts\Simple_Setting_Page; 17 | 18 | 19 | 20 | if ( ! defined( 'ABSPATH' ) ) { 21 | exit; 22 | } 23 | 24 | /** 25 | * Class Setting_Page1. 26 | * If you want create an admin page inside admin panel of WordPress, 27 | * you can use from this class. 28 | * 29 | * @package Plugin_Name_Name_Space 30 | * @author Mehdi Soltani 31 | * 32 | * @see wp-admin/includes/plugin.php 33 | * @see https://developer.wordpress.org/reference/functions/add_menu_page/ 34 | */ 35 | class Simple_Setting_In_Reading_Page1 extends Simple_Setting_Page{ 36 | 37 | /** 38 | * Setting_Page1 constructor. 39 | * 40 | * @access public 41 | * 42 | * @param array $initial_value Initial value to pass to add_option_page function. 43 | */ 44 | public function __construct( array $initial_values ) { 45 | parent::__construct($initial_values); 46 | } 47 | 48 | 49 | public function sanitize_setting_fields( $input ) { 50 | $valid = array(); 51 | $valid['text_field_1_1'] = preg_replace( 52 | '/[^a-zA-Z\s]/', 53 | '', 54 | $input['text_field_1_1'] ); 55 | 56 | //generate error 57 | if ( $valid['text_field_1_1'] !== $input['text_field_1_1'] ) { 58 | $this->create_settings_error('error1'); 59 | } 60 | return $valid; 61 | } 62 | 63 | public function create_section1() { 64 | //echo '

Enter your settings here.

'; 65 | _e( 'Some help text regarding Section One goes here.', PLUGIN_NAME_TEXTDOMAIN ); 66 | } 67 | 68 | 69 | public function create_field_1_1() { 70 | $settings = (array) get_option( $this->option_name ); 71 | //TODO: It can be put in initial values 72 | $field = "text_field_1_1"; 73 | $value = esc_attr( $settings[$field] ); 74 | 75 | echo ""; 76 | } 77 | 78 | } 79 | -------------------------------------------------------------------------------- /includes/uninstall/class-deactivator.php: -------------------------------------------------------------------------------- 1 | 9 | * @license https://www.gnu.org/licenses/gpl-3.0.txt GNU/GPLv3 10 | * @link https://github.com/msn60/oop-wordpress-plugin-boilerplate 11 | * @since 1.0.2 12 | */ 13 | 14 | namespace Plugin_Name_Name_Space\Includes\Uninstall; 15 | 16 | use Plugin_Name_Name_Space\Includes\Functions\Current_User; 17 | use Plugin_Name_Name_Space\Includes\Functions\Logger; 18 | 19 | if ( ! defined( 'ABSPATH' ) ) { 20 | exit; 21 | } 22 | 23 | /** 24 | * Class Deactivator. 25 | * You can run desire tasks with this class when your plugin is de-activated. 26 | * 27 | * @package Plugin_Name_Name_Space 28 | * @author Mehdi Soltani 29 | */ 30 | class Deactivator { 31 | use Current_User; 32 | use Logger; 33 | 34 | /** 35 | * Run related tasks when plugin is deactivated 36 | * 37 | * @access public 38 | * @since 1.0.2 39 | */ 40 | public function deactivate() { 41 | 42 | $this->register_deactivator_user(); 43 | 44 | if ( get_option( 'plugin_name_prefix_plugin_setting_option2' ) ) { 45 | update_option( 46 | 'plugin_name_prefix_plugin_setting_option2', 47 | 'After de-activation' 48 | ); 49 | } 50 | 51 | if ( get_option( 'plugin_name_prefix_plugin_setting_option3' ) ) { 52 | delete_option( 'plugin_name_prefix_plugin_setting_option3' ); 53 | } 54 | 55 | 56 | if ( get_option( 'has_rewrite_for_plugin_name_new_post_types' ) ) { 57 | update_option( 58 | 'has_rewrite_for_plugin_name_new_post_types', 59 | false 60 | ); 61 | } 62 | 63 | if ( get_option( 'has_rewrite_for_plugin_name_new_taxonomies' ) ) { 64 | update_option( 65 | 'has_rewrite_for_plugin_name_new_taxonomies', 66 | false 67 | ); 68 | } 69 | } 70 | 71 | /** 72 | * Register user who de-activate the plugin 73 | */ 74 | public function register_deactivator_user() { 75 | 76 | $current_user = $this->get_this_login_user(); 77 | $this->append_log_in_text_file( 78 | 'The user with login of: "' . $current_user->user_login . '" and display name of: "' . $current_user->display_name 79 | . '" de-activated this plugin', 80 | PLUGIN_NAME_LOGS . 'deactivator-logs.txt', 81 | 'De-Activator User' ); 82 | 83 | } 84 | 85 | } 86 | 87 | 88 | -------------------------------------------------------------------------------- /includes/admin/class-meta-box3.php: -------------------------------------------------------------------------------- 1 | 9 | * @license https://www.gnu.org/licenses/gpl-3.0.txt GNU/GPLv3 10 | * @link https://github.com/msn60/oop-wordpress-plugin-boilerplate 11 | * @since 1.0.2 12 | */ 13 | 14 | namespace Plugin_Name_Name_Space\Includes\Admin; 15 | 16 | use Plugin_Name_Name_Space\Includes\Abstracts\Meta_box; 17 | 18 | if ( ! defined( 'ABSPATH' ) ) { 19 | exit; 20 | } 21 | 22 | /** 23 | * Class Meta_Box3. 24 | * Methods and settings which will need for meta box1 25 | * 26 | * @package Plugin_Name_Name_Space 27 | * @author Mehdi Soltani 28 | */ 29 | class Meta_Box3 extends Meta_box { 30 | 31 | /** 32 | * Meta_box constructor. 33 | * This constructor gets initial values to send to add_meta_box function to 34 | * create admin menu. 35 | * 36 | * @access public 37 | * 38 | * @param array $initial_value Initial value to pass to add_meta_box function. 39 | */ 40 | public function __construct( $initial_values ) { 41 | parent::__construct( $initial_values ); 42 | } 43 | 44 | 45 | /** 46 | * Render meta box 1. 47 | * 48 | * @access public 49 | * 50 | * @see https://wpbrigade.com/what-is-wordpress-nonce-and-how-it-works/ 51 | * 52 | * @param object $post Current post. 53 | */ 54 | public function render_content( $post ) { 55 | 56 | // Add an nonce field so we can check for it later. 57 | wp_nonce_field( $this->action, $this->nonce_name ); 58 | 59 | // Use get_post_meta to retrieve an existing value from the database. 60 | $value = get_post_meta( $post->ID, $this->meta_key, $this->single ); 61 | 62 | 63 | // Display the form, using the current value. 64 | ?> 65 | 68 | 70 | meta_key, $meta_value ); 79 | } 80 | 81 | } 82 | -------------------------------------------------------------------------------- /includes/admin/notices/class-admin-notice1.php: -------------------------------------------------------------------------------- 1 | 9 | * @license https://www.gnu.org/licenses/gpl-3.0.txt GNU/GPLv3 10 | * @link https://github.com/msn60/oop-wordpress-plugin-boilerplate 11 | * @since 1.0.2 12 | */ 13 | 14 | namespace Plugin_Name_Name_Space\Includes\Admin\Notices; 15 | 16 | 17 | use Plugin_Name_Name_Space\Includes\Abstracts\Admin_Notice; 18 | 19 | if ( ! defined( 'ABSPATH' ) ) { 20 | exit; 21 | } 22 | 23 | /** 24 | * Admin_Notice1 Class File 25 | * 26 | * This file contains contract for Admin_Notice1 class. 27 | * If you want to create a Admin_Notice1, you must use from this contract. 28 | * 29 | * @package Plugin_Name_Name_Space 30 | * @author Mehdi Soltani 31 | * @link https://github.com/msn60/oop-wordpress-plugin-boilerplate 32 | * 33 | * @see https://code.tutsplus.com/series/persisted-wordpress-admin-notices--cms-1252 34 | * @see https://code.tutsplus.com/tutorials/persisted-wordpress-admin-notices-part-1--cms-30134 35 | */ 36 | class Admin_Notice1 extends Admin_Notice { 37 | 38 | 39 | /** 40 | * Abstract Method show admin notice 41 | * 42 | * For each each defined notice, you must generate it 43 | * 44 | * @param array $args Arguments which are needed to show on notice 45 | */ 46 | public function show_admin_notice() { 47 | 48 | $actual_link = ( 'on' === isset( $_SERVER['HTTPS'] ) && $_SERVER['HTTPS'] ? 'https' : 'http' ) 49 | . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; 50 | 51 | if ( preg_match( '/plugin-name-option-page-url-2/', $actual_link ) ) { 52 | ?> 53 |

Sample of Admin success notice

54 |

Sample of Admin warning notice

55 |

Sample of Admin info notice

56 |

Sample of Admin error notice

57 | 58 | 10 | * @license https://www.gnu.org/licenses/gpl-3.0.txt GNU/GPLv3 11 | * @link https://github.com/msn60/oop-wordpress-plugin-boilerplate 12 | * @since 1.0.2 13 | */ 14 | 15 | namespace Plugin_Name_Name_Space\Includes\Admin; 16 | 17 | use Plugin_Name_Name_Space\Includes\Abstracts\Admin_Sub_Menu; 18 | use Plugin_Name_Name_Space\Includes\Functions\Template_Builder; 19 | 20 | if ( ! defined( 'ABSPATH' ) ) { 21 | exit; 22 | } 23 | 24 | /** 25 | * Class Admin_Sub_Menu1. 26 | * If you want create an sub menu page under an admin page 27 | * (inside Admin panel of WordPress), you can use from this class. 28 | * 29 | * @package Plugin_Name_Name_Space 30 | * @author Mehdi Soltani 31 | * @see wp-admin/includes/plugin.php 32 | * @see https://developer.wordpress.org/reference/functions/add_submenu_page/ 33 | * @see https://codex.wordpress.org/Creating_Options_Pages 34 | * @see https://codex.wordpress.org/Settings_API 35 | * @see https://wisdmlabs.com/blog/create-settings-options-page-for-wordpress-plugin/ 36 | * @see https://www.smashingmagazine.com/2016/04/three-approaches-to-adding-configurable-fields-to-your-plugin/ 37 | */ 38 | class Admin_Sub_Menu1 extends Admin_Sub_Menu { 39 | use Template_Builder; 40 | 41 | /** 42 | * Admin_Sub_Menu constructor. 43 | * This constructor gets initial values to send to add_submenu_page function to 44 | * create admin submenu. 45 | * 46 | * @access public 47 | * 48 | * @param array $initial_value Initial value to pass to add_submenu_page function. 49 | */ 50 | public function __construct( array $initial_value ) { 51 | parent::__construct( $initial_value ); 52 | } 53 | 54 | /** 55 | * Method sub_menu1_panel_handler in Admin_Sub_Menu Class 56 | * 57 | * For each admin submenu page, we must have callable function that render and 58 | * handle this menu page. For each menu page, you must have its own function. 59 | * 60 | * @access public 61 | * @see https://codex.wordpress.org/index.php?title=Creating_Options_Pages&oldid=97268 62 | * @see https://wisdmlabs.com/blog/create-settings-options-page-for-wordpress-plugin/ 63 | */ 64 | public function render_sub_menu_panel() { 65 | $this->load_template( 'plugin-page.primary-section', [] ); 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /includes/parts/other/class-remove-post-column.php: -------------------------------------------------------------------------------- 1 | 10 | * @license https://www.gnu.org/licenses/gpl-3.0.txt GNU/GPLv3 11 | * @link https://github.com/msn60/oop-wordpress-plugin-boilerplate 12 | * @since 1.0.2 13 | */ 14 | 15 | namespace Plugin_Name_Name_Space\Includes\Parts\Other; 16 | 17 | use Plugin_Name_Name_Space\Includes\Interfaces\Custom_Admin_Columns\Manage_Post_Columns; 18 | 19 | if ( ! defined( 'ABSPATH' ) ) { 20 | exit; 21 | } 22 | 23 | /** 24 | * Class Remove_Post_Column. 25 | * This file contains contract for Remove_Post_Column class. If you want create a 26 | * custom post type in WordPress, you must to use this contract. 27 | * 28 | * @package Plugin_Name_Name_Space 29 | * @author Mehdi Soltani 30 | * @link https://github.com/msn60/oop-wordpress-plugin-boilerplate 31 | * 32 | * @see https://carlalexander.ca/saving-wordpress-custom-post-types-using-interface/ 33 | * @see https://developer.wordpress.org/reference/functions/register_post_type/ 34 | * @see https://carlalexander.ca/designing-entities-wordpress-custom-post-types/ 35 | * @see https://www.hostinger.com/tutorials/wordpress-custom-post-types 36 | */ 37 | class Remove_Post_Column implements Manage_Post_Columns { 38 | 39 | 40 | public function register_add_filter() { 41 | add_filter( 'manage_posts_columns', array( $this, 'manage_columns_list' ) ); 42 | } 43 | 44 | /** 45 | * Unset some columns in posts 46 | * 47 | * 48 | * ' cb' => string '' (length=25) 49 | * 'title' => string 'عنوان' (length=10) 50 | * 'author' => string 'نویسنده' (length=14) 51 | * 'categories' => string 'دسته‌ها' (length=15) 52 | * 'tags' => string 'برچسب‌ها' (length=17) 53 | * 'comments' => string 'دیدگاه‌ها' (length=133) 54 | * 'date' => string 'تاریخ' (length=10) 55 | * 56 | * @param array $columns 57 | * 58 | * @return array 59 | */ 60 | public function manage_columns_list( $columns ) { 61 | /*var_dump( $columns ); 62 | if you want to see list of columns you must use from title 63 | unset ($columns['title']);*/ 64 | unset( $columns['comments'] ); 65 | unset( $columns['author'] ); 66 | unset( $columns['tags'] ); 67 | 68 | return $columns; 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /includes/interfaces/custom-admin-columns/class-manage-post-columns.php: -------------------------------------------------------------------------------- 1 | 10 | * @license https://www.gnu.org/licenses/gpl-3.0.txt GNU/GPLv3 11 | * @link https://github.com/msn60/oop-wordpress-plugin-boilerplate 12 | * @since 1.0.2 13 | */ 14 | 15 | namespace Plugin_Name_Name_Space\Includes\Interfaces\Custom_Admin_Columns; 16 | 17 | use Plugin_Name_Name_Space\Includes\Interfaces\Filter_Hook_Interface; 18 | 19 | if ( ! defined( 'ABSPATH' ) ) { 20 | exit; 21 | } 22 | 23 | /** 24 | * Interface Manage_Post_Columns. 25 | * This file contains contract for adding columns that are specific to 26 | * your content type, removing columns that are obsolete, and reordering columns 27 | * 28 | * @package Plugin_Name_Name_Space 29 | * @author Mehdi Soltani 30 | * @link https://github.com/msn60/oop-wordpress-plugin-boilerplate 31 | * 32 | * @see https://www.smashingmagazine.com/2017/12/customizing-admin-columns-wordpress/ 33 | * @see https://developer.wordpress.org/reference/hooks/manage_post_type_posts_columns/ 34 | * @see https://joebuckle.me/wordpress-patterns-elegant-way-create-custom-post-type/ 35 | * @see https://gist.github.com/igorbenic/a93fc46d03aa90411523 36 | */ 37 | interface Manage_Post_Columns extends Filter_Hook_Interface { 38 | 39 | /** 40 | * call related filter to Manage columns of your post 41 | * 42 | * Sample is something like in following inside this method: 43 | * add_filter( 'manage_product_posts_columns', array($this, 'manage_column_list')); 44 | * You can use Hooks like: 45 | * manage_[post_type]_posts_columns : manage_page_posts_columns 46 | * manage_[post_type]_posts_custom_column : manage_page_posts_custom_column 47 | * 48 | * @access public 49 | * @see https://developer.wordpress.org/reference/hooks/manage_posts_columns/ 50 | * @see https://developer.wordpress.org/reference/hooks/manage_post_type_posts_columns/ 51 | */ 52 | public function register_add_filter(); 53 | 54 | /** 55 | * A method for ADDING, REMOVING AND REORDERING COLUMNS 56 | * 57 | * @see https://www.smashingmagazine.com/2017/12/customizing-admin-columns-wordpress/ 58 | * @see https://wordpress.stackexchange.com/questions/116122/how-to-use-manage-post-type-posts-columns-with-underscore-in-post-type 59 | */ 60 | public function manage_columns_list( $columns ); 61 | 62 | 63 | } 64 | -------------------------------------------------------------------------------- /includes/parts/custom-posts/class-custom-post1.php: -------------------------------------------------------------------------------- 1 | 10 | * @license https://www.gnu.org/licenses/gpl-3.0.txt GNU/GPLv3 11 | * @link https://github.com/msn60/oop-wordpress-plugin-boilerplate 12 | * @since 1.0.2 13 | */ 14 | 15 | namespace Plugin_Name_Name_Space\Includes\Parts\Custom_Posts; 16 | 17 | use Plugin_Name_Name_Space\Includes\Interfaces\Action_Hook_Interface; 18 | use Plugin_Name_Name_Space\Includes\Abstracts\Custom_Post_Type; 19 | 20 | if ( ! defined( 'ABSPATH' ) ) { 21 | exit; 22 | } 23 | 24 | /** 25 | * Class Custom_Post1. 26 | * This file contains contract for Custom_Post1 class. 27 | * 28 | * @package Plugin_Name_Name_Space 29 | * @author Mehdi Soltani 30 | * @link https://github.com/msn60/oop-wordpress-plugin-boilerplate 31 | * 32 | * @see https://carlalexander.ca/saving-wordpress-custom-post-types-using-interface/ 33 | * @see https://developer.wordpress.org/reference/functions/register_post_type/ 34 | * @see https://carlalexander.ca/designing-entities-wordpress-custom-post-types/ 35 | * @see https://www.hostinger.com/tutorials/wordpress-custom-post-types 36 | */ 37 | class Custom_Post1 extends Custom_Post_Type { 38 | 39 | /** 40 | * The name of the product. 41 | * 42 | * @var string $name 43 | */ 44 | private $name; 45 | 46 | /** 47 | * The price of the product. 48 | * 49 | * @var float $price 50 | */ 51 | private $price; 52 | 53 | /** 54 | * Custom_Post1 constructor. 55 | * 56 | * @param array $initial_values 57 | * @param string $name 58 | * @param float $price 59 | */ 60 | public function __construct( array $initial_values, $name = null, $price = null ) { 61 | parent::__construct( $initial_values ); 62 | $this->name = $name; 63 | $this->price = $price; 64 | } 65 | 66 | 67 | /** 68 | * Get the post data as a wp_insert_post compatible array. 69 | * 70 | * @access public 71 | * @return array 72 | */ 73 | public function get_post_data() { 74 | return array( 75 | 'post_content' => $this->args['description'], 76 | 'post_title' => $this->args['labels']['name'], 77 | 'post_status' => 'publish', 78 | 'post_type' => $this->post_type 79 | ); 80 | } 81 | 82 | /** 83 | * Get all the post meta as a key-value associative array. 84 | * 85 | * @access public 86 | * @return array 87 | */ 88 | public function get_post_meta() { 89 | return [ 90 | 'price' => $this->price, 91 | 'name' => $this->name 92 | ]; 93 | } 94 | 95 | } 96 | -------------------------------------------------------------------------------- /includes/parts/shortcodes/class-complete-shortcode.php: -------------------------------------------------------------------------------- 1 | 8 | * @license https://www.gnu.org/licenses/gpl-3.0.txt GNU/GPLv3 9 | * @link https://github.com/msn60/oop-wordpress-plugin-boilerplate 10 | * @since 1.0.2 11 | */ 12 | 13 | namespace Plugin_Name_Name_Space\Includes\Parts\Shortcodes; 14 | 15 | use Plugin_Name_Name_Space\Includes\Abstracts\Shortcode; 16 | 17 | if ( ! defined( 'ABSPATH' ) ) { 18 | exit; 19 | } 20 | 21 | /** 22 | * Complete_Shortcode Class File 23 | * 24 | * Simple self-closing tag shortcode sample class 25 | * 26 | * @package Plugin_Name_Name_Space 27 | * @author Mehdi Soltani 28 | * @link https://github.com/msn60/oop-wordpress-plugin-boilerplate 29 | */ 30 | class Complete_Shortcode extends Shortcode { 31 | 32 | /** 33 | * define_shortcode method in Shortcode Class 34 | * 35 | * For each each defined shortcode, you must define callable function 36 | * for that. This method has this role as a shortcode callable function 37 | * sample for define this shortcode: 38 | * [msn_complete_shortcode] OR [/msn_complete_shortcode] 39 | * 40 | * @param array $atts attributes which can pass throw shortcode in front end 41 | * @param string $content The content between starting and closing shortcode tag 42 | * @param string $tag The name of the shortcode tag 43 | * 44 | * @return string 45 | */ 46 | public function define_shortcode_handler( $atts = [], $content = null, $tag = '' ) { 47 | 48 | // normalize attribute keys, lowercase 49 | $atts = array_change_key_case( (array) $atts, CASE_LOWER ); 50 | 51 | // override default attributes with user attributes 52 | $shortcode_attributes = shortcode_atts( [ 53 | 'link' => $this->default_atts['link'], 54 | 'name' => $this->default_atts['name'], 55 | ], $atts, $tag ); 56 | 57 | // start output 58 | $output = ''; 59 | 60 | // enclosing tags 61 | if ( ! is_null( $content ) ) { 62 | // secure output by executing the_content filter hook on $content 63 | //$output .= apply_filters( 'the_content', $content ); 64 | 65 | // run shortcode parser recursively 66 | //$output .= do_shortcode( $content ); 67 | // use content 68 | $output .= '

' . do_shortcode( $content ) . '

'; 69 | } 70 | 71 | // start box 72 | $output .= '
'; 73 | 74 | // generate a link 75 | $output .= 'Click here to access ' 76 | . esc_html__( $shortcode_attributes['name'], PLUGIN_NAME_TEXTDOMAIN ) . ''; 77 | 78 | // end box 79 | $output .= '
'; 80 | 81 | // return output 82 | return $output; 83 | } 84 | 85 | 86 | } 87 | -------------------------------------------------------------------------------- /includes/admin/class-simple-setting-page1.php: -------------------------------------------------------------------------------- 1 | 9 | * @license https://www.gnu.org/licenses/gpl-3.0.txt GNU/GPLv3 10 | * @link https://github.com/msn60/oop-wordpress-plugin-boilerplate 11 | * @since 1.0.2 12 | */ 13 | 14 | namespace Plugin_Name_Name_Space\Includes\Admin; 15 | 16 | use Plugin_Name_Name_Space\Includes\Abstracts\Simple_Setting_Page; 17 | use Plugin_Name_Name_Space\Includes\Functions\Template_Builder; 18 | 19 | 20 | 21 | if ( ! defined( 'ABSPATH' ) ) { 22 | exit; 23 | } 24 | 25 | /** 26 | * Class Setting_Page1. 27 | * If you want create an admin page inside admin panel of WordPress, 28 | * you can use from this class. 29 | * 30 | * @package Plugin_Name_Name_Space 31 | * @author Mehdi Soltani 32 | * 33 | * @see wp-admin/includes/plugin.php 34 | * @see https://developer.wordpress.org/reference/functions/add_menu_page/ 35 | */ 36 | class Simple_Setting_Page1 extends Simple_Setting_Page{ 37 | use Template_Builder; 38 | 39 | /** 40 | * Setting_Page1 constructor. 41 | * 42 | * @access public 43 | * 44 | * @param array $initial_value Initial value to pass to add_option_page function. 45 | */ 46 | public function __construct( array $initial_values ) { 47 | parent::__construct($initial_values); 48 | } 49 | 50 | 51 | public function sanitize_setting_fields( $input ) { 52 | $valid = array(); 53 | $valid['text_field_1_1'] = preg_replace( 54 | '/[^a-zA-Z\s]/', 55 | '', 56 | $input['text_field_1_1'] ); 57 | 58 | $valid['text_field_1_2'] = sanitize_text_field($input['text_field_1_2']); 59 | 60 | //generate error 61 | if ( $valid['text_field_1_1'] !== $input['text_field_1_1'] ) { 62 | $this->create_settings_error('error1'); 63 | } 64 | return $valid; 65 | } 66 | 67 | public function create_section1() { 68 | //echo '

Enter your settings here.

'; 69 | _e( 'Some help text regarding Section One goes here.', PLUGIN_NAME_TEXTDOMAIN ); 70 | } 71 | 72 | public function create_section2() { 73 | //echo '

Enter your settings here.

'; 74 | _e( 'Some help text regarding Section Two goes here.', PLUGIN_NAME_TEXTDOMAIN ); 75 | } 76 | 77 | public function create_field_1_1() { 78 | $settings = (array) get_option( $this->option_name ); 79 | //TODO: It can be put in initial values 80 | $field = "text_field_1_1"; 81 | $value = esc_attr( $settings[$field] ); 82 | 83 | echo ""; 84 | } 85 | 86 | public function create_field_1_2() { 87 | $settings = (array) get_option( $this->option_name ); 88 | //TODO: It can be put in initial values 89 | $field = "text_field_1_2"; 90 | $value = esc_attr( $settings[$field] ); 91 | 92 | echo ""; 93 | } 94 | 95 | } 96 | -------------------------------------------------------------------------------- /includes/admin/class-meta-box4.php: -------------------------------------------------------------------------------- 1 | 9 | * @license https://www.gnu.org/licenses/gpl-3.0.txt GNU/GPLv3 10 | * @link https://github.com/msn60/oop-wordpress-plugin-boilerplate 11 | * @since 1.0.2 12 | */ 13 | 14 | namespace Plugin_Name_Name_Space\Includes\Admin; 15 | 16 | use Plugin_Name_Name_Space\Includes\Abstracts\Meta_box; 17 | 18 | if ( ! defined( 'ABSPATH' ) ) { 19 | exit; 20 | } 21 | 22 | /** 23 | * Class Meta_Box4. 24 | * Methods and settings which will need for meta box1 25 | * 26 | * @package Plugin_Name_Name_Space 27 | * @author Mehdi Soltani 28 | */ 29 | class Meta_Box4 extends Meta_box { 30 | 31 | /** 32 | * Meta_box constructor. 33 | * This constructor gets initial values to send to add_meta_box function to 34 | * create admin menu. 35 | * 36 | * @access public 37 | * 38 | * @param array $initial_value Initial value to pass to add_meta_box function. 39 | */ 40 | public function __construct( $initial_values ) { 41 | parent::__construct( $initial_values ); 42 | } 43 | 44 | 45 | /** 46 | * Render meta box 1. 47 | * 48 | * @access public 49 | * 50 | * @see https://wpbrigade.com/what-is-wordpress-nonce-and-how-it-works/ 51 | * 52 | * @param object $post Current post. 53 | */ 54 | public function render_content( $post ) { 55 | // Add an nonce field so we can check for it later. 56 | wp_nonce_field( $this->action, $this->nonce_name ); 57 | 58 | // Use get_post_meta to retrieve an existing value from the database. 59 | $values = get_post_meta( $post->ID, $this->meta_key, $this->single ); 60 | if ( ! empty( $values ) ) { 61 | $values = get_post_meta( $post->ID, $this->meta_key, $this->single ); 62 | $values = array_shift( $values ); 63 | } 64 | 65 | // Display the form, using the current value. 66 | ?> 67 |
68 | 71 | 74 |
75 |
76 |
77 | 80 | 83 |
84 | 85 | meta_key, $meta_value ); 95 | } 96 | 97 | } 98 | -------------------------------------------------------------------------------- /includes/abstracts/class-custom-taxonomy.php: -------------------------------------------------------------------------------- 1 | 10 | * @license https://www.gnu.org/licenses/gpl-3.0.txt GNU/GPLv3 11 | * @link https://github.com/msn60/oop-wordpress-plugin-boilerplate 12 | * @since 1.0.2 13 | */ 14 | 15 | namespace Plugin_Name_Name_Space\Includes\Abstracts; 16 | 17 | use Plugin_Name_Name_Space\Includes\Interfaces\Action_Hook_Interface; 18 | 19 | if ( ! defined( 'ABSPATH' ) ) { 20 | exit; 21 | } 22 | 23 | /** 24 | * Class Custom_Taxonomy. 25 | * This file contains contract for Custom_Taxonomy class. If you want create a 26 | * custom post type in WordPress, you must to use this contract. 27 | * 28 | * @package Plugin_Name_Name_Space 29 | * @author Mehdi Soltani 30 | * @link https://github.com/msn60/oop-wordpress-plugin-boilerplate 31 | * 32 | * @see https://carlalexander.ca/saving-wordpress-custom-post-types-using-interface/ 33 | * @see https://developer.wordpress.org/reference/functions/register_taxonomy/ 34 | * @see https://www.smashingmagazine.com/2012/01/create-custom-taxonomies-wordpress/ 35 | */ 36 | class Custom_Taxonomy implements Action_Hook_Interface { 37 | 38 | /** 39 | * Post Taxonomy key. 40 | * Taxonomy key, must not exceed 32 characters. 41 | * 42 | * @access protected 43 | * @var string $taxonomy The key of custom taxonomy 44 | * @since 1.0.2 45 | */ 46 | protected $taxonomy; 47 | /** 48 | * Object type. 49 | * Object type or array of object types with which the taxonomy should be associated. 50 | * 51 | * @access protected 52 | * @var array|string $object_type Array of associated object with the taxonomy 53 | * @since 1.0.2 54 | */ 55 | protected $object_type; 56 | /** 57 | * Array or string of arguments for registering a taxonomy. 58 | * 59 | * @access protected 60 | * @var array | string $args Array or string of arguments for registering a Taxonomy. 61 | * @since 1.0.2 62 | */ 63 | protected $args; 64 | 65 | /** 66 | * Custom_Taxonomy constructor. 67 | * This constructor gets initial values to send to add_menu_page function to 68 | * create admin menu. 69 | * 70 | * @access public 71 | * 72 | * @param array $initial_value Initial value to pass to add_menu_page function. 73 | */ 74 | public function __construct( array $initial_values ) { 75 | $this->taxonomy = $initial_values['taxonomy']; 76 | $this->object_type = $initial_values['object_type']; 77 | $this->args = $initial_values['args']; 78 | } 79 | 80 | /** 81 | * Method to register custom taxonomy 82 | * 83 | * Inside this method, we call register_post_type to create custom post type 84 | * 85 | * @access public 86 | * @see https://developer.wordpress.org/reference/functions/register_post_type/ 87 | */ 88 | public function add_custom_taxonomy() { 89 | register_taxonomy( $this->taxonomy, $this->object_type, $this->args ); 90 | } 91 | 92 | /** 93 | * call 'init' add_action to create custom taxonomy 94 | * 95 | * @access public 96 | */ 97 | public function register_add_action() { 98 | add_action( 'init', array( $this, 'add_custom_taxonomy' ) ); 99 | } 100 | 101 | } 102 | -------------------------------------------------------------------------------- /includes/class-autoloader.php: -------------------------------------------------------------------------------- 1 | 10 | * @license https://www.gnu.org/licenses/gpl-3.0.txt GNU/GPLv3 11 | * @link https://github.com/msn60/oop-wordpress-plugin-boilerplate 12 | * @since 1.0.2 13 | */ 14 | 15 | namespace Plugin_Name_Name_Space\Includes; 16 | 17 | if ( ! defined( 'ABSPATH' ) ) { 18 | exit; 19 | } 20 | 21 | /** 22 | * Class Autoloader 23 | * 24 | * Autoloader class can manage and handle using classes and files in whole of 25 | * your plugin by including them when they are needed. 26 | * 27 | * @package Plugin_Name_Name_Space 28 | * @author Mehdi Soltani 29 | */ 30 | class Autoloader { 31 | 32 | /** 33 | * Autoloader constructor. 34 | * This constructor calls spl_autoload_register method with autoload method 35 | * inside Autoloader class. 36 | * 37 | * @access public 38 | */ 39 | public function __construct() { 40 | spl_autoload_register( array( $this, 'autoload' ) ); 41 | } 42 | 43 | /** 44 | * Method to handle Undefined Classes and include them when script is running. 45 | * 46 | * @param string $class_name The name of class that passed throw spl_auto_register. 47 | */ 48 | public function autoload( $class_name ) { 49 | // If the specified $class_name does not include our namespace, duck out. 50 | if ( false === strpos( $class_name, 'Plugin_Name_Name_Space' ) ) { 51 | return; 52 | } 53 | 54 | // Split the class name into an array to read the namespace and class. 55 | $file_parts = explode( '\\', $class_name ); 56 | 57 | // Do a reverse loop through $file_parts to build the path to the file. 58 | $namespace = ''; 59 | $file_name = ''; 60 | for ( $i = count( $file_parts ) - 1; $i > 0; $i -- ) { 61 | // Read the current component of the file part. 62 | $current = strtolower( $file_parts[ $i ] ); 63 | $current = str_ireplace( '_', '-', $current ); 64 | 65 | // If we're at the first entry, then we're at the filename. 66 | if ( count( $file_parts ) - 1 === $i ) { 67 | $file_name = "class-$current.php"; 68 | /** If 'interface' is contained in the parts of the file name, then 69 | * define the $file_name differently so that it's properly loaded 70 | * Otherwise, just set the $file_name equal to that of the class 71 | * filename structure. 72 | */ 73 | /*if ( strpos( strtolower( $file_parts[ count( $file_parts ) - 1 ] ), 'interface' ) ) { 74 | 75 | // Grab the name of the interface from its qualified name. 76 | $interface_name = explode( '_', $file_parts[ count( $file_parts ) - 1 ] ); 77 | $interface_name = $interface_name[0]; 78 | 79 | $file_name = "interface-$interface_name.php"; 80 | 81 | } else { 82 | $file_name = "class-$current.php"; 83 | }*/ 84 | } else { 85 | $namespace = '/' . $current . $namespace; 86 | } 87 | } 88 | 89 | // Now build a path to the file using mapping to the file location. 90 | $file_path = trailingslashit( dirname( dirname( __FILE__ ) ) . $namespace ); 91 | $file_path .= $file_name; 92 | 93 | // If the file exists in the specified path, then include it. 94 | if ( file_exists( $file_path ) ) { 95 | include_once $file_path; 96 | } else { 97 | wp_die( 98 | esc_html( "The file attempting to be loaded at $file_path does not exist." ) 99 | ); 100 | } 101 | } 102 | } 103 | 104 | new Autoloader(); 105 | -------------------------------------------------------------------------------- /includes/functions/class-init-functions.php: -------------------------------------------------------------------------------- 1 | 10 | * @license https://www.gnu.org/licenses/gpl-3.0.txt GNU/GPLv3 11 | * @link https://github.com/msn60/oop-wordpress-plugin-boilerplate 12 | * @since 1.0.2 13 | */ 14 | 15 | namespace Plugin_Name_Name_Space\Includes\Functions; 16 | 17 | use Plugin_Name_Name_Space\Includes\Interfaces\Action_Hook_Interface; 18 | 19 | if ( ! defined( 'ABSPATH' ) ) { 20 | exit; 21 | } 22 | 23 | /** 24 | * Class Init_Functions. 25 | * This class handle all of initial functions or methods 26 | * that you need alongside of your plugin. 27 | * 28 | * @package Plugin_Name_Name_Space 29 | * @author Mehdi Soltani 30 | */ 31 | class Init_Functions implements Action_Hook_Interface { 32 | 33 | /** 34 | * Method app_output_buffer in Init_Functions Class 35 | * 36 | * This function will turn output buffering on. While output buffering 37 | * is active no output is sent from the script (other than headers), 38 | * instead the output is stored in an internal buffer. 39 | * 40 | * @access public 41 | * @static 42 | * @see http://php.net/manual/en/function.ob-start.php 43 | * @see https://wpshout.com/php-output-buffering/ 44 | */ 45 | public function app_output_buffer() { 46 | ob_start(); 47 | } 48 | 49 | /** 50 | * Method not_access_to_wp_admin_panel in Init_Functions Class 51 | * 52 | * This method checks to avoid access of some caps to admin panel 53 | * 54 | * @access public 55 | * @see https://alka-web.com/blog/how-to-restrict-access-to-wordpress-dashboard-programmatically/ 56 | */ 57 | public function not_access_to_wp_admin_panel() { 58 | if ( is_admin() && ! ( defined( 'DOING_AJAX' ) && DOING_AJAX ) && ( current_user_can( 'your_custom_cap' ) ) ) { 59 | wp_safe_redirect( home_url(), 302 ); 60 | exit; 61 | } 62 | } 63 | 64 | /** 65 | * Method not_access_to_wp_admin_panel_for_list in Init_Functions Class 66 | * 67 | * This method checks to avoid access of a list (for some roles/caps) to admin panel 68 | * 69 | * @access public 70 | * @see https://alka-web.com/blog/how-to-restrict-access-to-wordpress-dashboard-programmatically/ 71 | */ 72 | public function not_access_to_wp_admin_panel_for_list() { 73 | 74 | // Check if the current page is an admin page && and ensure that this is not an ajax call. 75 | if ( is_admin() && ! ( defined( 'DOING_AJAX' ) && DOING_AJAX ) ) { 76 | // Get all capabilities of the current user. 77 | $user = get_userdata( get_current_user_id() ); 78 | $caps = ( is_object( $user ) ) ? array_keys( $user->allcaps ) : array(); 79 | 80 | // All capabilities/roles listed here are not able to see the dashboard. 81 | $block_access_to = array( 'subscriber', 'contributor', 'your_custom_role', 'your_custom_capability' ); 82 | 83 | if ( array_intersect( $block_access_to, $caps ) ) { 84 | wp_safe_redirect( home_url(), 302 ); 85 | exit; 86 | } 87 | } 88 | } 89 | 90 | /** 91 | * Method remove_admin_bar in Init_Functions Class 92 | * 93 | * This method removes admin bar in front end. 94 | * 95 | * @access public 96 | */ 97 | public function remove_admin_bar() { 98 | if ( current_user_can( 'your_custom_cap' ) ) { 99 | show_admin_bar( false ); 100 | } 101 | } 102 | 103 | /** 104 | * Register actions that the object needs to be subscribed to. 105 | * 106 | */ 107 | public function register_add_action() { 108 | add_action( 'init', array( $this, 'app_output_buffer' ) ); 109 | } 110 | } 111 | -------------------------------------------------------------------------------- /templates/admin/options-page/sample-option-page3.php: -------------------------------------------------------------------------------- 1 | 59 | 60 | 89 | 90 |
91 |

WP OOP Settings API MSN GHOLAM

92 |
93 | '; 95 | 96 | foreach ( $params as $tab ) { 97 | $html .= sprintf( '%2$s', $tab['id'], $tab['header_title'] ); 98 | } 99 | 100 | $html .= ''; 101 | 102 | echo $html; 103 | ?> 104 | 105 | 106 |
107 | 108 | 109 |
110 |
111 | 116 |
117 |
118 | 119 |
120 |
121 | 122 | -------------------------------------------------------------------------------- /includes/init/class-admin-hook.php: -------------------------------------------------------------------------------- 1 | 10 | * @license https://www.gnu.org/licenses/gpl-3.0.txt GNU/GPLv3 11 | * @link https://github.com/msn60/oop-wordpress-plugin-boilerplate 12 | * @since 1.0.2 13 | */ 14 | 15 | namespace Plugin_Name_Name_Space\Includes\Init; 16 | 17 | use Plugin_Name_Name_Space\Includes\Interfaces\Action_Hook_Interface; 18 | 19 | if ( ! defined( 'ABSPATH' ) ) { 20 | exit; 21 | } 22 | 23 | /** 24 | * The admin-specific functionality of the plugin. 25 | * 26 | * Defines the plugin name, version, and two examples hooks for how to 27 | * enqueue the admin-specific stylesheet and JavaScript. 28 | * 29 | * @package Plugin_Name_Name_Space 30 | * @author Mehdi Soltani 31 | */ 32 | class Admin_Hook implements Action_Hook_Interface { 33 | 34 | /** 35 | * The ID of this plugin. 36 | * 37 | * @since 1.0.2 38 | * @access private 39 | * @var string $plugin_name The ID of this plugin. 40 | */ 41 | private $plugin_name; 42 | 43 | /** 44 | * The version of this plugin. 45 | * 46 | * @since 1.0.2 47 | * @access private 48 | * @var string $version The current version of this plugin. 49 | */ 50 | private $version; 51 | 52 | /** 53 | * Initialize the class and set its properties. 54 | * 55 | * @since 1.0.2 56 | * @access public 57 | * 58 | * @param string $plugin_name The name of this plugin. 59 | * @param string $version The version of this plugin. 60 | */ 61 | public function __construct( $plugin_name, $version ) { 62 | 63 | $this->plugin_name = $plugin_name; 64 | $this->version = $version; 65 | 66 | } 67 | 68 | /** 69 | * Register the stylesheets for the admin area. 70 | * 71 | * @since 1.0.2 72 | * @access public 73 | */ 74 | public function enqueue_styles() { 75 | 76 | /** 77 | * This function is provided for demonstration purposes only. 78 | * 79 | * An instance of this class should be passed to the run() function 80 | * defined in Plugin_Name_Loader as all of the hooks are defined 81 | * in that particular class. 82 | * 83 | * The Plugin_Name_Loader will then create the relationship 84 | * between the defined hooks and the functions defined in this 85 | * class. 86 | */ 87 | 88 | wp_enqueue_style( 89 | $this->plugin_name . '-admin-style', 90 | PLUGIN_NAME_ADMIN_CSS . 'plugin-name-admin-ver-' . PLUGIN_NAME_ADMIN_CSS_VERSION . '.css', 91 | array(), 92 | null, 93 | 'all' 94 | ); 95 | 96 | } 97 | 98 | /** 99 | * Register the JavaScript for the admin area. 100 | * 101 | * @since 1.0.2 102 | * @access public 103 | */ 104 | public function enqueue_scripts() { 105 | 106 | /** 107 | * This function is provided for demonstration purposes only. 108 | * 109 | * An instance of this class should be passed to the run() function 110 | * defined in Plugin_Name_Loader as all of the hooks are defined 111 | * in that particular class. 112 | * 113 | * The Plugin_Name_Loader will then create the relationship 114 | * between the defined hooks and the functions defined in this 115 | * class. 116 | */ 117 | wp_enqueue_script( 118 | $this->plugin_name . '-admin-script', 119 | PLUGIN_NAME_ADMIN_JS . 'plugin-name-admin-ver-' . PLUGIN_NAME_ADMIN_JS_VERSION . '.js', 120 | array( 'jquery' ), 121 | null, 122 | true 123 | ); 124 | } 125 | 126 | /** 127 | * Register actions that the object needs to be subscribed to. 128 | * 129 | */ 130 | public function register_add_action() { 131 | add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_styles' ) ); 132 | add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) ); 133 | } 134 | } 135 | 136 | -------------------------------------------------------------------------------- /includes/database/class-table.php: -------------------------------------------------------------------------------- 1 | 10 | * @license https://www.gnu.org/licenses/gpl-3.0.txt GNU/GPLv3 11 | * @link https://github.com/msn60/oop-wordpress-plugin-boilerplate 12 | * @since 1.0.2 13 | */ 14 | 15 | namespace Plugin_Name_Name_Space\Includes\Database; 16 | 17 | if ( ! defined( 'ABSPATH' ) ) { 18 | exit; 19 | } 20 | 21 | /** 22 | * Class Table to add new tables to your project 23 | * 24 | * If you want to add new tables to your project 25 | * (except of WordPress table), you can use from this class. 26 | * 27 | * @package Plugin_Name_Name_Space 28 | * @author Mehdi Soltani 29 | */ 30 | class Table { 31 | // TODO: This class needs to refactor based on SOLID principles 32 | /** 33 | * Define charset_collate property in Table class 34 | * 35 | * @access public 36 | * @var string $charset_collate Define charset collection for database. 37 | * @since 1.0.2 38 | */ 39 | public $charset_collate; 40 | /** 41 | * Define db_version property in Table class 42 | * 43 | * @access public 44 | * @var int $db_version Set database version for creating table. 45 | * @since 1.0.2 46 | */ 47 | public $db_version; 48 | /** 49 | * Define has_table_name property in Table class 50 | * 51 | * @access public 52 | * @var int $has_table_name To check that "Is a table exist with this name or not?". 53 | * @since 1.0.2 54 | */ 55 | public $has_table_name; 56 | /** 57 | * Define wpdb property in Table class 58 | * 59 | * @access private 60 | * @var object $wpdb It keeps global $wpdb object inside a Table instance. 61 | * @since 1.0.2 62 | */ 63 | private $wpdb; 64 | 65 | /** 66 | * Table constructor 67 | * 68 | * This constructor initial all of property for an object which is created 69 | * from Table class. 70 | * 71 | * @access public 72 | */ 73 | public function __construct( 74 | $wpdb_object, $db_version, $has_table_name 75 | ) { 76 | /** 77 | * Use from global $wpdb object. 78 | * 79 | * @global object $wpdb This is an instantiation of the wpdb class. 80 | * @see /wp-includes/wp-db.php 81 | */ 82 | $this->wpdb = $wpdb_object; 83 | $this->charset_collate = $this->wpdb->get_charset_collate(); 84 | $this->db_version = $db_version; 85 | $this->has_table_name = $has_table_name; 86 | } 87 | 88 | /** 89 | * Define create_your_table_name method in Table class 90 | * 91 | * If you want to create a table, you can use from this method. If you 92 | * need to create more than one table, you must user from several methods 93 | * like this (separated form each other). 94 | * 95 | * @since 1.0.2 96 | * @access public 97 | */ 98 | public function create_your_table_name() { 99 | $table_name = $this->wpdb->prefix . 'your_table_name_in_mysql'; 100 | if ( $this->wpdb->get_var( "show tables like '$table_name'" ) !== $table_name ) { 101 | $sql 102 | = "CREATE TABLE IF NOT EXISTS $table_name ( 103 | id INT(9) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, 104 | sample_col1 INT(9) UNSIGNED NOT NULL, 105 | sample_col2 VARCHAR(20), 106 | sample_col3 INT(9) UNSIGNED NOT NULL, 107 | sample_col4 TEXT, 108 | sample_col5 DATETIME NOT NULL, 109 | sample_col6 BOOLEAN DEFAULT FALSE, 110 | sample_col7 TINYINT UNSIGNED, 111 | sample_col8 CHAR, 112 | sample_col9 VARCHAR(30) 113 | ) $this->charset_collate;"; 114 | 115 | require_once ABSPATH . 'wp-admin/includes/upgrade.php'; 116 | dbDelta( $sql ); 117 | update_option( 'has_table_name', true ); 118 | } 119 | } 120 | } 121 | -------------------------------------------------------------------------------- /includes/abstracts/class-ajax.php: -------------------------------------------------------------------------------- 1 | 9 | * @license https://www.gnu.org/licenses/gpl-3.0.txt GNU/GPLv3 10 | * @link https://github.com/msn60/oop-wordpress-plugin-boilerplate 11 | * @since 1.0.2 12 | */ 13 | 14 | namespace Plugin_Name_Name_Space\Includes\Abstracts; 15 | 16 | use Plugin_Name_Name_Space\Includes\Interfaces\Action_Hook_Interface; 17 | 18 | if ( ! defined( 'ABSPATH' ) ) { 19 | exit; 20 | } 21 | 22 | /** 23 | * abstract Class Ajax. 24 | * This file contains an abstract class that specify how you must handle ajax requests in your theme 25 | * 26 | * @package Plugin_Name_Name_Space 27 | * @author Mehdi Soltani 28 | */ 29 | abstract class Ajax implements Action_Hook_Interface { 30 | /** 31 | * Data that need for wp_ajax_sample_ajax_call_1 32 | * 33 | * @since 1.0.2 34 | * @access protected 35 | * @var array $ajax_data1 array of ata that need for wp_ajax_sample_ajax_call. 36 | */ 37 | /** 38 | * @var string $ajax_url Use to identify admin-ajax.php. 39 | */ 40 | protected $ajax_url; 41 | /** 42 | * @var string Action name for wp_create_nonce. 43 | */ 44 | protected $ajax_nonce; 45 | /** 46 | * @var string action name for ajax call 47 | */ 48 | protected $action; 49 | 50 | /** 51 | * Main constructor. 52 | * This is constructor of Ajax abstract class 53 | * 54 | * @access public 55 | * @since 1.0.2 56 | * 57 | * @param string $action Action name for ajax call 58 | */ 59 | public function __construct( $action ) { 60 | $this->ajax_url = admin_url( 'admin-ajax.php' ); 61 | $this->ajax_nonce = wp_create_nonce( 'sample_ajax_nonce' ); 62 | $this->action = $action; 63 | 64 | } 65 | 66 | /** 67 | * Method to define add_action for using in theme or plugin 68 | * 69 | * @access public 70 | * @since 1.0.2 71 | * 72 | */ 73 | public function register_add_action() { 74 | add_action( 'wp_enqueue_scripts', array( $this, 'register_script' ), 10 ); 75 | //hook to add your ajax request 76 | add_action( 'wp_ajax_' . $this->action, [ $this, 'handle' ] ); 77 | add_action( 'wp_ajax_nopriv_' . $this->action, [ $this, 'handle' ] ); 78 | } 79 | 80 | 81 | /** 82 | * Method to register script and localize it 83 | * 84 | * @access public 85 | * @since 1.0.2 86 | * 87 | */ 88 | public function register_script() { 89 | //only use when you u 90 | /*wp_enqueue_script( 91 | MSN_THEME_NAME . '-script', 92 | THEME_NAME_JS . 'theme-name-ver-' . THEME_NAME_JS_VERSION . '.js', 93 | array( 'jquery' ), 94 | null, 95 | true 96 | );*/ 97 | /* 98 | * localize script to handle ajax call 99 | * */ 100 | wp_localize_script( MSN_THEME_NAME . '-script', 'data', $this->sending_ajax_data() ); 101 | // TODO: customize it for ajax in plugin not theme 102 | } 103 | 104 | 105 | /** 106 | * Method to prepare primary values to send from PHP to Javascript file 107 | * 108 | * This method prepares data for wp_localize_script 109 | * 110 | * @access public 111 | * @since 1.0.2 112 | * 113 | */ 114 | public function sending_ajax_data() { 115 | $initial_value = [ 116 | 'ajax_url' => $this->ajax_url, 117 | 'ajax_nonce' => $this->ajax_nonce, 118 | 'msn_ajax_sample' => 'Ajax sample for OOP theme starter', 119 | 'msn_ajax_sample2' => 'Ajax sample for OOP theme starter', 120 | ]; 121 | 122 | return $initial_value; 123 | } 124 | 125 | /* 126 | * Handle method for ajax request in back-end 127 | * */ 128 | abstract public function handle(); 129 | 130 | /** 131 | * Sends a JSON response with the details of the given error. 132 | * 133 | * @param \WP_Error $error 134 | */ 135 | private function send_error( \WP_Error $error ) { 136 | wp_send_json( array( 137 | 'code' => $error->get_error_code(), 138 | 'message' => $error->get_error_message() 139 | ) ); 140 | } 141 | 142 | 143 | } 144 | -------------------------------------------------------------------------------- /includes/init/class-public-hook.php: -------------------------------------------------------------------------------- 1 | 10 | * @license https://www.gnu.org/licenses/gpl-3.0.txt GNU/GPLv3 11 | * @link https://github.com/msn60/oop-wordpress-plugin-boilerplate 12 | * @since 1.0.2 13 | */ 14 | 15 | namespace Plugin_Name_Name_Space\Includes\Init; 16 | 17 | use Plugin_Name_Name_Space\Includes\Interfaces\Action_Hook_Interface; 18 | 19 | if ( ! defined( 'ABSPATH' ) ) { 20 | exit; 21 | } 22 | 23 | /** 24 | * The public-facing functionality of the plugin. 25 | * 26 | * Defines the plugin name, version, and two examples hooks for how to 27 | * enqueue the public-facing stylesheet and JavaScript. 28 | * 29 | * @package Plugin_Name_Name_Space 30 | * @author Mehdi Soltani 31 | */ 32 | class Public_Hook implements Action_Hook_Interface { 33 | 34 | /** 35 | * The ID of this plugin. 36 | * 37 | * @since 1.0.2 38 | * @access private 39 | * @var string $plugin_name The ID of this plugin. 40 | */ 41 | private $plugin_name; 42 | 43 | /** 44 | * The version of this plugin. 45 | * 46 | * @since 1.0.2 47 | * @access private 48 | * @var string $version The current version of this plugin. 49 | */ 50 | private $plugin_version; 51 | 52 | /** 53 | * Initialize the class and set its properties. 54 | * 55 | * @since 1.0.2 56 | * @access public 57 | * 58 | * @param string $plugin_name The name of the plugin. 59 | * @param string $plugin_version The version of this plugin. 60 | */ 61 | public function __construct( $plugin_name, $plugin_version ) { 62 | 63 | $this->plugin_name = $plugin_name; 64 | $this->plugin_version = $plugin_version; 65 | 66 | } 67 | 68 | /** 69 | * Register the stylesheets for the public-facing side of the site. 70 | * 71 | * @since 1.0.2 72 | * @access public 73 | */ 74 | public function enqueue_styles() { 75 | 76 | /** 77 | * This function is provided for demonstration purposes only. 78 | * 79 | * An instance of this class should be passed to the run() function 80 | * defined in Plugin_Name_Loader as all of the hooks are defined 81 | * in that particular class. 82 | * 83 | * The Plugin_Name_Loader will then create the relationship 84 | * between the defined hooks and the functions defined in this 85 | * class. 86 | */ 87 | 88 | wp_enqueue_style( 89 | $this->plugin_name . '-public-style', 90 | PLUGIN_NAME_CSS . 'plugin-name-public-ver-' . PLUGIN_NAME_CSS_VERSION . '.css', 91 | array(), 92 | null, 93 | 'all' 94 | ); 95 | } 96 | 97 | /** 98 | * Register the JavaScript for the public-facing side of the site. 99 | * 100 | * @since 1.0.2 101 | * @access public 102 | */ 103 | public function enqueue_scripts() { 104 | 105 | /** 106 | * This function is provided for demonstration purposes only. 107 | * 108 | * An instance of this class should be passed to the run() function 109 | * defined in Plugin_Name_Loader as all of the hooks are defined 110 | * in that particular class. 111 | * 112 | * The Plugin_Name_Loader will then create the relationship 113 | * between the defined hooks and the functions defined in this 114 | * class. 115 | */ 116 | 117 | wp_enqueue_script( 118 | $this->plugin_name . '-public-script', 119 | PLUGIN_NAME_JS . 'plugin-name-public-ver-' . PLUGIN_NAME_JS_VERSION . '.js', 120 | array( 'jquery' ), 121 | null, 122 | true 123 | ); 124 | } 125 | 126 | /** 127 | * Register actions that the object needs to be subscribed to. 128 | * 129 | */ 130 | public function register_add_action() { 131 | $this->set_enqueue_scripts_action(); 132 | } 133 | 134 | /** 135 | * Register enqueue scripts action 136 | */ 137 | public function set_enqueue_scripts_action() { 138 | add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_styles' ) ); 139 | add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) ); 140 | } 141 | } 142 | 143 | -------------------------------------------------------------------------------- /includes/abstracts/class-shortcode.php: -------------------------------------------------------------------------------- 1 | 10 | * @license https://www.gnu.org/licenses/gpl-3.0.txt GNU/GPLv3 11 | * @link https://github.com/msn60/oop-wordpress-plugin-boilerplate 12 | * @since 1.0.2 13 | */ 14 | 15 | namespace Plugin_Name_Name_Space\Includes\Abstracts; 16 | 17 | 18 | use Plugin_Name_Name_Space\Includes\Interfaces\Action_Hook_Interface; 19 | 20 | if ( ! defined( 'ABSPATH' ) ) { 21 | exit; 22 | } 23 | 24 | /** 25 | * Shortcode abstract Class File 26 | * 27 | * This file contains contract for Shortcode class. 28 | * If you want to create a shortcode, you must use from this contract. 29 | * 30 | * @package Plugin_Name_Name_Space 31 | * @author Mehdi Soltani 32 | * @link https://github.com/msn60/oop-wordpress-plugin-boilerplate 33 | * 34 | * @see https://developer.wordpress.org/plugins/shortcodes/shortcodes-with-parameters/ 35 | * @see https://code.tutsplus.com/articles/create-wordpress-plugins-with-oop-techniques--net-20153 36 | * @see https://www.smashingmagazine.com/2012/05/wordpress-shortcodes-complete-guide/ 37 | * @see https://speckyboy.com/getting-started-with-wordpress-shortcodes-examples/ 38 | * @see https://wpshout.com/how-to-create-wordpress-shortcodes/ 39 | * @see https://codex.wordpress.org/Shortcode_API 40 | * @see https://en.support.wordpress.com/shortcodes/ 41 | * @see https://kinsta.com/blog/wordpress-shortcodes/ 42 | */ 43 | abstract class Shortcode implements Action_Hook_Interface { 44 | 45 | /** 46 | * The name of the [$tag] 47 | * 48 | * @access protected 49 | * @var string $tag The name of the [$tag] (i.e. the name of the shortcode) 50 | */ 51 | protected $tag; 52 | /** 53 | * [$tag] attributes 54 | * 55 | * @access protected 56 | * @var array $atts [$tag] attributes 57 | */ 58 | protected $atts; 59 | /** 60 | * [$tag] default attributes value 61 | * 62 | * @access protected 63 | * @var array $default_atts [$tag] default attributes value 64 | */ 65 | protected $default_atts; 66 | /** 67 | * Content inside of [$tag] 68 | * 69 | * @access protected 70 | * @var string $content Content inside of [$tag] 71 | */ 72 | protected $content; 73 | 74 | /** 75 | * Admin_Menu constructor. 76 | * This constructor gets initial values to send to add_menu_page function to 77 | * create admin menu. 78 | * 79 | * @access public 80 | * 81 | * @param array $initial_value Initial value to pass to add_menu_page function. 82 | */ 83 | public function __construct( array $initial_values ) { 84 | $this->tag = $initial_values['tag']; 85 | $this->default_atts = $initial_values['default_atts']; 86 | $this->atts = []; 87 | $this->content = null; 88 | } 89 | 90 | /** 91 | * call 'init' add_action to register add_shortcut in correct place 92 | * 93 | * @access public 94 | */ 95 | public function register_add_action() { 96 | add_action( 'init', array( $this, 'register_shortcode' ) ); 97 | } 98 | 99 | /** 100 | * Method register_shortcode to call add_shortcode function 101 | * 102 | * @access public 103 | */ 104 | public function register_shortcode() { 105 | add_shortcode( $this->tag, array( $this, 'define_shortcode_handler' ) ); 106 | } 107 | 108 | 109 | /** 110 | * Abstract Method define_shortcode in Shortcode Class 111 | * 112 | * For each each defined shortcode, you must define callable function 113 | * for that. This method has this role as a shortcode callable function 114 | * 115 | * @access public 116 | * 117 | * @param array $atts attributes which can pass throw shortcode in front end 118 | * @param string $content The content between starting and closing shortcode tag 119 | * @param string $tag The name of the shortcode tag 120 | */ 121 | abstract public function define_shortcode_handler( $atts = [], $content = null, $tag = '' ); 122 | 123 | } 124 | -------------------------------------------------------------------------------- /includes/abstracts/class-custom-post-type.php: -------------------------------------------------------------------------------- 1 | 10 | * @license https://www.gnu.org/licenses/gpl-3.0.txt GNU/GPLv3 11 | * @link https://github.com/msn60/oop-wordpress-plugin-boilerplate 12 | * @since 1.0.2 13 | */ 14 | 15 | namespace Plugin_Name_Name_Space\Includes\Abstracts; 16 | 17 | use Plugin_Name_Name_Space\Includes\Interfaces\Action_Hook_Interface; 18 | 19 | if ( ! defined( 'ABSPATH' ) ) { 20 | exit; 21 | } 22 | 23 | /** 24 | * Class Custom_Post_Type. 25 | * This file contains contract for Custom_Post_Type class. If you want create a 26 | * custom post type in WordPress, you must to use this contract. 27 | * 28 | * @package Plugin_Name_Name_Space 29 | * @author Mehdi Soltani 30 | * @link https://github.com/msn60/oop-wordpress-plugin-boilerplate 31 | * 32 | * @see https://carlalexander.ca/saving-wordpress-custom-post-types-using-interface/ 33 | * @see https://developer.wordpress.org/reference/functions/register_post_type/ 34 | * @see https://carlalexander.ca/designing-entities-wordpress-custom-post-types/ 35 | * @see https://www.hostinger.com/tutorials/wordpress-custom-post-types 36 | */ 37 | abstract class Custom_Post_Type implements Action_Hook_Interface { 38 | 39 | /** 40 | * Post type key. 41 | * Must not exceed 20 characters and may only contain lowercase alphanumeric characters, dashes, and underscores. See sanitize_key(). 42 | * 43 | * @access protected 44 | * @var string $post_type The key of post type 45 | * @since 1.0.2 46 | */ 47 | protected $post_type; 48 | /** 49 | * Array or string of arguments for registering a post type. 50 | * 51 | * @access protected 52 | * @var array | string $args Array or string of arguments for registering a post type. 53 | * @since 1.0.2 54 | */ 55 | protected $args; 56 | 57 | /** 58 | * Custom_Post_Type constructor. 59 | * This constructor gets initial values to send to add_menu_page function to 60 | * create admin menu. 61 | * 62 | * @access public 63 | * 64 | * @param array $initial_value Initial value to pass to add_menu_page function. 65 | */ 66 | public function __construct( array $initial_values ) { 67 | $this->post_type = $initial_values['post_type']; 68 | $this->args = $initial_values['args']; 69 | } 70 | 71 | /** 72 | * Method to register custom post type 73 | * 74 | * Inside this method, we call register_post_type to create custom post type 75 | * 76 | * @access public 77 | * @see https://developer.wordpress.org/reference/functions/register_post_type/ 78 | */ 79 | public function add_custom_post_type() { 80 | register_post_type( $this->post_type, $this->args ); 81 | } 82 | 83 | /** 84 | * call 'init' add_action to create custom post type 85 | * 86 | * @access public 87 | */ 88 | public function register_add_action() { 89 | add_action( 'init', array( $this, 'add_custom_post_type' ) ); 90 | //add_filter( 'manage_edit-'.$this->post_type.'_columns', array($this, 'set_columns'), 10, 1) ; 91 | } 92 | 93 | /** 94 | * Insert or update a custom post type. 95 | * 96 | * @param bool $wp_error 97 | * 98 | * @return int| \WP_Error 99 | */ 100 | public function insert_custom_post( $wp_error = false ) { 101 | $post_id = wp_insert_post( $this->get_post_data(), $wp_error ); 102 | 103 | if ( 0 === $post_id || $post_id instanceof \WP_Error ) { 104 | return $post_id; 105 | } 106 | 107 | foreach ( $this->get_post_meta() as $key => $value ) { 108 | update_post_meta( $post_id, $key, $value ); 109 | } 110 | 111 | return $post_id; 112 | } 113 | 114 | 115 | /** 116 | * Get the post data as a wp_insert_post compatible array. 117 | * 118 | * @access public 119 | * @return array 120 | */ 121 | abstract public function get_post_data(); 122 | 123 | /** 124 | * Get all the post meta as a key-value associative array. 125 | * 126 | * @access public 127 | * @return array 128 | */ 129 | abstract public function get_post_meta(); 130 | 131 | } 132 | -------------------------------------------------------------------------------- /includes/config/class-info.php: -------------------------------------------------------------------------------- 1 | 10 | * @license https://www.gnu.org/licenses/gpl-3.0.txt GNU/GPLv3 11 | * @link https://github.com/msn60/oop-wordpress-plugin-boilerplate 12 | * @since 1.0.2 13 | */ 14 | 15 | namespace Plugin_Name_Name_Space\Includes\Config; 16 | 17 | if ( ! defined( 'ABSPATH' ) ) { 18 | exit; 19 | } 20 | 21 | /** 22 | * Class Info 23 | * If you want to add default settings or some value for options 24 | * for your plugin (when it's activated) you can use from this class. 25 | * 26 | * @package Plugin_Name_Name_Space 27 | * @author Mehdi Soltani 28 | */ 29 | class Info { 30 | 31 | /** 32 | * Define plugin_setting_option1 property in Info class 33 | * 34 | * @access public 35 | * @var string $plugin_setting_option1 Define plugin setting option1 for your plugin. 36 | * @since 1.0.2 37 | */ 38 | public $plugin_setting_option1; 39 | /** 40 | * Define plugin_setting_option2 property in Info class 41 | * 42 | * @access public 43 | * @var string $plugin_setting_option2 Define plugin setting option2 for your plugin. 44 | * @since 1.0.2 45 | */ 46 | public $plugin_setting_option2; 47 | /** 48 | * Define plugin_setting_option3 property in Info class 49 | * 50 | * @access public 51 | * @var string $plugin_setting_option3 Define plugin setting option1 for your plugin. 52 | * @since 1.0.2 53 | */ 54 | public $plugin_setting_option3; 55 | /** 56 | * Define plugin_setting_option4 property in Info class 57 | * 58 | * @access public 59 | * @var string $plugin_setting_option4 Define plugin setting option1 for your plugin. 60 | * @since 1.0.2 61 | */ 62 | public $plugin_setting_option4; 63 | 64 | /** 65 | * Info constructor. 66 | * This is used to get option values from options table and instantiate 67 | * object property which is created from Info class. 68 | * 69 | * @access public 70 | */ 71 | public function __construct() { 72 | $this->plugin_setting_option1 73 | = get_option( 'plugin_name_prefix_plugin_setting_option1' ); 74 | $this->plugin_setting_option2 75 | = get_option( 'plugin_name_prefix_plugin_setting_option2' ); 76 | $this->plugin_setting_option3 77 | = get_option( 'plugin_name_prefix_plugin_setting_option3' ); 78 | $this->plugin_setting_option4 79 | = get_option( 'plugin_name_prefix_plugin_setting_option4' ); 80 | } 81 | 82 | /** 83 | * Add default values for key options in plugin activation. 84 | * This method will run when plugin is activated and assign default values 85 | * for your key options in plugin. 86 | * 87 | * @access public 88 | * @static 89 | */ 90 | public static function add_info_in_plugin_activation() { 91 | if ( ! get_option( 'plugin_name_prefix_plugin_setting_option1' ) ) { 92 | update_option( 93 | 'plugin_name_prefix_plugin_setting_option1', 94 | 'Initial value for option 1' 95 | ); 96 | } 97 | 98 | if ( ! get_option( 'plugin_name_prefix_plugin_setting_option2' ) ) { 99 | update_option( 100 | 'plugin_name_prefix_plugin_setting_option2', 101 | 'Initial value for option 2' 102 | ); 103 | } 104 | 105 | if ( ! get_option( 'plugin_name_prefix_plugin_setting_option3' ) ) { 106 | update_option( 107 | 'plugin_name_prefix_plugin_setting_option3', 108 | 'Initial value for option 3' 109 | ); 110 | } 111 | if ( ! get_option( 'plugin_name_prefix_plugin_setting_option4' ) ) { 112 | update_option( 113 | 'plugin_name_prefix_plugin_setting_option4', 114 | 'Initial value for option 4' 115 | ); 116 | } 117 | } 118 | 119 | /** 120 | * Update option values for plugin settings. 121 | * With this method, you can update you key options with new values. You 122 | * can use from several method to achieve this goal. 123 | * 124 | * @access public 125 | */ 126 | public function update_some_info() { 127 | update_option( 128 | 'plugin_name_prefix_plugin_setting_option1', 129 | $this->plugin_setting_option1 130 | ); 131 | update_option( 132 | 'plugin_name_prefix_plugin_setting_option2', 133 | $this->plugin_setting_option2 134 | ); 135 | update_option( 136 | 'plugin_name_prefix_plugin_setting_option3', 137 | $this->plugin_setting_option3 138 | ); 139 | update_option( 140 | 'plugin_name_prefix_plugin_setting_option4', 141 | $this->plugin_setting_option4 142 | ); 143 | } 144 | 145 | 146 | } 147 | 148 | -------------------------------------------------------------------------------- /templates/admin/plugin-page/second-section.php: -------------------------------------------------------------------------------- 1 |
2 |

Header Samples in Msn Plugin

3 |

Header Samples in Msn Plugin

4 |

Header Samples in Msn Plugin

5 |

Header Samples in Msn Plugin

6 |
Header Samples in Msn Plugins
7 |
Header Samples in Msn Plugin
8 |
9 | 10 |

11 | 12 | 14 |

15 |

16 | 17 | 19 |

20 | 21 |
22 | Search 23 | Search Primary 24 | Search Secondary 25 |
26 | 27 |

My Plugin

28 |
29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 69 | 70 |
41 | 45 |
54 | Male 55 | Female 56 |
64 | 66 | 68 |
71 |
72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 |
NameFavorite Holiday
NameFavorite Holiday
Brad WilliamsHalloween
Ozh RichardTalk Like a Pirate
Justin TadlockChristmas
101 | 102 |
103 |
104 |
Displaying 1-20 of 69
105 | 1 106 | 2 107 | 3 108 | 4 109 | 110 | 111 |
112 |
113 | 114 |
115 |
116 | Displaying 1-20 of 69 117 | 1 118 | 2 119 | 3 120 | 4 121 | 122 |
123 |
124 | 125 |
-------------------------------------------------------------------------------- /includes/init/class-router.php: -------------------------------------------------------------------------------- 1 | 10 | * @license https://www.gnu.org/licenses/gpl-3.0.txt GNU/GPLv3 11 | * @link https://github.com/msn60/oop-wordpress-plugin-boilerplate 12 | * @since 1.0.2 13 | */ 14 | 15 | namespace Plugin_Name_Name_Space\Includes\Init; 16 | 17 | if ( ! defined( 'ABSPATH' ) ) { 18 | exit; 19 | } 20 | 21 | use Plugin_Name_Name_Space\Includes\Interfaces\Action_Hook_Interface; 22 | use Plugin_Name_Name_Space\Includes\PageHandlers\{ 23 | Contracts\Page_Handler, Second_Page_Handler, First_Page_Handler 24 | }; 25 | 26 | /** 27 | * Class Router. 28 | * This class use to handle different routes in your project 29 | * 30 | * @package Plugin_Name_Name_Space 31 | * @author Mehdi Soltani 32 | * @see \Plugin_Name_Name_Space\Includes\PageHandlers\First_Page_Handler 33 | * @see \Plugin_Name_Name_Space\Includes\PageHandlers\Second_Page_Handler 34 | */ 35 | class Router implements Action_Hook_Interface { 36 | 37 | /** 38 | * To keep routes for your plugin 39 | * 40 | * @access private 41 | * @var array $routes you can save all of routes in your project in this array. 42 | * @since 1.0.2 43 | */ 44 | private $routes; 45 | 46 | /** 47 | * Router constructor. 48 | * This constructor set default routes for your plugin. 49 | * 50 | * @access public 51 | */ 52 | public function __construct() { 53 | $this->set_default_routes(); 54 | } 55 | 56 | /** 57 | * Method set_default_routes in Router Class 58 | * 59 | * Inside this method, you can set your routes and initialize $routes variable for your object. 60 | * 61 | * @access private 62 | */ 63 | private function set_default_routes() { 64 | $this->routes = [ 65 | '/url1/url2/' => Second_Page_Handler::class, 66 | '/first-sample-url/' => First_Page_Handler::class, 67 | ]; 68 | 69 | } 70 | 71 | /** 72 | * Method check_routes in Router Class 73 | * 74 | * This is primary method in Router class that is called by Core class. First, this method get actual 75 | * url that client requests for it. Second, check that is in routes array or not. Third, if it's in 76 | * routes, it creates an instance from its handler class and then invoke render method to create 77 | * desire page for user. 78 | * 79 | * @since 1.0.2 80 | * @access public 81 | * 82 | * @global object $wpdb It contains a set of functions used to interact with a database. 83 | * @global object $wp_roles An object of WP_Roles class that is used to implement a user roles API. 84 | */ 85 | public function check_routes() { 86 | $current_route = $this->get_current_route(); 87 | if ( in_array( $current_route, $this->get_routes_keys(), true ) ) { 88 | $handler = $this->get_route_handler( $current_route ); 89 | global $wpdb, $wp_roles; 90 | $send_error_message = array(); 91 | /** 92 | * @var Page_Handler $handler_instance 93 | */ 94 | $handler_instance = new $handler(); 95 | $handler_instance->render(); 96 | } 97 | } 98 | 99 | /** 100 | * Method get_current_route in Router Class 101 | * 102 | * This method return actual url that is requested by user (without query string in the end of url 103 | * and also site url in the beginning) 104 | * 105 | * @access private 106 | * @return string Actual url that user requests it. 107 | */ 108 | private function get_current_route() { 109 | $actual_link = ( 'on' === isset( $_SERVER['HTTPS'] ) && $_SERVER['HTTPS'] ? 'https' : 'http' ) 110 | . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; 111 | $site_url = get_site_url(); 112 | $temp_url = str_replace( $site_url, '', $actual_link ); 113 | 114 | return strtok( $temp_url, '?' ); 115 | } 116 | 117 | /** 118 | * Method get_routes_keys in Router Class 119 | * 120 | * This method returns an array of defined routes. 121 | * 122 | * @since 1.0.2 123 | * @access private 124 | * 125 | * @return array Return routes as an array from key values of $routes property. 126 | */ 127 | private function get_routes_keys() { 128 | return array_keys( $this->routes ); 129 | } 130 | 131 | /** 132 | * Method get_route_handler in Router Class 133 | * 134 | * This method gets the name of class handler that is related to its route value. 135 | * 136 | * @since 1.0.2 137 | * @access private 138 | * 139 | * @param string $route Name of route that you need related class handler for it. 140 | * 141 | * @return string It returns class handler for route which is passed to this method. 142 | */ 143 | private function get_route_handler( $route ) { 144 | return $this->routes[ $route ]; 145 | } 146 | 147 | /** 148 | * Register actions that the object needs to be subscribed to. 149 | * 150 | */ 151 | public function register_add_action() { 152 | add_action( 'init', array( $this, 'check_routes' ) ); 153 | } 154 | } 155 | -------------------------------------------------------------------------------- /includes/abstracts/class-admin-sub-menu.php: -------------------------------------------------------------------------------- 1 | 10 | * @license https://www.gnu.org/licenses/gpl-3.0.txt GNU/GPLv3 11 | * @link https://github.com/msn60/oop-wordpress-plugin-boilerplate 12 | * @since 1.0.2 13 | */ 14 | 15 | namespace Plugin_Name_Name_Space\Includes\Abstracts; 16 | 17 | use Plugin_Name_Name_Space\Includes\Interfaces\Action_Hook_Interface; 18 | 19 | if ( ! defined( 'ABSPATH' ) ) { 20 | exit; 21 | } 22 | 23 | /** 24 | * Abstract Class Admin_Sub_Menu. 25 | * If you want create an sub menu page under an admin page 26 | * (inside Admin panel of WordPress), you must use this contract. 27 | * 28 | * @package Plugin_Name_Name_Space 29 | * @author Mehdi Soltani 30 | * @see wp-admin/includes/plugin.php 31 | * @see https://developer.wordpress.org/reference/functions/add_submenu_page/ 32 | */ 33 | abstract class Admin_Sub_Menu implements Action_Hook_Interface{ 34 | 35 | /** 36 | * Define parent_slug property in Admin_Sub_Menu class. 37 | * This property use to pass to add_submenu_page as an argument. 38 | * 39 | * @access protected 40 | * @var string $parent_slug The slug name for the parent menu. 41 | * @since 1.0.2 42 | */ 43 | protected $parent_slug; 44 | /** 45 | * Define page_title property in Admin_Sub_Menu class. 46 | * This property use to pass to add_submenu_page as an argument. 47 | * 48 | * @access protected 49 | * @var string $page_title The text to be displayed in the title tags of the page when the menu is selected. 50 | * @since 1.0.2 51 | */ 52 | protected $page_title; 53 | /** 54 | * Define menu_title property in Admin_Sub_Menu class. 55 | * This property use to pass to add_submenu_page as an argument. 56 | * 57 | * @access protected 58 | * @var string $menu_title The text to be used for the menu. 59 | * @since 1.0.2 60 | */ 61 | protected $menu_title; 62 | /** 63 | * Define capability property in Admin_Sub_Menu class. 64 | * This property use to pass to add_submenu_page as an argument. 65 | * 66 | * @access protected 67 | * @var string $capability he capability required for this menu to be displayed to the user. 68 | * @since 1.0.2 69 | */ 70 | protected $capability; 71 | /** 72 | * Define menu_slug property in Admin_Sub_Menu class. 73 | * This property use to pass to add_submenu_page as an argument. 74 | * 75 | * @access protected 76 | * @var string $menu_slug The slug name to refer to this menu by. 77 | * @since 1.0.2 78 | */ 79 | protected $menu_slug; 80 | /** 81 | * Define callable_function property in Admin_Sub_Menu class. 82 | * This property use to pass to add_submenu_page as an argument. 83 | * 84 | * @access protected 85 | * @var callable $callable_function The function to be called to output the content for this page. 86 | * @since 1.0.2 87 | */ 88 | protected $callable_function; 89 | 90 | /** 91 | * Admin_Sub_Menu constructor. 92 | * This constructor gets initial values to send to add_submenu_page function to 93 | * create admin submenu. 94 | * 95 | * @access public 96 | * 97 | * @param array $initial_value Initial value to pass to add_submenu_page function. 98 | */ 99 | public function __construct( array $initial_value ) { 100 | 101 | $this->parent_slug = $initial_value['parent-slug']; 102 | $this->page_title = $initial_value['page_title']; 103 | $this->menu_title = $initial_value['menu_title']; 104 | $this->capability = $initial_value['capability']; 105 | $this->menu_slug = $initial_value['menu_slug']; 106 | $this->callable_function = $initial_value['callable_function']; 107 | 108 | } 109 | 110 | /** 111 | * Method add_admin_sub_menu_page in Admin_Menu Class 112 | * 113 | * Inside this method, we call add_submenu_page function to create admin menu 114 | * page in WordPress Admin Panel. 115 | * 116 | * @access public 117 | */ 118 | public function add_admin_sub_menu_page() { 119 | add_submenu_page( 120 | $this->parent_slug, 121 | $this->page_title, 122 | $this->menu_title, 123 | $this->capability, 124 | $this->menu_slug, 125 | array( $this, 'render_sub_menu_panel' ) 126 | ); 127 | 128 | } 129 | 130 | 131 | /** 132 | * call 'admin_menu' add_action to create Admin submenu page 133 | * 134 | * @access public 135 | */ 136 | public function register_add_action() { 137 | add_action( 'admin_menu', array( $this, 'add_admin_sub_menu_page' ) ); 138 | } 139 | 140 | 141 | /** 142 | * Method render_sub_menu_panel in Admin_Sub_Menu Class 143 | * 144 | * For each admin submenu page, we must have callable function that render and 145 | * handle this menu page. For each menu page, you must implement this method. 146 | * 147 | * @access public 148 | */ 149 | abstract public function render_sub_menu_panel(); 150 | 151 | } 152 | -------------------------------------------------------------------------------- /templates/admin/options-page/option-page1.php: -------------------------------------------------------------------------------- 1 | 2 | 3 |
4 |

Complete Settings Page By MSN

5 |
6 | '; 8 | 9 | foreach ( $params as $tab ) { 10 | $html .= sprintf( '%2$s', $tab['id'], $tab['header_title'] ); 11 | } 12 | 13 | $html .= ''; 14 | 15 | echo $html; 16 | ?> 17 | 18 | 19 |
20 | 21 | 22 |
23 |
24 | 29 |
30 |
31 | 32 |
33 |
34 |
35 | 36 | 37 | 137 | 138 | -------------------------------------------------------------------------------- /includes/init/class-loader.php: -------------------------------------------------------------------------------- 1 | 9 | * @license https://www.gnu.org/licenses/gpl-3.0.txt GNU/GPLv3 10 | * @link https://github.com/msn60/oop-wordpress-plugin-boilerplate 11 | * @since 1.0.2 12 | */ 13 | 14 | namespace Plugin_Name_Name_Space\Includes\Init; 15 | 16 | if ( ! defined( 'ABSPATH' ) ) { 17 | exit; 18 | } 19 | 20 | /** 21 | * Register all actions and filters for the plugin. 22 | * 23 | * Maintain a list of all hooks that are registered throughout 24 | * the plugin, and register them with the WordPress API. Call the 25 | * run function to execute the list of actions and filters. 26 | * 27 | * @package Plugin_Name_Name_Space 28 | * @author Mehdi Soltani 29 | */ 30 | class Loader { 31 | 32 | /** 33 | * The array of actions registered with WordPress. 34 | * 35 | * @since 1.0.2 36 | * @access protected 37 | * @var array $actions The actions registered with WordPress to fire when the plugin loads. 38 | */ 39 | protected $actions; 40 | 41 | /** 42 | * The array of filters registered with WordPress. 43 | * 44 | * @since 1.0.2 45 | * @access protected 46 | * @var array $filters The filters registered with WordPress to fire when the plugin loads. 47 | */ 48 | protected $filters; 49 | 50 | /** 51 | * Initialize the collections used to maintain the actions and filters. 52 | * 53 | * @since 1.0.2 54 | * @access public 55 | */ 56 | public function __construct() { 57 | 58 | $this->actions = array(); 59 | $this->filters = array(); 60 | 61 | } 62 | 63 | /** 64 | * Add a new action to the collection to be registered with WordPress. 65 | * 66 | * @since 1.0.2 67 | * 68 | * @param string $hook The name of the WordPress action that is being registered. 69 | * @param object $component A reference to the instance of the object on which the action is defined. 70 | * @param string $callback The name of the function definition on the $component. 71 | * @param int $priority Optional. The priority at which the function should be fired. Default is 10. 72 | * @param int $accepted_args Optional. The number of arguments that should be passed to the $callback. Default is 1. 73 | */ 74 | public function add_action( $hook, $component, $callback, $priority = 10, $accepted_args = 1 ) { 75 | $this->actions = $this->add( $this->actions, $hook, $component, $callback, $priority, $accepted_args ); 76 | } 77 | 78 | /** 79 | * A utility function that is used to register the actions and hooks into a single 80 | * collection. 81 | * 82 | * @since 1.0.2 83 | * @access private 84 | * 85 | * @param array $hooks The collection of hooks that is being registered (that is, actions or filters). 86 | * @param string $hook The name of the WordPress filter that is being registered. 87 | * @param object $component A reference to the instance of the object on which the filter is defined. 88 | * @param string $callback The name of the function definition on the $component. 89 | * @param int $priority The priority at which the function should be fired. 90 | * @param int $accepted_args The number of arguments that should be passed to the $callback. 91 | * 92 | * @return array The collection of actions and filters registered with WordPress. 93 | */ 94 | private function add( $hooks, $hook, $component, $callback, $priority, $accepted_args ) { 95 | 96 | $hooks[] = array( 97 | 'hook' => $hook, 98 | 'component' => $component, 99 | 'callback' => $callback, 100 | 'priority' => $priority, 101 | 'accepted_args' => $accepted_args, 102 | ); 103 | 104 | return $hooks; 105 | 106 | } 107 | 108 | /** 109 | * Add a new filter to the collection to be registered with WordPress. 110 | * 111 | * @since 1.0.2 112 | * 113 | * @param string $hook The name of the WordPress filter that is being registered. 114 | * @param object $component A reference to the instance of the object on which the filter is defined. 115 | * @param string $callback The name of the function definition on the $component. 116 | * @param int $priority Optional. The priority at which the function should be fired. Default is 10. 117 | * @param int $accepted_args Optional. The number of arguments that should be passed to the $callback. Default is 1. 118 | */ 119 | public function add_filter( $hook, $component, $callback, $priority = 10, $accepted_args = 1 ) { 120 | $this->filters = $this->add( $this->filters, $hook, $component, $callback, $priority, $accepted_args ); 121 | } 122 | 123 | /** 124 | * Register the filters and actions with WordPress. 125 | * 126 | * @since 1.0.2 127 | */ 128 | public function run() { 129 | 130 | foreach ( $this->filters as $hook ) { 131 | add_filter( $hook['hook'], array( $hook['component'], $hook['callback'] ), $hook['priority'], $hook['accepted_args'] ); 132 | } 133 | 134 | foreach ( $this->actions as $hook ) { 135 | add_action( $hook['hook'], array( $hook['component'], $hook['callback'] ), $hook['priority'], $hook['accepted_args'] ); 136 | } 137 | 138 | } 139 | } 140 | 141 | -------------------------------------------------------------------------------- /includes/abstracts/class-option-menu.php: -------------------------------------------------------------------------------- 1 | 10 | * @license https://www.gnu.org/licenses/gpl-3.0.txt GNU/GPLv3 11 | * @link https://github.com/msn60/oop-wordpress-plugin-boilerplate 12 | * @since 1.0.2 13 | */ 14 | 15 | namespace Plugin_Name_Name_Space\Includes\Abstracts; 16 | 17 | use Plugin_Name_Name_Space\Includes\Functions\Template_Builder; 18 | use Plugin_Name_Name_Space\Includes\Interfaces\Action_Hook_Interface; 19 | use Plugin_Name_Name_Space\Includes\Interfaces\Action_Hook_With_Args_Interface; 20 | 21 | if ( ! defined( 'ABSPATH' ) ) { 22 | exit; 23 | } 24 | 25 | /** 26 | * Class Option_Menu. 27 | * If you want create an option page inside setting section of WordPress, 28 | * you can use from this class. 29 | * 30 | * @package Plugin_Name_Name_Space 31 | * @author Mehdi Soltani 32 | * @link https://github.com/msn60/oop-wordpress-plugin-boilerplate 33 | * 34 | * @see wp-admin/includes/plugin.php 35 | * @see https://developer.wordpress.org/reference/functions/add_menu_page/ 36 | */ 37 | abstract class Option_Menu implements Action_Hook_With_Args_Interface { 38 | use Template_Builder; 39 | /** 40 | * Define page_title property in Option_Menu class. 41 | * This property use to pass to add_options_page as an argument. 42 | * 43 | * @access protected 44 | * @var string $page_title The text to be displayed in the title tags of the page when the menu is selected. 45 | * @since 1.0.2 46 | */ 47 | protected $page_title; 48 | /** 49 | * Define menu_title property in Option_Menu class. 50 | * This property use to pass to add_options_page as an argument. 51 | * 52 | * @access protected 53 | * @var string $menu_title The text to be used for the menu. 54 | * @since 1.0.2 55 | */ 56 | protected $menu_title; 57 | /** 58 | * Define capability property in Option_Menu class. 59 | * This property use to pass to add_options_page as an argument. 60 | * 61 | * @access protected 62 | * @var string $capability The capability required for this menu to be displayed to the user. 63 | * @since 1.0.2 64 | */ 65 | protected $capability; 66 | /** 67 | * Define menu_slug property in Option_Menu class. 68 | * This property use to pass to add_options_page as an argument. 69 | * 70 | * @access protected 71 | * @var string $menu_slug The slug name to refer to this menu by. 72 | * @since 1.0.2 73 | */ 74 | protected $menu_slug; 75 | /** 76 | * Define callable_function property in Option_Menu class. 77 | * This property use to define callable function name in add_options_page. 78 | * 79 | * @access protected 80 | * @var callable $callable_function The function to be called to output the content for this page. 81 | * @since 1.0.2 82 | */ 83 | protected $callable_function; 84 | /** 85 | * Define position property in Option_Menu class. 86 | * This property use to pass to add_options_page as an argument. 87 | * 88 | * @access protected 89 | * @var int $position The position in the menu order this item should appear. 90 | * @since 1.0.2 91 | */ 92 | protected $position; 93 | 94 | /** 95 | * Option_Menu constructor. 96 | * This constructor gets initial values to send to add_options_page function to 97 | * create admin menu. 98 | * 99 | * @access public 100 | * 101 | * @param array $initial_value Initial value to pass to add_options_page function. 102 | */ 103 | public function __construct( array $initial_values ) { 104 | $this->page_title = $initial_values['page_title']; 105 | $this->menu_title = $initial_values['menu_title']; 106 | $this->capability = $initial_values['capability']; 107 | $this->menu_slug = $initial_values['menu_slug']; 108 | $this->callable_function = $initial_values['callable_function']; 109 | $this->position = $initial_values['position']; 110 | } 111 | 112 | /** 113 | * Method add_Option_Menu_page in Option_Menu Class 114 | * 115 | * Inside this method, we call add_options_page function to create admin menu 116 | * page in WordPress Admin Panel. 117 | * 118 | * @access public 119 | */ 120 | public function add_option_menu_page( $extra_args = null ) { 121 | add_options_page( 122 | $this->page_title, 123 | $this->menu_title, 124 | $this->capability, 125 | $this->menu_slug, 126 | //array( $this, 'handle_option_panel' ), 127 | function () use ( $extra_args ) { 128 | $this->handle_option_panel( $extra_args ); 129 | }, 130 | $this->position 131 | ); 132 | } 133 | 134 | /** 135 | * call 'admin_menu' add_action to create Admin menu page 136 | * 137 | * @access public 138 | */ 139 | public function register_add_action() { 140 | add_action( 'admin_menu', array( $this, 'add_option_menu_page' ) ); 141 | } 142 | 143 | /** 144 | * call 'admin_menu' add_action to create Admin menu page with extra arguments 145 | * 146 | * @access public 147 | */ 148 | public function register_add_action_with_arguments( $extra_args = null ) { 149 | add_action( 'admin_menu', function () use ( $extra_args ) { 150 | $this->add_option_menu_page( $extra_args ); 151 | } ); 152 | } 153 | 154 | 155 | /** 156 | * Abstract Method handle_option_panel in Option_Menu Class 157 | * 158 | * For each option menu page, we must have callable function that render and 159 | * handle this option menu page. For each option menu page, you must implement it. 160 | * 161 | * @access public 162 | */ 163 | abstract public function handle_option_panel( $extra_args = null ); 164 | 165 | } 166 | -------------------------------------------------------------------------------- /includes/abstracts/class-admin-menu.php: -------------------------------------------------------------------------------- 1 | 10 | * @license https://www.gnu.org/licenses/gpl-3.0.txt GNU/GPLv3 11 | * @link https://github.com/msn60/oop-wordpress-plugin-boilerplate 12 | * @since 1.0.2 13 | */ 14 | 15 | namespace Plugin_Name_Name_Space\Includes\Abstracts; 16 | 17 | use Plugin_Name_Name_Space\Includes\Interfaces\Action_Hook_Interface; 18 | 19 | if ( ! defined( 'ABSPATH' ) ) { 20 | exit; 21 | } 22 | 23 | /** 24 | * Class Admin_Menu. 25 | * If you want create an admin page inside admin panel of WordPress, 26 | * you can use from this class. 27 | * 28 | * @package Plugin_Name_Name_Space 29 | * @author Mehdi Soltani 30 | * @link https://github.com/msn60/oop-wordpress-plugin-boilerplate 31 | * 32 | * @see wp-admin/includes/plugin.php 33 | * @see https://developer.wordpress.org/reference/functions/add_menu_page/ 34 | */ 35 | abstract class Admin_Menu implements Action_Hook_Interface { 36 | 37 | /** 38 | * Define page_title property in Admin_Menu class. 39 | * This property use to pass to add_menu_page as an argument. 40 | * 41 | * @access protected 42 | * @var string $page_title The text to be displayed in the title tags of the page when the menu is selected. 43 | * @since 1.0.2 44 | */ 45 | protected $page_title; 46 | /** 47 | * Define menu_title property in Admin_Menu class. 48 | * This property use to pass to add_menu_page as an argument. 49 | * 50 | * @access protected 51 | * @var string $menu_title The text to be used for the menu. 52 | * @since 1.0.2 53 | */ 54 | protected $menu_title; 55 | /** 56 | * Define capability property in Admin_Menu class. 57 | * This property use to pass to add_menu_page as an argument. 58 | * 59 | * @access protected 60 | * @var string $capability The capability required for this menu to be displayed to the user. 61 | * @since 1.0.2 62 | */ 63 | protected $capability; 64 | /** 65 | * Define menu_slug property in Admin_Menu class. 66 | * This property use to pass to add_menu_page as an argument. 67 | * 68 | * @access protected 69 | * @var string $menu_slug The slug name to refer to this menu by. 70 | * @since 1.0.2 71 | */ 72 | protected $menu_slug; 73 | /** 74 | * Define callable_function property in Admin_Menu class. 75 | * This property use to define callable function name in add_menu_page. 76 | * 77 | * @access protected 78 | * @var callable $callable_function The function to be called to output the content for this page. 79 | * @since 1.0.2 80 | */ 81 | protected $callable_function; 82 | /** 83 | * Define icon_url property in Admin_Menu class. 84 | * This property use to pass to add_menu_page as an argument. 85 | * 86 | * @access protected 87 | * @var string $icon_url The URL to the icon to be used for this menu. 88 | * @since 1.0.2 89 | */ 90 | protected $icon_url; 91 | /** 92 | * Define position property in Admin_Menu class. 93 | * This property use to pass to add_menu_page as an argument. 94 | * 95 | * @access protected 96 | * @var int $position The position in the menu order this one should appear 97 | * @since 1.0.2 98 | */ 99 | protected $position; 100 | /** 101 | * Define unique name for admin menu. 102 | * I will use it for logging in future implementation 103 | * 104 | * @access protected 105 | * @var string $identifier Define a unique name for admin menu 106 | * @since 1.0.2 107 | */ 108 | protected $identifier; 109 | 110 | /** 111 | * Admin_Menu constructor. 112 | * This constructor gets initial values to send to add_menu_page function to 113 | * create admin menu. 114 | * 115 | * @access public 116 | * 117 | * @param array $initial_value Initial value to pass to add_menu_page function. 118 | */ 119 | public function __construct( array $initial_values ) { 120 | $this->page_title = $initial_values['page_title']; 121 | $this->menu_title = $initial_values['menu_title']; 122 | $this->capability = $initial_values['capability']; 123 | $this->menu_slug = $initial_values['menu_slug']; 124 | $this->callable_function = $initial_values['callable_function']; 125 | $this->icon_url = $initial_values['icon_url']; 126 | $this->position = $initial_values['position']; 127 | $this->identifier = $initial_values['identifier']; 128 | } 129 | 130 | /** 131 | * Method add_admin_menu_page in Admin_Menu Class 132 | * 133 | * Inside this method, we call add_menu_page function to create admin menu 134 | * page in WordPress Admin Panel. 135 | * 136 | * @access public 137 | */ 138 | public function add_admin_menu_page() { 139 | add_menu_page( 140 | $this->page_title, 141 | $this->menu_title, 142 | $this->capability, 143 | $this->menu_slug, 144 | array( $this, 'management_panel_handler' ), 145 | $this->icon_url, 146 | $this->position 147 | ); 148 | } 149 | 150 | /** 151 | * call 'admin_menu' add_action to create Admin menu page 152 | * 153 | * @access public 154 | */ 155 | public function register_add_action() { 156 | add_action( 'admin_menu', array( $this, 'add_admin_menu_page' ) ); 157 | } 158 | 159 | 160 | /** 161 | * Abstract Method management_panel_handler in Admin_Menu Class 162 | * 163 | * For each admin menu page, we must have callable function that render and 164 | * handle this menu page. For each menu page, you must implement it. 165 | * 166 | * @access public 167 | */ 168 | abstract public function management_panel_handler(); 169 | 170 | } 171 | -------------------------------------------------------------------------------- /includes/init/class-activator.php: -------------------------------------------------------------------------------- 1 | 12 | * @license https://www.gnu.org/licenses/gpl-3.0.txt GNU/GPLv3 13 | * @link https://github.com/msn60/oop-wordpress-plugin-boilerplate 14 | * @since 1.0.2 15 | */ 16 | 17 | namespace Plugin_Name_Name_Space\Includes\Init; 18 | 19 | if ( ! defined( 'ABSPATH' ) ) { 20 | exit; 21 | } 22 | 23 | use Plugin_Name_Name_Space\Includes\Abstracts\{ 24 | Custom_Post_Type, Custom_Taxonomy 25 | }; 26 | use Plugin_Name_Name_Space\Includes\Database\Table; 27 | use Plugin_Name_Name_Space\Includes\Config\Info; 28 | use Plugin_Name_Name_Space\Includes\Functions\{ 29 | Check_Type, Current_User, Logger 30 | }; 31 | 32 | /** 33 | * Class Activator. 34 | * If you want to perform some actions in activating of your plugin, you can add your desire methods to it. 35 | * Actions likes installing separated tables (except WordPress tables), 36 | * initializing configs for plugin and using update_option, can do with this class. 37 | * 38 | * @package Plugin_Name_Name_Space 39 | * @author Mehdi Soltani 40 | * @see \Plugin_Name_Name_Space\Includes\Config\Info 41 | * @see \Plugin_Name_Name_Space\Includes\Database\Table 42 | */ 43 | class Activator { 44 | use Logger; 45 | use Check_Type; 46 | use Current_User; 47 | /** 48 | * @var int $last_db_version The last version of your plugin database 49 | */ 50 | private $last_db_version; 51 | 52 | /** 53 | * @var Custom_Post_Type[] $custom_post_types 54 | */ 55 | private $custom_post_types; 56 | 57 | /** 58 | * @var Custom_Taxonomy[] $custom_taxonomies 59 | */ 60 | private $custom_taxonomies; 61 | 62 | /** 63 | * @var Table $table_object Table object to create or modify tables 64 | */ 65 | private $table_object; 66 | 67 | /** 68 | * Activator constructor. 69 | */ 70 | public function __construct( $last_db_version = null ) { 71 | $this->last_db_version = $last_db_version; 72 | } 73 | 74 | /** 75 | * Method activate in Activator Class 76 | * 77 | * It calls when plugin is activated. 78 | * 79 | * @access public 80 | * @static 81 | */ 82 | public function activate( 83 | $is_need_table_modification = false, 84 | array $custom_post_types = null, 85 | array $custom_taxonomies = null, 86 | Table $table_object = null 87 | ) { 88 | 89 | $this->register_activator_user(); 90 | 91 | if ( ! is_null( $custom_post_types ) ) { 92 | $this->custom_post_types = $this->check_array_by_parent_type( $custom_post_types, Custom_Post_Type::class )['valid']; 93 | if ( ! is_null( $this->custom_post_types ) ) { 94 | $this->register_plugin_custom_post_type(); 95 | } 96 | } 97 | 98 | if ( ! is_null( $custom_taxonomies ) ) { 99 | $this->custom_taxonomies = $this->check_array_by_parent_type( $custom_taxonomies, Custom_Taxonomy::class )['valid']; 100 | if ( ! is_null( $this->custom_taxonomies ) ) { 101 | $this->register_plugin_custom_taxonomy(); 102 | } 103 | } 104 | 105 | // Create needed tables in plugin activation. 106 | $this->table_object = $table_object; 107 | if ( true === $is_need_table_modification ) { 108 | if ( intval( $this->table_object->db_version ) > $this->last_db_version 109 | ) { 110 | $this->create_needed_tables(); 111 | } 112 | } 113 | // Initialize plugin settings and info in option table. 114 | // TODO: separate this part to another method and then call it 115 | Info::add_info_in_plugin_activation(); 116 | $this->append_log_in_text_file( 'Sample to test logger class when plugin is activated', PLUGIN_NAME_LOGS . 'activator-logs.txt', 117 | 'Activator Last Log' ); 118 | 119 | //TODO: Show customized messages when plugin is activated 120 | 121 | 122 | } 123 | 124 | /** 125 | * Register user who activate the plugin 126 | */ 127 | public function register_activator_user() { 128 | 129 | $current_user = $this->get_this_login_user(); 130 | $this->append_log_in_text_file( 131 | 'The user with login of: "' . $current_user->user_login . '" and display name of: "' . $current_user->display_name 132 | . '" activated this plugin', 133 | PLUGIN_NAME_LOGS . 'activator-logs.txt', 134 | 'Activator User' ); 135 | 136 | } 137 | 138 | /** 139 | * Method to register all of needed custom post types and flush rewrite rules 140 | * 141 | * @access private 142 | * @since 1.0.2 143 | */ 144 | private function register_plugin_custom_post_type() { 145 | if ( ! is_null( $this->custom_post_types ) ) { 146 | foreach ( $this->custom_post_types as $custom_post_type ) { 147 | $custom_post_type->add_custom_post_type(); 148 | } 149 | // ATTENTION: This is *only* done during plugin activation hook in this example! 150 | // You should *NEVER EVER* do this on every page load!! 151 | flush_rewrite_rules(); 152 | if ( ! get_option( 'has_rewrite_for_plugin_name_new_post_types' ) ) { 153 | flush_rewrite_rules(); 154 | update_option( 155 | 'has_rewrite_for_plugin_name_new_post_types', 156 | true 157 | ); 158 | } 159 | } 160 | } 161 | 162 | /** 163 | * Method to register all of needed custom taxonomies and flush rewrite rules 164 | * 165 | * @access private 166 | * @since 1.0.2 167 | */ 168 | private function register_plugin_custom_taxonomy() { 169 | if ( ! is_null( $this->custom_taxonomies ) ) { 170 | foreach ( $this->custom_taxonomies as $custom_taxonomy ) { 171 | $custom_taxonomy->add_custom_taxonomy(); 172 | } 173 | // ATTENTION: This is *only* done during plugin activation hook in this example! 174 | // You should *NEVER EVER* do this on every page load!! 175 | flush_rewrite_rules(); 176 | if ( ! get_option( 'has_rewrite_for_plugin_name_new_taxonomies' ) ) { 177 | flush_rewrite_rules(); 178 | update_option( 179 | 'has_rewrite_for_plugin_name_new_taxonomies', 180 | true 181 | ); 182 | } 183 | } 184 | } 185 | 186 | /** 187 | * Create needed tables when plugin is activated 188 | * 189 | * Check is your table exist in database or not you can use from it 190 | * for all of your table in first time that your plugin is created. 191 | */ 192 | private function create_needed_tables() { 193 | if ( true !== $this->table_object->has_table_name ) { 194 | $this->table_object->create_your_table_name(); 195 | } 196 | 197 | update_option( 198 | 'last_plugin_name_dbs_version', 199 | $this->table_object->db_version 200 | ); 201 | } 202 | } 203 | 204 | -------------------------------------------------------------------------------- /includes/abstracts/class-simple-setting-page.php: -------------------------------------------------------------------------------- 1 | 10 | * @license https://www.gnu.org/licenses/gpl-3.0.txt GNU/GPLv3 11 | * @link https://github.com/msn60/oop-wordpress-plugin-boilerplate 12 | * @since 1.0.2 13 | */ 14 | 15 | namespace Plugin_Name_Name_Space\Includes\Abstracts; 16 | 17 | use Plugin_Name_Name_Space\Includes\Interfaces\Action_Hook_Interface; 18 | 19 | if ( ! defined( 'ABSPATH' ) ) { 20 | exit; 21 | } 22 | 23 | /** 24 | * Class Simple_Setting_Page. 25 | * This file contains contract for Simple_Setting_Page class. If you want create an settings page 26 | * inside admin panel of WordPress, you must to use this contract. 27 | * 28 | * @package Plugin_Name_Name_Space 29 | * @author Mehdi Soltani 30 | * @link https://github.com/msn60/oop-wordpress-plugin-boilerplate 31 | * 32 | * @see wp-admin/includes/plugin.php 33 | * @see https://developer.wordpress.org/reference/functions/add_menu_page/ 34 | */ 35 | abstract class Simple_Setting_Page implements Action_Hook_Interface { 36 | 37 | /** 38 | * A settings group name. 39 | * Should correspond to a whitelisted option key name. 40 | * 41 | * @var string $option_group A settings group name 42 | * @since 1.0.2 43 | */ 44 | protected $option_group; 45 | /** 46 | * The name of an option to sanitize and save. 47 | * 48 | * @var string $option_name The name of an option to sanitize and save 49 | * @since 1.0.2 50 | */ 51 | protected $option_name; 52 | /** 53 | * Data used to describe the setting when registered. 54 | * 55 | * @var string $type The type of data associated with this setting. 56 | * @since 1.0.2 57 | * @see https://developer.wordpress.org/reference/functions/register_setting/ 58 | */ 59 | protected $register_setting_args; 60 | /** 61 | * Array of settings sections arguments. 62 | * It's an array of arguments that 'add_setting_section' method needs. 63 | * 64 | * @var array $settings_sections Array of settings sections arguments. 65 | * @see https://developer.wordpress.org/reference/functions/add_settings_section/ 66 | */ 67 | protected $settings_sections; 68 | /** 69 | * Array of settings fields arguments. 70 | * It's an array of argument that 'add_setting_field' method needs. 71 | * 72 | * @var array $settings_fields Array of settings fields arguments. 73 | * @see https://developer.wordpress.org/reference/functions/add_settings_field/ 74 | */ 75 | protected $settings_fields; 76 | /** 77 | * Array of settings errors arguments. 78 | * It's an array of argument that 'add_setting_error' method needs. 79 | * 80 | * @var array $settings_errors Array of settings errors arguments. 81 | * @see https://developer.wordpress.org/reference/functions/add_settings_error/ 82 | */ 83 | protected $settings_errors; 84 | 85 | 86 | /** 87 | * Simple_Setting_Page constructor. 88 | * This constructor gets initial values to send to add_menu_page function to 89 | * create admin menu. 90 | * 91 | * @access public 92 | * 93 | * @param array $initial_value Initial value to pass to add_menu_page function. 94 | */ 95 | public function __construct( array $initial_values ) { 96 | $this->option_group = $initial_values['option_group']; 97 | $this->option_name = $initial_values['option_name']; 98 | $this->register_setting_args = array( 99 | 'type' => $initial_values['register_setting_args']['type'], 100 | 'description' => $initial_values['register_setting_args']['description'], 101 | 'sanitize_callback' => array( $this, 'sanitize_setting_fields' ), 102 | 'show_in_rest' => $initial_values['register_setting_args']['show_in_rest'], 103 | 'default' => $initial_values['register_setting_args']['default'], 104 | 105 | ); 106 | $this->settings_sections = $initial_values['settings_sections']; 107 | $this->settings_fields = $initial_values['settings_fields']; 108 | $this->settings_errors = $initial_values['settings_errors']; 109 | } 110 | 111 | /** 112 | * call 'admin_init' add_action to create settings page 113 | * 114 | * @access public 115 | */ 116 | public function register_add_action() { 117 | add_action( 'admin_init', array( $this, 'add_settings_page' ) ); 118 | } 119 | 120 | 121 | /** 122 | * Method add_admin_menu_page in Simple_Setting_Page Class 123 | * 124 | * Inside this method, we call add_menu_page function to create admin menu 125 | * page in WordPress Admin Panel. 126 | * 127 | * @access public 128 | */ 129 | public function add_settings_page() { 130 | 131 | $this->init_register_setting(); 132 | $this->add_all_settings_sections(); 133 | $this->add_all_settings_fields(); 134 | 135 | } 136 | 137 | /** 138 | * A method to register settings 139 | */ 140 | public function init_register_setting() { 141 | register_setting($this->option_group, $this->option_name, $this->register_setting_args); 142 | } 143 | 144 | /** 145 | * A method to add all of settings section 146 | * 147 | * @see https://developer.wordpress.org/reference/functions/add_settings_section/ 148 | */ 149 | public function add_all_settings_sections() { 150 | if ( ! is_null($this->settings_sections)) { 151 | foreach ( $this->settings_sections as $settings_section ) { 152 | add_settings_section( 153 | $settings_section['id'], 154 | $settings_section['title'], 155 | array( $this, 'create_'.$settings_section['callback_function'] ), 156 | $settings_section['page'] 157 | ); 158 | } 159 | } 160 | 161 | } 162 | 163 | /** 164 | * A method to add all of settings fields 165 | * 166 | * @see https://developer.wordpress.org/reference/functions/add_settings_field/ 167 | */ 168 | public function add_all_settings_fields() { 169 | if ( ! is_null( $this->settings_fields ) ) { 170 | foreach ( $this->settings_fields as $settings_field ) { 171 | add_settings_field( 172 | $settings_field['id'], 173 | $settings_field['title'], 174 | array( $this, 'create_'.$settings_field['callback_function'] ), 175 | $settings_field['page'], 176 | $settings_field['section'] 177 | ); 178 | } 179 | } 180 | } 181 | 182 | public function create_settings_error( $name ) { 183 | add_settings_error( 184 | $this->settings_errors[$name]['setting'], 185 | $this->settings_errors[$name]['code'], 186 | $this->settings_errors[$name]['message'], 187 | $this->settings_errors[$name]['type'], 188 | ); 189 | 190 | } 191 | 192 | /** 193 | * sanitize_setting_fields method. 194 | * A callback function that sanitizes the option's value. 195 | * 196 | * @access public 197 | * 198 | */ 199 | abstract public function sanitize_setting_fields( $input); 200 | 201 | } 202 | -------------------------------------------------------------------------------- /includes/init/class-constant.php: -------------------------------------------------------------------------------- 1 | 10 | * @license https://www.gnu.org/licenses/gpl-3.0.txt GNU/GPLv3 11 | * @link https://github.com/msn60/oop-wordpress-plugin-boilerplate 12 | * @since 1.0.2 13 | */ 14 | 15 | namespace Plugin_Name_Name_Space\Includes\Init; 16 | 17 | if ( ! defined( 'ABSPATH' ) ) { 18 | exit; 19 | } 20 | /** 21 | * Class Constant 22 | * 23 | * This class defines needed constants that you will use in plugin development. 24 | * 25 | * @package Plugin_Name_Name_Space 26 | * @author Mehdi Soltani 27 | */ 28 | class Constant { 29 | 30 | /** 31 | * Define define_constant method in Constant class 32 | * 33 | * It defines all of constants that you need 34 | * 35 | * @access public 36 | * @static 37 | */ 38 | public static function define_constant() { 39 | 40 | /** 41 | * PLUGIN_NAME_PATH constant. 42 | * It is used to specify plugin path 43 | */ 44 | if ( ! defined( 'PLUGIN_NAME_PATH' ) ) { 45 | define( 'PLUGIN_NAME_PATH', trailingslashit( plugin_dir_path( dirname( dirname( __FILE__ ) ) ) ) ); 46 | } 47 | 48 | /** 49 | * PLUGIN_NAME_URL constant. 50 | * It is used to specify plugin urls 51 | */ 52 | if ( ! defined( 'PLUGIN_NAME_URL' ) ) { 53 | define( 'PLUGIN_NAME_URL', trailingslashit( plugin_dir_url( dirname( dirname( __FILE__ ) ) ) ) ); 54 | } 55 | 56 | /** 57 | * PLUGIN_NAME_CSS constant. 58 | * It is used to specify css urls inside assets directory. It's used in front end and 59 | * using to load related CSS files for front end user. 60 | */ 61 | if ( ! defined( 'PLUGIN_NAME_CSS' ) ) { 62 | define( 'PLUGIN_NAME_CSS', trailingslashit( PLUGIN_NAME_URL ) . 'assets/css/' ); 63 | } 64 | 65 | /** 66 | * PLUGIN_NAME_JS constant. 67 | * It is used to specify JavaScript urls inside assets directory. It's used in front end and 68 | * using to load related JS files for front end user. 69 | */ 70 | if ( ! defined( 'PLUGIN_NAME_JS' ) ) { 71 | define( 'PLUGIN_NAME_JS', trailingslashit( PLUGIN_NAME_URL ) . 'assets/js/' ); 72 | } 73 | 74 | /** 75 | * PLUGIN_NAME_IMG constant. 76 | * It is used to specify image urls inside assets directory. It's used in front end and 77 | * using to load related image files for front end user. 78 | */ 79 | if ( ! defined( 'PLUGIN_NAME_IMG' ) ) { 80 | define( 'PLUGIN_NAME_IMG', trailingslashit( PLUGIN_NAME_URL ) . 'assets/images/' ); 81 | } 82 | 83 | /** 84 | * PLUGIN_NAME_ADMIN_CSS constant. 85 | * It is used to specify css urls inside assets/admin directory. It's used in WordPress 86 | * admin panel and using to load related CSS files for admin user. 87 | */ 88 | if ( ! defined( 'PLUGIN_NAME_ADMIN_CSS' ) ) { 89 | define( 'PLUGIN_NAME_ADMIN_CSS', trailingslashit( PLUGIN_NAME_URL ) . 'assets/admin/css/' ); 90 | } 91 | 92 | /** 93 | * PLUGIN_NAME_ADMIN_JS constant. 94 | * It is used to specify JS urls inside assets/admin directory. It's used in WordPress 95 | * admin panel and using to load related JS files for admin user. 96 | */ 97 | if ( ! defined( 'PLUGIN_NAME_ADMIN_JS' ) ) { 98 | define( 'PLUGIN_NAME_ADMIN_JS', trailingslashit( PLUGIN_NAME_URL ) . 'assets/admin/js/' ); 99 | } 100 | 101 | /** 102 | * PLUGIN_NAME_ADMIN_IMG constant. 103 | * It is used to specify image urls inside assets/admin directory. It's used in WordPress 104 | * admin panel and using to load related JS files for admin user. 105 | */ 106 | if ( ! defined( 'PLUGIN_NAME_ADMIN_IMG' ) ) { 107 | define( 'PLUGIN_NAME_ADMIN_IMG', trailingslashit( PLUGIN_NAME_URL ) . 'assets/admin/images/' ); 108 | } 109 | 110 | /** 111 | * PLUGIN_NAME_TPL constant. 112 | * It is used to specify template urls inside templates directory. 113 | */ 114 | if ( ! defined( 'PLUGIN_NAME_TPL' ) ) { 115 | define( 'PLUGIN_NAME_TPL', trailingslashit( PLUGIN_NAME_PATH . 'templates' ) ); 116 | } 117 | 118 | /** 119 | * PLUGIN_NAME_INC constant. 120 | * It is used to specify include path inside includes directory. 121 | */ 122 | if ( ! defined( 'PLUGIN_NAME_INC' ) ) { 123 | define( 'PLUGIN_NAME_INC', trailingslashit( PLUGIN_NAME_PATH . 'includes' ) ); 124 | } 125 | 126 | /** 127 | * PLUGIN_NAME_LANG constant. 128 | * It is used to specify language path inside languages directory. 129 | */ 130 | if ( ! defined( 'PLUGIN_NAME_LANG' ) ) { 131 | define( 'PLUGIN_NAME_LANG', trailingslashit( PLUGIN_NAME_PATH . 'languages' ) ); 132 | } 133 | 134 | /** 135 | * PLUGIN_NAME_TPL_ADMIN constant. 136 | * It is used to specify template urls inside templates/admin directory. If you want to 137 | * create a template for admin panel or administration purpose, you will use from it. 138 | */ 139 | if ( ! defined( 'PLUGIN_NAME_TPL_ADMIN' ) ) { 140 | define( 'PLUGIN_NAME_TPL_ADMIN', trailingslashit( PLUGIN_NAME_TPL . 'admin' ) ); 141 | } 142 | 143 | /** 144 | * PLUGIN_NAME_TPL_FRONT constant. 145 | * It is used to specify template urls inside templates/front directory. If you want to 146 | * create a template for front end or end user purposes, you will use from it. 147 | */ 148 | if ( ! defined( 'PLUGIN_NAME_TPL_FRONT' ) ) { 149 | define( 'PLUGIN_NAME_TPL_FRONT', trailingslashit( PLUGIN_NAME_TPL . 'front' ) ); 150 | } 151 | 152 | /** 153 | * PLUGIN_NAME_TPL constant. 154 | * It is used to specify template urls inside templates directory. 155 | */ 156 | if ( ! defined( 'PLUGIN_NAME_LOGS' ) ) { 157 | define( 'PLUGIN_NAME_LOGS', trailingslashit( PLUGIN_NAME_PATH . 'logs' ) ); 158 | } 159 | 160 | /** 161 | * PLUGIN_NAME_CSS_VERSION constant. 162 | * You can use from this constant to apply on main CSS file when you have changed it. 163 | */ 164 | if ( ! defined( 'PLUGIN_NAME_CSS_VERSION' ) ) { 165 | define( 'PLUGIN_NAME_CSS_VERSION', 1 ); 166 | } 167 | /** 168 | * PLUGIN_NAME_JS_VERSION constant. 169 | * You can use from this constant to apply on main JS file when you have changed it. 170 | */ 171 | if ( ! defined( 'PLUGIN_NAME_JS_VERSION' ) ) { 172 | define( 'PLUGIN_NAME_JS_VERSION', 1 ); 173 | } 174 | 175 | /** 176 | * PLUGIN_NAME_CSS_VERSION constant. 177 | * You can use from this constant to apply on main CSS file when you have changed it. 178 | */ 179 | if ( ! defined( 'PLUGIN_NAME_ADMIN_CSS_VERSION' ) ) { 180 | define( 'PLUGIN_NAME_ADMIN_CSS_VERSION', 1 ); 181 | } 182 | /** 183 | * PLUGIN_NAME_JS_VERSION constant. 184 | * You can use from this constant to apply on main JS file when you have changed it. 185 | */ 186 | if ( ! defined( 'PLUGIN_NAME_ADMIN_JS_VERSION' ) ) { 187 | define( 'PLUGIN_NAME_ADMIN_JS_VERSION', 1 ); 188 | } 189 | 190 | /** 191 | * PLUGIN_NAME_VERSION constant. 192 | * It defines version of plugin for management tasks in your plugin 193 | */ 194 | if ( ! defined( 'PLUGIN_NAME_VERSION') ) { 195 | define( 'PLUGIN_NAME_VERSION', '1.0.2' ); 196 | } 197 | 198 | /** 199 | * PLUGIN_NAME_MAIN_NAME constant. 200 | * It defines name of plugin for management tasks in your plugin 201 | */ 202 | if ( ! defined( 'PLUGIN_NAME_MAIN_NAME') ) { 203 | define( 'PLUGIN_NAME_MAIN_NAME', 'plugin-name' ); 204 | } 205 | 206 | /** 207 | * PLUGIN_NAME_DB_VERSION constant 208 | * 209 | * It defines database version 210 | * You can use from this constant to apply your changes in updates or 211 | * activate plugin again 212 | */ 213 | if ( ! defined( 'PLUGIN_NAME_DB_VERSION') ) { 214 | define( 'PLUGIN_NAME_DB_VERSION', 1 ); 215 | } 216 | 217 | /** 218 | * PLUGIN_NAME_TEXTDOMAIN constant 219 | * 220 | * It defines text domain name for plugin 221 | */ 222 | if ( ! defined( 'PLUGIN_NAME_TEXTDOMAIN') ) { 223 | define( 'PLUGIN_NAME_TEXTDOMAIN', 'plugin-name-textdomain' ); 224 | } 225 | /*In future maybe I want to add constants for separated upload directory inside plugin directory*/ 226 | } 227 | } 228 | -------------------------------------------------------------------------------- /includes/abstracts/class-meta-box.php: -------------------------------------------------------------------------------- 1 | 10 | * @license https://www.gnu.org/licenses/gpl-3.0.txt GNU/GPLv3 11 | * @link https://github.com/msn60/oop-wordpress-plugin-boilerplate 12 | * @since 1.0.2 13 | */ 14 | 15 | namespace Plugin_Name_Name_Space\Includes\Abstracts; 16 | 17 | use Plugin_Name_Name_Space\Includes\Interfaces\Action_Hook_Interface; 18 | 19 | 20 | if ( ! defined( 'ABSPATH' ) ) { 21 | exit; 22 | } 23 | 24 | /** 25 | * Class Admin_Menu. 26 | * This file contains Meta_box class. If you want create a meta box 27 | * inside admin panel of WordPress, you can use from this class. 28 | * 29 | * @package Plugin_Name_Name_Space 30 | * @author Mehdi Soltani 31 | * @see https://developer.wordpress.org/reference/functions/add_meta_box/ 32 | * @see https://developer.wordpress.org/reference/functions/get_post_meta/ 33 | * @see https://developer.wordpress.org/reference/functions/wp_nonce_field/ 34 | */ 35 | abstract class Meta_box implements Action_Hook_Interface { 36 | 37 | /** 38 | * Meta box ID (used in the 'id' attribute for the meta box) 39 | * 40 | * @access private 41 | * @var string $id The Meta box ID. 42 | * @since 1.0.2 43 | */ 44 | protected $id; 45 | /** 46 | * Title of the meta box. 47 | * 48 | * @access private 49 | * @var string $title Title of the meta box. 50 | * @since 1.0.2 51 | */ 52 | protected $title; 53 | /** 54 | * Function that fills the box with the desired content. The function should echo its output. 55 | * 56 | * @access private 57 | * @var callable $callback Function that fills the box with the desired content. 58 | * @since 1.0.2 59 | */ 60 | protected $callback; 61 | /** 62 | * The screen or screens on which to show the box (such as a post type, 'link', or 'comment'). 63 | * 64 | * Accepts a single screen ID, WP_Screen object, or array of screen IDs. 65 | * Default is the current screen. If you have used add_menu_page() or add_submenu_page() 66 | * to create a new screen (and hence screen_id), make sure your menu slug conforms to the limits of sanitize_key() 67 | * otherwise the 'screen' menu may not correctly render on your page.Default value: null 68 | * 69 | * @access private 70 | * @var string | array | \WP_Screen $screen The screen or screens on which to show the box. 71 | * @since 1.0.2 72 | */ 73 | protected $screens; 74 | /** 75 | * The context within the screen where the boxes should display. 76 | * 77 | * Available contexts vary from screen to screen. Post edit screen contexts include 'normal', 'side', and 'advanced'. 78 | * Comments screen contexts include 'normal' and 'side'. Menus meta boxes (accordion sections) all use the 'side' context. 79 | * GlobalDefault value: 'advanced'. 80 | * 81 | * @access private 82 | * @var string $context The context within the screen where the boxes should display. 83 | * @since 1.0.2 84 | */ 85 | protected $context; 86 | /** 87 | * The priority within the context where the boxes should show ('high', 'low'). 88 | * 89 | * Default value: 'default'. 90 | * 91 | * @access private 92 | * @var string $priority The priority within the context where the boxes should show . 93 | * @since 1.0.2 94 | */ 95 | protected $priority; 96 | /** 97 | * Data that should be set as the $args property of the box array. 98 | * 99 | * Data that should be set as the $args property of the box array 100 | * which is the second parameter passed to your callback). 101 | * 102 | * @access private 103 | * @var array $callback_args Data that should be set as the $args property of the box array. 104 | * @since 1.0.2 105 | */ 106 | protected $callback_args; 107 | /** 108 | * Name of post meta key: it's used as meta_key 109 | * 110 | * @access private 111 | * @var string $meta_key Name of post meta key. 112 | * @since 1.0.2 113 | */ 114 | protected $meta_key; 115 | /** 116 | * If true, returns only the first value for the specified meta key. 117 | * 118 | * @access private 119 | * @var bool $single If true, returns only the first value for the specified meta key. 120 | * @since 1.0.2 121 | */ 122 | protected $single; 123 | /** 124 | * Name of your action for wp_nonce_field method 125 | * 126 | * @access private 127 | * @var string $action Name of your action to set nonce. 128 | * @since 1.0.2 129 | */ 130 | protected $action; 131 | /** 132 | * Name of your nonce for wp_nonce_field method 133 | * 134 | * @access private 135 | * @var string $nonce_name Name of your nonce. 136 | * @since 1.0.2 137 | */ 138 | protected $nonce_name; 139 | 140 | 141 | /** 142 | * Meta_Box constructor. 143 | * This constructor gets initial values to send to add_meta_box & get_post_meta 144 | * & update_post_meta function to create specific meta box. 145 | * 146 | * @access public 147 | * 148 | * @param array $initial_value Initial value to pass to add_meta_box get_post_meta & update_post_meta function. 149 | */ 150 | public function __construct( array $initial_values ) { 151 | $this->id = $initial_values['id']; 152 | $this->title = $initial_values['title']; 153 | $this->callback = $initial_values['callback']; 154 | $this->screens = $initial_values['screens']; 155 | $this->context = $initial_values['context']; 156 | $this->priority = $initial_values['priority']; 157 | $this->callback_args = $initial_values['callback_args']; 158 | $this->meta_key = $initial_values['meta_key']; 159 | $this->single = $initial_values['single']; 160 | $this->action = $initial_values['action']; 161 | $this->nonce_name = $initial_values['nonce_name']; 162 | 163 | } 164 | 165 | /** 166 | * Register actions for Meta box class 167 | */ 168 | public function register_add_action() { 169 | add_action( 'add_meta_boxes', array( $this, 'add_meta_box' ) ); 170 | add_action( 'save_post', array( $this, 'save' ) ); 171 | } 172 | 173 | 174 | /** 175 | * Adds the meta box container. 176 | * 177 | * @access protected 178 | * 179 | * @param string $post_type Pass the current post type inside WordPress admin panel(global $post_type). 180 | */ 181 | public function add_meta_box( $post_type ) { 182 | // Limit meta box to certain post types which is define in $sreen 183 | if ( $this->screens === null or in_array( $post_type, $this->screens ) ) { 184 | add_meta_box( 185 | $this->id, 186 | $this->title, 187 | array( $this, $this->callback ), 188 | $post_type, 189 | $this->context, 190 | $this->priority 191 | ); 192 | } 193 | } 194 | 195 | /** 196 | * Abstract Method to render meta box content 197 | * 198 | * @access public 199 | * 200 | * @param object $post Pass current global post. 201 | */ 202 | abstract public function render_content( $post ); 203 | 204 | /** 205 | * Save the meta when the post is saved. 206 | * 207 | * @access protected 208 | * 209 | * @param int $post_id The ID of the post being saved. 210 | */ 211 | public function save( $post_id ) { 212 | 213 | /* 214 | * We need to verify this came from the our screen and with proper authorization, 215 | * because save_post can be triggered at other times. 216 | */ 217 | if ( $this->verify_before_save( $post_id ) == 'verified' ) { 218 | /* OK, it's safe for us to save the data now. */ 219 | $this->save_meta_box( $post_id ); 220 | } 221 | 222 | 223 | } 224 | 225 | protected function verify_before_save( $post_id ) { 226 | 227 | // Check if our nonce is set. 228 | if ( ! isset( $_POST[ $this->nonce_name ] ) ) { 229 | return $post_id; 230 | } 231 | 232 | $nonce = $_POST[ $this->nonce_name ]; 233 | 234 | // Verify that the nonce is valid. 235 | if ( ! wp_verify_nonce( $nonce, $this->action ) ) { 236 | return $post_id; 237 | } 238 | 239 | /* 240 | * If this is an autosave, our form has not been submitted, 241 | * so we don't want to do anything. 242 | */ 243 | if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { 244 | return $post_id; 245 | } 246 | 247 | // Check the user's permissions. 248 | if ( 'page' == $_POST['post_type'] ) { 249 | if ( ! current_user_can( 'edit_page', $post_id ) ) { 250 | return $post_id; 251 | } 252 | } else { 253 | if ( ! current_user_can( 'edit_post', $post_id ) ) { 254 | return $post_id; 255 | } 256 | } 257 | 258 | return 'verified'; 259 | } 260 | 261 | /** 262 | * Abstract method to save content of meta box 263 | * 264 | * @param $post_id 265 | * @param $_POST 266 | * 267 | * @return mixed 268 | */ 269 | abstract protected function save_meta_box( $post_id ); 270 | } 271 | -------------------------------------------------------------------------------- /includes/functions/class-utility.php: -------------------------------------------------------------------------------- 1 | 13 | * @license https://www.gnu.org/licenses/gpl-3.0.txt GNU/GPLv3 14 | * @link https://github.com/msn60/oop-wordpress-plugin-boilerplate 15 | * @since 1.0.2 16 | */ 17 | 18 | namespace Plugin_Name_Name_Space\Includes\Functions; 19 | 20 | if ( ! defined( 'ABSPATH' ) ) { 21 | exit; 22 | } 23 | 24 | /** 25 | * Class Utility. 26 | * This class contains functions that help you in general tasks like rendering 27 | * template, convert to numbers and words and so on. 28 | * 29 | * @package Plugin_Name_Name_Space 30 | * @author Mehdi Soltani 31 | */ 32 | trait Utility { 33 | 34 | /** 35 | * Method load_template in Utility Class 36 | * 37 | * This method calls to render Admin or Front HTML templates from templates/admin 38 | * or templates/front directories. You can use from dot (.) to separate nested 39 | * directories and this method will include your desire file for your plugin. 40 | * 41 | * @access public 42 | * @static 43 | * 44 | * @param string $template Path of template file which is separated by dot. 45 | * @param array $params Related parameters that must be extracted to use inside your template. 46 | * @param string $type To detect admin or front directory to use related constant path. 47 | */ 48 | public static function load_template( $template, $params = array(), $type = 'admin' ) { 49 | $template = str_replace( '.', '/', $template ); 50 | $base_path = 'admin' === $type ? PLUGIN_NAME_TPL_ADMIN : PLUGIN_NAME_TPL_FRONT; 51 | $view_file_path = $base_path . $template . '.php'; 52 | if ( file_exists( $view_file_path ) && is_readable( $view_file_path ) ) { 53 | ! empty( $params ) ? extract( $params ) : null; 54 | /** 55 | * Include template file path which will be rendered by your plugin. 56 | */ 57 | include $view_file_path; 58 | } else { 59 | echo '

Your file does not exist.

'; 60 | exit; 61 | } 62 | 63 | } 64 | 65 | /** 66 | * Method is_admin in Utility Class 67 | * 68 | * You can check with this method that user is admin and logged in. 69 | * 70 | * @access public 71 | * @static 72 | */ 73 | public static function is_admin() { 74 | return is_user_logged_in() && current_user_can( 'manage_options' ); 75 | } 76 | 77 | /** 78 | * Method check_menu_link in Utility Class 79 | * 80 | * This method can help you to generate link for menu (and other parts) 81 | * in special dashboard. Also you can skip to generate url for page which 82 | * is inside it (at the moment). 83 | * 84 | * @access public 85 | * @static 86 | * 87 | * @param string $menu_current_url Current menu URL. 88 | * @param string $page Page URL that client is inside it at the moment. 89 | * 90 | * @return string 91 | */ 92 | public static function check_menu_link( $menu_current_url, $page ) { 93 | if ( strpos( $menu_current_url, $page ) !== false ) { 94 | return '#'; 95 | } else { 96 | return self::create_url( '/your-desire-url-start' . $page ); 97 | } 98 | } 99 | 100 | /** 101 | * Method create_url in Utility Class 102 | * 103 | * This method generate absolute URL from relative address. 104 | * 105 | * @access public 106 | * @static 107 | * 108 | * @param string $url Relative address that is passed to this method. 109 | * 110 | * @return string It returns absolute path. 111 | */ 112 | public static function create_url( $url = '/' ) { 113 | return get_site_url() . $url; 114 | 115 | } 116 | 117 | /** 118 | * Method echo_active_class in Utility Class 119 | * 120 | * This method can echo active class (string) for item in menu that client 121 | * is inside it (at the moment). 122 | * 123 | * @access public 124 | * @static 125 | * 126 | * @param string $menu_current_url Current menu URL. 127 | * @param string $page Page URL that client is inside it at the moment. 128 | * 129 | * @return string 130 | */ 131 | public static function echo_active_class( $menu_current_url, $page ) { 132 | 133 | if ( strpos( $menu_current_url, $page ) !== false ) { 134 | return ' active '; 135 | } else { 136 | return ''; 137 | } 138 | } 139 | 140 | /** 141 | * Method detect_page in Utility Class 142 | * 143 | * This method can detect that url is the same with page (which client is inside it). 144 | * 145 | * @access public 146 | * @static 147 | * 148 | * @param string $menu_current_url Current menu URL. 149 | * @param string $page Page URL that client is inside it at the moment. 150 | * 151 | * @return string 152 | */ 153 | public static function detect_page( $menu_current_url, $page ) { 154 | if ( strpos( $menu_current_url, $page ) !== false ) { 155 | return true; 156 | } else { 157 | return false; 158 | } 159 | } 160 | 161 | /** 162 | * Method generate_random_code in Utility Class 163 | * 164 | * This method uses to generate cryptographically secure pseudo-random bytes (to use in e.g. unique token). 165 | * 166 | * @access public 167 | * @static 168 | * @see http://php.net/manual/en/function.random-bytes.php 169 | * 170 | * @param int $length The length of the random string that should be returned in bytes. 171 | * 172 | * @return string Returns a string containing the requested number of cryptographically secure random bytes. 173 | */ 174 | public static function generate_random_code( $length = 16 ) { 175 | return bin2hex( random_bytes( $length ) ); 176 | } 177 | 178 | /** 179 | * Method format_amount_by_3_digits in Utility Class 180 | * 181 | * This method uses to separate numbers, 3 digits of 3 digits in persian numbers format. 182 | * 183 | * @access public 184 | * @static 185 | * @see http://php.net/manual/en/function.number-format.php 186 | * 187 | * @param number $amount The number being formatted. 188 | * 189 | * @return string A formatted version of number. 190 | */ 191 | public static function format_amount_by_3_digits( $amount ) { 192 | $amount = number_format( $amount ); 193 | 194 | return self::convert_to_persian_number( $amount ); 195 | } 196 | 197 | /** 198 | * Method convert_to_persian_number in Utility Class 199 | * 200 | * This method converts English number to Persian number in a string 201 | * 202 | * @access public 203 | * @static 204 | * @see http://php.net/manual/en/function.number-format.php 205 | * 206 | * @param string $number The number which is passed to method. 207 | * 208 | * @return string A formatted version of number. 209 | */ 210 | public static function convert_to_persian_number( $number ) { 211 | $persian_numbers = [ '۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹', '۰' ]; 212 | $english_numbers = [ '1', '2', '3', '4', '5', '6', '7', '8', '9', '0' ]; 213 | 214 | return str_replace( $english_numbers, $persian_numbers, $number ); 215 | } 216 | 217 | /** 218 | * Method convert_ip_to_long in Utility Class 219 | * 220 | * This method converts ip of visitor to long integer. 221 | * 222 | * @access public 223 | * @static 224 | * @see http://php.net/manual/en/function.ip2long.php 225 | * 226 | * @return int/FALSE Returns the long integer or FALSE if ip_address is invalid. 227 | */ 228 | public static function convert_ip_to_long() { 229 | $remote_address = filter_var( wp_unslash( $_SERVER['REMOTE_ADDR'] ), FILTER_SANITIZE_STRING ); 230 | if ( '::1' === $remote_address ) { 231 | $remote_address = '127.0.0.1'; 232 | } 233 | 234 | return ip2long( $remote_address ); 235 | } 236 | 237 | /** 238 | * Method get_visitor_ip_address in Utility Class 239 | * 240 | * This method returns visitor IP address. 241 | * 242 | * @access public 243 | * @static 244 | * 245 | * @return string Returns IP address of visitor. 246 | */ 247 | public static function get_visitor_ip_address() { 248 | $remote_address = filter_var( wp_unslash( $_SERVER['REMOTE_ADDR'] ), FILTER_SANITIZE_STRING ); 249 | if ( '::1' === $remote_address ) { 250 | $remote_address = '127.0.0.1'; 251 | } 252 | 253 | return $remote_address; 254 | } 255 | 256 | /** 257 | * Method get_current_url in Utility Class 258 | * 259 | * This method return actual url that in your page 260 | * 261 | * @access public 262 | * @return string Actual url that user requests it. 263 | */ 264 | public function get_current_url() { 265 | $actual_link = ( 'on' === isset( $_SERVER['HTTPS'] ) && $_SERVER['HTTPS'] ? 'https' : 'http' ) 266 | . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; 267 | 268 | return $actual_link; 269 | } 270 | 271 | } 272 | -------------------------------------------------------------------------------- /plugin-name.php: -------------------------------------------------------------------------------- 1 | 17 | * Author URI: https://wpwebmaster.ir 18 | * License: GPL-2.0+ 19 | * License URI: http://www.gnu.org/licenses/gpl-2.0.txt 20 | */ 21 | 22 | /** 23 | * Define your namespaces here by use keyword 24 | */ 25 | use Plugin_Name_Name_Space\Includes\Init\{ 26 | Admin_Hook, Core, Constant, Activator, I18n, Public_Hook, Router 27 | }; 28 | use Plugin_Name_Name_Space\Includes\Config\Initial_Value; 29 | use Plugin_Name_Name_Space\Includes\Parts\Other\Remove_Post_Column; 30 | use Plugin_Name_Name_Space\Includes\Uninstall\{ 31 | Deactivator, Uninstall 32 | }; 33 | use Plugin_Name_Name_Space\Includes\Admin\{ 34 | Admin_Menu1, Admin_Sub_Menu1, Admin_Sub_Menu2, Meta_Box3, Meta_Box4, Simple_Setting_Page1, 35 | Simple_Setting_In_Reading_Page1, Option_Menu1, Option_Menu2, Setting_Page1, 36 | Notices\Admin_Notice1, Notices\Woocommerce_Deactive_Notice 37 | }; 38 | 39 | use Plugin_Name_Name_Space\Includes\Functions\Init_Functions; 40 | use Plugin_Name_Name_Space\Includes\Database\Table; 41 | use Plugin_Name_Name_Space\Includes\Parts\Shortcodes\{ 42 | Shortcode1, Content_For_Login_User_Shortcode, Complete_Shortcode 43 | }; 44 | use Plugin_Name_Name_Space\Includes\Parts\Custom_Posts\Custom_Post1; 45 | use Plugin_Name_Name_Space\Includes\Parts\Custom_Taxonomies\Custom_Taxonomy1; 46 | use Plugin_Name_Name_Space\Includes\Hooks\Filters\Custom_Cron_Schedule; 47 | 48 | /** 49 | * If this file is called directly, then abort execution. 50 | */ 51 | if ( ! defined( 'ABSPATH' ) ) { 52 | exit; 53 | } 54 | 55 | 56 | /** 57 | * Class Plugin_Name_Plugin 58 | * 59 | * This class is primary file of plugin which is used from 60 | * singletone design pattern. 61 | * 62 | * @package Plugin_Name_Name_Space 63 | * @author Mehdi Soltani 64 | * @see Plugin_Name_Name_Space\Includes\Init\Core Class 65 | * @see Plugin_Name_Name_Space\Includes\Init\Constant Class 66 | * @see Plugin_Name_Name_Space\Includes\Init\Activator Class 67 | * @see Plugin_Name_Name_Space\Includes\Uninstall\Deactivator Class 68 | * @see Plugin_Name_Name_Space\Includes\Uninstall\Uninstall Class 69 | */ 70 | final class Plugin_Name_Plugin { 71 | /** 72 | * Instance property of Plugin_Name_Plugin Class. 73 | * This is a property in your plugin primary class. You will use to create 74 | * one object from Plugin_Name_Plugin class in whole of program execution. 75 | * 76 | * @access private 77 | * @var Plugin_Name_Plugin $instance create only one instance from plugin primary class 78 | * @static 79 | */ 80 | private static $instance; 81 | /** 82 | * @var Initial_Value $initial_values An object to keep all of initial values for theme 83 | */ 84 | protected $initial_values; 85 | /** 86 | * @var Core $core_object An object to keep core class for plugin. 87 | */ 88 | private $core_object; 89 | 90 | /** 91 | * Plugin_Name_Plugin constructor. 92 | * It defines related constant, include autoloader class, register activation hook, 93 | * deactivation hook and uninstall hook and call Core class to run dependencies for plugin 94 | * 95 | * @access private 96 | */ 97 | public function __construct() { 98 | /*Define Autoloader class for plugin*/ 99 | $autoloader_path = 'includes/class-autoloader.php'; 100 | /** 101 | * Include autoloader class to load all of classes inside this plugin 102 | */ 103 | require_once trailingslashit( plugin_dir_path( __FILE__ ) ) . $autoloader_path; 104 | /*Define required constant for plugin*/ 105 | Constant::define_constant(); 106 | 107 | /** 108 | * Register activation hook. 109 | * Register activation hook for this plugin by invoking activate 110 | * in Plugin_Name_Plugin class. 111 | * 112 | * @param string $file path to the plugin file. 113 | * @param callback $function The function to be run when the plugin is activated. 114 | */ 115 | register_activation_hook( 116 | __FILE__, 117 | function () { 118 | $this->activate( 119 | new Activator( intval( get_option( 'last_plugin_name_dbs_version' ) ) ) 120 | ); 121 | } 122 | ); 123 | /** 124 | * Register deactivation hook. 125 | * Register deactivation hook for this plugin by invoking deactivate 126 | * in Plugin_Name_Plugin class. 127 | * 128 | * @param string $file path to the plugin file. 129 | * @param callback $function The function to be run when the plugin is deactivated. 130 | */ 131 | register_deactivation_hook( 132 | __FILE__, 133 | function () { 134 | $this->deactivate( 135 | new Deactivator() 136 | ); 137 | } 138 | ); 139 | /** 140 | * Register uninstall hook. 141 | * Register uninstall hook for this plugin by invoking uninstall 142 | * in Plugin_Name_Plugin class. 143 | * 144 | * @param string $file path to the plugin file. 145 | * @param callback $function The function to be run when the plugin is uninstalled. 146 | */ 147 | register_uninstall_hook( 148 | __FILE__, 149 | array( 'Plugin_Name_Plugin', 'uninstall' ) 150 | ); 151 | } 152 | 153 | /** 154 | * Call activate method. 155 | * This function calls activate method from Activator class. 156 | * You can use from this method to run every thing you need when plugin is activated. 157 | * 158 | * @access public 159 | * @since 1.0.2 160 | * @see Plugin_Name_Name_Space\Includes\Init\Activator Class 161 | */ 162 | public function activate( Activator $activator_object ) { 163 | global $wpdb; 164 | $activator_object->activate( 165 | true, 166 | [ 167 | new Custom_Post1( $this->initial_values->sample_custom_post1() ) 168 | ], 169 | [ 170 | new Custom_Taxonomy1( $this->initial_values->sample_custom_taxonomy1() ) 171 | ], 172 | new Table( $wpdb, PLUGIN_NAME_DB_VERSION, get_option( 'has_table_name' ) ) 173 | ); 174 | } 175 | 176 | /** 177 | * Call deactivate method. 178 | * This function calls deactivate method from Dectivator class. 179 | * You can use from this method to run every thing you need when plugin is deactivated. 180 | * 181 | * @access public 182 | * @since 1.0.2 183 | */ 184 | public function deactivate( Deactivator $deaactivator_object ) { 185 | $deaactivator_object->deactivate(); 186 | } 187 | 188 | /** 189 | * Create an instance from Plugin_Name_Plugin class. 190 | * 191 | * @access public 192 | * @since 1.0.2 193 | * @return Plugin_Name_Plugin 194 | */ 195 | public static function instance() { 196 | if ( is_null( ( self::$instance ) ) ) { 197 | self::$instance = new self(); 198 | } 199 | 200 | return self::$instance; 201 | } 202 | 203 | /** 204 | * Call uninstall method. 205 | * This function calls uninstall method from Uninstall class. 206 | * You can use from this method to run every thing you need when plugin is uninstalled. 207 | * 208 | * @access public 209 | * @since 1.0.2 210 | */ 211 | public static function uninstall() { 212 | Uninstall::uninstall(); 213 | } 214 | 215 | /** 216 | * Load Core plugin class. 217 | * 218 | * @access public 219 | * @since 1.0.2 220 | */ 221 | public function run_plugin_name_plugin() { 222 | $this->initial_values = new Initial_Value(); 223 | $this->core_object = new Core( 224 | $this->initial_values, 225 | new Init_Functions(), 226 | new I18n(), 227 | new Admin_Hook( PLUGIN_NAME_MAIN_NAME, PLUGIN_NAME_VERSION ), 228 | new Public_Hook( PLUGIN_NAME_MAIN_NAME, PLUGIN_NAME_VERSION ), 229 | new Router(), 230 | [ 231 | new Admin_Menu1( $this->initial_values->sample_menu_page() ) 232 | ], 233 | [ 234 | new Admin_Sub_Menu1( $this->initial_values->sample_sub_menu_page1() ), 235 | new Admin_Sub_Menu2( $this->initial_values->sample_sub_menu_page2() ), 236 | ], 237 | [ 238 | new Meta_Box3( $this->initial_values->sample_meta_box3() ), 239 | new Meta_Box4( $this->initial_values->sample_meta_box4() ), 240 | ], 241 | [ 242 | new Shortcode1( $this->initial_values->sample_shortcode1() ), 243 | new Complete_Shortcode( $this->initial_values->sample_complete_shortcode() ), 244 | new Content_For_Login_User_Shortcode( $this->initial_values->sample_content_for_login_user_shortcode() ), 245 | ], 246 | [ 247 | new Custom_Post1( $this->initial_values->sample_custom_post1() ) 248 | ], 249 | [ 250 | new Custom_Taxonomy1( $this->initial_values->sample_custom_taxonomy1() ) 251 | ], 252 | [ 253 | 'admin_notice1' => new Admin_Notice1(), 254 | 'woocommerce_deactivate_notice' => new Woocommerce_Deactive_Notice(), 255 | ], 256 | [ 257 | new Option_Menu1($this->initial_values->sample_option_page()), 258 | ], 259 | [ 260 | new Simple_Setting_Page1( $this->initial_values->sample_setting_page1()), 261 | new Simple_Setting_In_Reading_Page1( $this->initial_values->sample_setting_in_reading_page1()), 262 | ], 263 | [ 264 | new Setting_Page1( 265 | $this->initial_values->get_complete_setting_page_arguments(), 266 | new Option_Menu2($this->initial_values->get_option_menu2()) 267 | ) 268 | ], 269 | new Custom_Cron_Schedule( $this->initial_values->sample_custom_cron_schedule() ) 270 | ); 271 | $this->core_object->init_core(); 272 | } 273 | } 274 | 275 | 276 | $plugin_name_plugin_object = Plugin_Name_Plugin::instance(); 277 | $plugin_name_plugin_object->run_plugin_name_plugin(); 278 | --------------------------------------------------------------------------------