From 6bc14385474322c2b70353e45beaa3eb6f85b159 Mon Sep 17 00:00:00 2001 From: MeatMAN Date: Wed, 1 Jun 2016 13:25:01 +0300 Subject: [PATCH 01/12] add the properties to the main clas with path/url data --- README.md | 82 +- assets/css/help-page.css | 1 + assets/css/index.php | 2 + {css => assets/css}/samplestyle-admin.css | 0 {css => assets/css}/samplestyle.css | 0 assets/index.php | 2 + assets/js/index.php | 2 + {js => assets/js}/samplescript-admin.js | 0 assets/js/samplescript.js | 3 + dx-plugin-base.php | 907 ++++++++++-------- .../class-dx-plugin-settings.php | 8 +- inc/dx-sample-widget.class.php | 24 +- inc/index.php | 2 + index.php | 2 + js/samplescript.js | 1 - lang/index.php | 2 + {inc => template}/base-page-template.php | 6 +- template/index.php | 2 + {inc => template}/remote-page-template.php | 12 +- 19 files changed, 566 insertions(+), 492 deletions(-) create mode 100644 assets/css/help-page.css create mode 100644 assets/css/index.php rename {css => assets/css}/samplestyle-admin.css (100%) rename {css => assets/css}/samplestyle.css (100%) create mode 100644 assets/index.php create mode 100644 assets/js/index.php rename {js => assets/js}/samplescript-admin.js (100%) create mode 100755 assets/js/samplescript.js rename dx-plugin-settings.class.php => inc/class-dx-plugin-settings.php (89%) create mode 100644 inc/index.php create mode 100644 index.php delete mode 100755 js/samplescript.js create mode 100644 lang/index.php rename {inc => template}/base-page-template.php (62%) create mode 100644 template/index.php rename {inc => template}/remote-page-template.php (76%) diff --git a/README.md b/README.md index 73a2389..b9335d3 100755 --- a/README.md +++ b/README.md @@ -31,7 +31,7 @@ This would add the `dx_add_JS` function do the hook responsible for adding scrip public function dx_add_JS() { wp_enqueue_script( 'jquery' ); // load custom JSes and put them in footer - wp_register_script( 'samplescript', plugins_url( '/js/samplescript.js' , __FILE__ ), array('jquery'), '1.0', true ); + wp_register_script( 'samplescript', $this->url_assets . 'js/samplescript.js', array('jquery'), '1.0', true ); wp_enqueue_script( 'samplescript' ); } ``` @@ -47,7 +47,7 @@ Calling a function for your backend is similar: ```php public function dx_add_admin_JS( $hook ) { wp_enqueue_script( 'jquery' ); - wp_register_script( 'samplescript-admin', plugins_url( '/js/samplescript-admin.js' , __FILE__ ), array('jquery'), '1.0', true ); + wp_register_script( 'samplescript-admin', $this->url_assets . 'js/samplescript-admin.js', array('jquery'), '1.0', true ); wp_enqueue_script( 'samplescript-admin' ); } ``` @@ -72,10 +72,10 @@ Then, you can call the `wp_enqueue_style` function within your callback method i ```php public function dx_add_admin_CSS( $hook ) { - wp_register_style( 'samplestyle-admin', plugins_url( '/css/samplestyle-admin.css', __FILE__ ), array(), '1.0', 'screen' ); + wp_register_style( 'samplestyle-admin', $this->url_assets . '/css/samplestyle-admin.css', array(), '1.0', 'screen' ); wp_enqueue_style( 'samplestyle-admin' ); if( 'toplevel_page_dx-plugin-base' === $hook ) { - wp_register_style('dx_help_page', plugins_url( '/help-page.css', __FILE__ ) ); + wp_register_style('dx_help_page', $this->url_assets . '/help-page.css' ); wp_enqueue_style('dx_help_page'); } } @@ -95,20 +95,22 @@ Then you can add top level or submenu pages to your dashboard menu: ```php public function dx_admin_pages_callback() { - add_menu_page(__( "Plugin Base Admin", 'dxbase' ), __( "Plugin Base Admin", 'dxbase' ), 'edit_themes', 'dx-plugin-base', array( $this, 'dx_plugin_base' ) ); - add_submenu_page( 'dx-plugin-base', __( "Base Subpage", 'dxbase' ), __( "Base Subpage", 'dxbase' ), 'edit_themes', 'dx-base-subpage', array( $this, 'dx_plugin_subpage' ) ); - add_submenu_page( 'dx-plugin-base', __( "Remote Subpage", 'dxbase' ), __( "Remote Subpage", 'dxbase' ), 'edit_themes', 'dx-remote-subpage', array( $this, 'dx_plugin_side_access_page' ) ); + add_menu_page(__( "Plugin Base Admin", DXP_TD ), __( "Plugin Base Admin", DXP_TD ), 'edit_themes', 'dx-plugin-base', array( $this, 'dx_plugin_base' ) ); + add_submenu_page( 'dx-plugin-base', __( "Base Subpage", DXP_TD ), __( "Base Subpage", DXP_TD ), 'edit_themes', 'dx-base-subpage', array( $this, 'dx_plugin_subpage' ) ); + add_submenu_page( 'dx-plugin-base', __( "Remote Subpage", DXP_TD ), __( "Remote Subpage", DXP_TD ), 'edit_themes', 'dx-remote-subpage', array( $this, 'dx_plugin_side_access_page' ) ); } ``` It's up to you what would you hook exactly and what would be the capabilities required for your users, but that's the sample syntax that you'd need. Each of those pages is defined via a callback at the end of the function parameters list, that could either be plain HTML/PHP, or loading an external file including your logic: ```php - // Earlier in your plugin header - define( 'DXP_PATH_INCLUDES', dirname( __FILE__ ) . '/inc' ); + // Earlier in your plugin setup + if( empty( $this->path_template ) ): + $this->path_template = trailingslashit( $this->path . 'template' ); + endif; // A class method for the callback public function dx_plugin_side_access_page() { - include_once( DXP_PATH_INCLUDES . '/remote-page-template.php' ); + include_once( $this->path_template . 'remote-page-template.php' ); } ``` @@ -126,18 +128,18 @@ The function responsible for the registration has plenty of options to play with public function dx_custom_post_types_callback() { register_post_type( 'pluginbase', array( 'labels' => array( - 'name' => __("Base Items", 'dxbase'), - 'singular_name' => __("Base Item", 'dxbase'), - 'add_new' => _x("Add New", 'pluginbase', 'dxbase' ), - 'add_new_item' => __("Add New Base Item", 'dxbase' ), - 'edit_item' => __("Edit Base Item", 'dxbase' ), - 'new_item' => __("New Base Item", 'dxbase' ), - 'view_item' => __("View Base Item", 'dxbase' ), - 'search_items' => __("Search Base Items", 'dxbase' ), - 'not_found' => __("No base items found", 'dxbase' ), - 'not_found_in_trash' => __("No base items found in Trash", 'dxbase' ), + 'name' => __("Base Items", DXP_TD), + 'singular_name' => __("Base Item", DXP_TD), + 'add_new' => _x("Add New", 'pluginbase', DXP_TD ), + 'add_new_item' => __("Add New Base Item", DXP_TD ), + 'edit_item' => __("Edit Base Item", DXP_TD ), + 'new_item' => __("New Base Item", DXP_TD ), + 'view_item' => __("View Base Item", DXP_TD ), + 'search_items' => __("Search Base Items", DXP_TD ), + 'not_found' => __("No base items found", DXP_TD ), + 'not_found_in_trash' => __("No base items found in Trash", DXP_TD ), ), - 'description' => __("Base Items for the demo", 'dxbase'), + 'description' => __("Base Items for the demo", DXP_TD), 'public' => true, 'publicly_queryable' => true, 'query_var' => true, @@ -177,20 +179,20 @@ Then, our callback is registering the custom taxonomy and binds it so a custom p register_taxonomy( 'pluginbase_taxonomy', 'pluginbase', array( 'hierarchical' => true, 'labels' => array( - 'name' => _x( "Base Item Taxonomies", 'taxonomy general name', 'dxbase' ), - 'singular_name' => _x( "Base Item Taxonomy", 'taxonomy singular name', 'dxbase' ), - 'search_items' => __( "Search Taxonomies", 'dxbase' ), - 'popular_items' => __( "Popular Taxonomies", 'dxbase' ), - 'all_items' => __( "All Taxonomies", 'dxbase' ), + 'name' => _x( "Base Item Taxonomies", 'taxonomy general name', DXP_TD ), + 'singular_name' => _x( "Base Item Taxonomy", 'taxonomy singular name', DXP_TD ), + 'search_items' => __( "Search Taxonomies", DXP_TD ), + 'popular_items' => __( "Popular Taxonomies", DXP_TD ), + 'all_items' => __( "All Taxonomies", DXP_TD ), 'parent_item' => null, 'parent_item_colon' => null, - 'edit_item' => __( "Edit Base Item Taxonomy", 'dxbase' ), - 'update_item' => __( "Update Base Item Taxonomy", 'dxbase' ), - 'add_new_item' => __( "Add New Base Item Taxonomy", 'dxbase' ), - 'new_item_name' => __( "New Base Item Taxonomy Name", 'dxbase' ), - 'separate_items_with_commas' => __( "Separate Base Item taxonomies with commas", 'dxbase' ), - 'add_or_remove_items' => __( "Add or remove Base Item taxonomy", 'dxbase' ), - 'choose_from_most_used' => __( "Choose from the most used Base Item taxonomies", 'dxbase' ) + 'edit_item' => __( "Edit Base Item Taxonomy", DXP_TD ), + 'update_item' => __( "Update Base Item Taxonomy", DXP_TD ), + 'add_new_item' => __( "Add New Base Item Taxonomy", DXP_TD ), + 'new_item_name' => __( "New Base Item Taxonomy Name", DXP_TD ), + 'separate_items_with_commas' => __( "Separate Base Item taxonomies with commas", DXP_TD ), + 'add_or_remove_items' => __( "Add or remove Base Item taxonomy", DXP_TD ), + 'choose_from_most_used' => __( "Choose from the most used Base Item taxonomies", DXP_TD ) ), 'show_ui' => true, 'query_var' => true, @@ -218,7 +220,7 @@ Our callback method will register the metaboxes that we need, attached to a spec // register side box add_meta_box( 'dx_side_meta_box', - __( "DX Side Box", 'dxbase' ), + __( "DX Side Box", DXP_TD ), array( $this, 'dx_side_meta_box' ), 'pluginbase', // leave empty quotes as '' if you want it on all custom post add/edit screens 'side', @@ -228,7 +230,7 @@ Our callback method will register the metaboxes that we need, attached to a spec // register bottom box add_meta_box( 'dx_bottom_meta_box', - __( "DX Bottom Box", 'dxbase' ), + __( "DX Bottom Box", DXP_TD ), array( $this, 'dx_bottom_meta_box' ), '' // leave empty quotes as '' if you want it on all custom post add/edit screens or add a post type slug ); @@ -239,7 +241,7 @@ The callback of our `add_meta_box` call includes everything that is to be displa ```php public function dx_side_meta_box( $post, $metabox) { - _e("

Side meta content here

", 'dxbase'); + _e("

Side meta content here

", DXP_TD); // Add some test data here - a custom field, that is $dx_test_input = ''; @@ -248,7 +250,7 @@ The callback of our `add_meta_box` call includes everything that is to be displa $dx_test_input = get_post_meta( $post->ID, 'dx_test_input', true ); } ?> - + path_include . 'dx-sample-widget.class.php'; } ``` @@ -353,7 +355,7 @@ Arguments are available in the `$attr` array - the first argument of the callbac /* * Manage the attributes and the content as per your request and return the result */ - return __( 'Sample Output', 'dxbase'); + return __( 'Sample Output', DXP_TD); } ``` diff --git a/assets/css/help-page.css b/assets/css/help-page.css new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/assets/css/help-page.css @@ -0,0 +1 @@ + diff --git a/assets/css/index.php b/assets/css/index.php new file mode 100644 index 0000000..6220032 --- /dev/null +++ b/assets/css/index.php @@ -0,0 +1,2 @@ +setup(); + + // add script and style calls the WP way + // it's a bit confusing as styles are called with a scripts hook + // @blamenacin - http://make.wordpress.org/core/2011/12/12/use-wp_enqueue_scripts-not-wp_print_styles-to-enqueue-scripts-and-styles-for-the-frontend/ + add_action( 'wp_enqueue_scripts', array( $this, 'dx_add_JS' ) ); + add_action( 'wp_enqueue_scripts', array( $this, 'dx_add_CSS' ) ); + + // add scripts and styles only available in admin add_action( 'admin_enqueue_scripts', array( $this, 'dx_add_admin_JS' ) ); - add_action( 'admin_enqueue_scripts', array( $this, 'dx_add_admin_CSS' ) ); - - // register admin pages for the plugin - add_action( 'admin_menu', array( $this, 'dx_admin_pages_callback' ) ); - - // register meta boxes for Pages (could be replicated for posts and custom post types) - add_action( 'add_meta_boxes', array( $this, 'dx_meta_boxes_callback' ) ); - - // register save_post hooks for saving the custom fields - add_action( 'save_post', array( $this, 'dx_save_sample_field' ) ); - - // Register custom post types and taxonomies - add_action( 'init', array( $this, 'dx_custom_post_types_callback' ), 5 ); - add_action( 'init', array( $this, 'dx_custom_taxonomies_callback' ), 6 ); - - // Register activation and deactivation hooks - register_activation_hook( __FILE__, 'dx_on_activate_callback' ); - register_deactivation_hook( __FILE__, 'dx_on_deactivate_callback' ); - - // Translation-ready - add_action( 'plugins_loaded', array( $this, 'dx_add_textdomain' ) ); - - // Add earlier execution as it needs to occur before admin page display - add_action( 'admin_init', array( $this, 'dx_register_settings' ), 5 ); - - // Add a sample shortcode - add_action( 'init', array( $this, 'dx_sample_shortcode' ) ); - - // Add a sample widget - add_action( 'widgets_init', array( $this, 'dx_sample_widget' ) ); - - /* - * TODO: - * template_redirect - */ - - // Add actions for storing value and fetching URL - // use the wp_ajax_nopriv_ hook for non-logged users (handle guest actions) - add_action( 'wp_ajax_store_ajax_value', array( $this, 'store_ajax_value' ) ); - add_action( 'wp_ajax_fetch_ajax_url_http', array( $this, 'fetch_ajax_url_http' ) ); - - } - - /** - * - * Adding JavaScript scripts - * - * Loading existing scripts from wp-includes or adding custom ones - * - */ - public function dx_add_JS() { - wp_enqueue_script( 'jquery' ); - // load custom JSes and put them in footer - wp_register_script( 'samplescript', plugins_url( '/js/samplescript.js' , __FILE__ ), array('jquery'), '1.0', true ); - wp_enqueue_script( 'samplescript' ); - } - - + add_action( 'admin_enqueue_scripts', array( $this, 'dx_add_admin_CSS' ) ); + // register admin pages for the plugin + add_action( 'admin_menu', array( $this, 'dx_admin_pages_callback' ) ); + + // register meta boxes for Pages (could be replicated for posts and custom post types) + add_action( 'add_meta_boxes', array( $this, 'dx_meta_boxes_callback' ) ); + + // register save_post hooks for saving the custom fields + add_action( 'save_post', array( $this, 'dx_save_sample_field' ) ); + + // Register custom post types and taxonomies + add_action( 'init', array( $this, 'dx_custom_post_types_callback' ), 5 ); + add_action( 'init', array( $this, 'dx_custom_taxonomies_callback' ), 6 ); + + // Register activation and deactivation hooks + register_activation_hook( __FILE__, 'dx_on_activate_callback' ); + register_deactivation_hook( __FILE__, 'dx_on_deactivate_callback' ); + + // Translation-ready + add_action( 'plugins_loaded', array( $this, 'dx_add_textdomain' ) ); + + // Add earlier execution as it needs to occur before admin page display + add_action( 'admin_init', array( $this, 'dx_register_settings' ), 5 ); + + // Add a sample shortcode + add_action( 'init', array( $this, 'dx_sample_shortcode' ) ); + + // Add a sample widget + add_action( 'widgets_init', array( $this, 'dx_sample_widget' ) ); + + /* + * TODO: + * template_redirect + */ + + // Add actions for storing value and fetching URL + // use the wp_ajax_nopriv_ hook for non-logged users (handle guest actions) + add_action( 'wp_ajax_store_ajax_value', array( $this, 'store_ajax_value' ) ); + add_action( 'wp_ajax_fetch_ajax_url_http', array( $this, 'fetch_ajax_url_http' ) ); + + } + + /** + * Setup the plugin main properties + */ + protected function setup() { + //check all properties, if they are empty assign data accordingly + if ( empty( $this->version ) ): + $this->version = '1.7'; + endif; + if( empty( $this->path ) ): + $this->path = trailingslashit( dirname( __FILE__ ) ); + endif; + if( empty( $this->path_include ) ): + $this->path_include = trailingslashit( $this->path . 'inc' ); + endif; + if( empty( $this->path_template ) ): + $this->path_template = trailingslashit( $this->path . 'template' ); + endif; + if( empty( $this->url ) ): + $this->url = trailingslashit( plugins_url( '', __FILE__ ) ); + endif; + if( empty( $this->url_assets ) ): + $this->url_assets = trailingslashit( $this->url . 'assets' ); + endif; + } + + /** + * + * Adding JavaScript scripts + * + * Loading existing scripts from wp-includes or adding custom ones + * + */ + public function dx_add_JS() { + wp_enqueue_script( 'jquery' ); + // load custom JSes and put them in footer + wp_register_script( 'samplescript', $this->url_assets . 'js/samplescript.js', array('jquery'), '1.0', true ); + wp_enqueue_script( 'samplescript' ); + } + + /** * * Adding JavaScript scripts for the admin pages only * * Loading existing scripts from wp-includes or adding custom ones * - */ - public function dx_add_admin_JS( $hook ) { - wp_enqueue_script( 'jquery' ); - wp_register_script( 'samplescript-admin', plugins_url( '/js/samplescript-admin.js' , __FILE__ ), array('jquery'), '1.0', true ); - wp_enqueue_script( 'samplescript-admin' ); - } - - /** - * - * Add CSS styles - * - */ - public function dx_add_CSS() { - wp_register_style( 'samplestyle', plugins_url( '/css/samplestyle.css', __FILE__ ), array(), '1.0', 'screen' ); - wp_enqueue_style( 'samplestyle' ); - } - + */ + public function dx_add_admin_JS( $hook ) { + wp_enqueue_script( 'jquery' ); + wp_register_script( 'samplescript-admin', $this->url_assets . 'js/samplescript-admin.js' , array('jquery'), '1.0', true ); + wp_enqueue_script( 'samplescript-admin' ); + } + + /** + * + * Add CSS styles + * + */ + public function dx_add_CSS() { + wp_register_style( 'samplestyle', $this->url_assets . 'css/samplestyle.css', array(), '1.0', 'screen' ); + wp_enqueue_style( 'samplestyle' ); + } + /** * * Add admin CSS styles - available only on admin * - */ - public function dx_add_admin_CSS( $hook ) { - wp_register_style( 'samplestyle-admin', plugins_url( '/css/samplestyle-admin.css', __FILE__ ), array(), '1.0', 'screen' ); - wp_enqueue_style( 'samplestyle-admin' ); - - if( 'toplevel_page_dx-plugin-base' === $hook ) { - wp_register_style('dx_help_page', plugins_url( '/help-page.css', __FILE__ ) ); - wp_enqueue_style('dx_help_page'); - } - } - - /** - * - * Callback for registering pages - * - * This demo registers a custom page for the plugin and a subpage - * - */ - public function dx_admin_pages_callback() { - add_menu_page(__( "Plugin Base Admin", 'dxbase' ), __( "Plugin Base Admin", 'dxbase' ), 'edit_themes', 'dx-plugin-base', array( $this, 'dx_plugin_base' ) ); - add_submenu_page( 'dx-plugin-base', __( "Base Subpage", 'dxbase' ), __( "Base Subpage", 'dxbase' ), 'edit_themes', 'dx-base-subpage', array( $this, 'dx_plugin_subpage' ) ); - add_submenu_page( 'dx-plugin-base', __( "Remote Subpage", 'dxbase' ), __( "Remote Subpage", 'dxbase' ), 'edit_themes', 'dx-remote-subpage', array( $this, 'dx_plugin_side_access_page' ) ); - } - - /** - * - * The content of the base page - * - */ - public function dx_plugin_base() { - include_once( DXP_PATH_INCLUDES . '/base-page-template.php' ); - } - - public function dx_plugin_side_access_page() { - include_once( DXP_PATH_INCLUDES . '/remote-page-template.php' ); - } - - /** - * - * The content of the subpage - * - * Use some default UI from WordPress guidelines echoed here (the sample above is with a template) - * - * @see http://www.onextrapixel.com/2009/07/01/how-to-design-and-style-your-wordpress-plugin-admin-panel/ - * - */ - public function dx_plugin_subpage() { - echo '
'; - _e( "

DX Plugin Subpage

", 'dxbase' ); - _e( "I'm a subpage and I know it!", 'dxbase' ); - echo '
'; - } - - /** - * - * Adding right and bottom meta boxes to Pages - * - */ - public function dx_meta_boxes_callback() { - // register side box - add_meta_box( - 'dx_side_meta_box', - __( "DX Side Box", 'dxbase' ), - array( $this, 'dx_side_meta_box' ), - 'pluginbase', // leave empty quotes as '' if you want it on all custom post add/edit screens - 'side', - 'high' - ); - - // register bottom box - add_meta_box( - 'dx_bottom_meta_box', - __( "DX Bottom Box", 'dxbase' ), - array( $this, 'dx_bottom_meta_box' ), - '' // leave empty quotes as '' if you want it on all custom post add/edit screens or add a post type slug - ); - } - - /** - * - * Init right side meta box here - * @param post $post the post object of the given page - * @param metabox $metabox metabox data - */ - public function dx_side_meta_box( $post, $metabox) { - _e("

Side meta content here

", 'dxbase'); - - // Add some test data here - a custom field, that is - $dx_test_input = ''; - if ( ! empty ( $post ) ) { - // Read the database record if we've saved that before - $dx_test_input = get_post_meta( $post->ID, 'dx_test_input', true ); - } - ?> - - - Bottom meta content here

", 'dxbase' ); - } - - /** - * Register custom post types - * - */ - public function dx_custom_post_types_callback() { - register_post_type( 'pluginbase', array( - 'labels' => array( - 'name' => __("Base Items", 'dxbase'), - 'singular_name' => __("Base Item", 'dxbase'), - 'add_new' => _x("Add New", 'pluginbase', 'dxbase' ), - 'add_new_item' => __("Add New Base Item", 'dxbase' ), - 'edit_item' => __("Edit Base Item", 'dxbase' ), - 'new_item' => __("New Base Item", 'dxbase' ), - 'view_item' => __("View Base Item", 'dxbase' ), - 'search_items' => __("Search Base Items", 'dxbase' ), - 'not_found' => __("No base items found", 'dxbase' ), - 'not_found_in_trash' => __("No base items found in Trash", 'dxbase' ), - ), - 'description' => __("Base Items for the demo", 'dxbase'), - 'public' => true, - 'publicly_queryable' => true, - 'query_var' => true, - 'rewrite' => true, - 'exclude_from_search' => true, - 'show_ui' => true, - 'show_in_menu' => true, - 'menu_position' => 40, // probably have to change, many plugins use this - 'supports' => array( - 'title', - 'editor', - 'thumbnail', - 'custom-fields', - 'page-attributes', - ), - 'taxonomies' => array( 'post_tag' ) - )); - } - - - /** - * Register custom taxonomies - * - */ - public function dx_custom_taxonomies_callback() { - register_taxonomy( 'pluginbase_taxonomy', 'pluginbase', array( - 'hierarchical' => true, - 'labels' => array( - 'name' => _x( "Base Item Taxonomies", 'taxonomy general name', 'dxbase' ), - 'singular_name' => _x( "Base Item Taxonomy", 'taxonomy singular name', 'dxbase' ), - 'search_items' => __( "Search Taxonomies", 'dxbase' ), - 'popular_items' => __( "Popular Taxonomies", 'dxbase' ), - 'all_items' => __( "All Taxonomies", 'dxbase' ), - 'parent_item' => null, - 'parent_item_colon' => null, - 'edit_item' => __( "Edit Base Item Taxonomy", 'dxbase' ), - 'update_item' => __( "Update Base Item Taxonomy", 'dxbase' ), - 'add_new_item' => __( "Add New Base Item Taxonomy", 'dxbase' ), - 'new_item_name' => __( "New Base Item Taxonomy Name", 'dxbase' ), - 'separate_items_with_commas' => __( "Separate Base Item taxonomies with commas", 'dxbase' ), - 'add_or_remove_items' => __( "Add or remove Base Item taxonomy", 'dxbase' ), - 'choose_from_most_used' => __( "Choose from the most used Base Item taxonomies", 'dxbase' ) - ), - 'show_ui' => true, - 'query_var' => true, - 'rewrite' => true, - )); - - register_taxonomy_for_object_type( 'pluginbase_taxonomy', 'pluginbase' ); - } - - /** - * Initialize the Settings class - * - * Register a settings section with a field for a secure WordPress admin option creation. - * - */ - public function dx_register_settings() { - require_once( DXP_PATH . '/dx-plugin-settings.class.php' ); - new DX_Plugin_Settings(); - } - - /** - * Register a sample shortcode to be used - * - * First parameter is the shortcode name, would be used like: [dxsampcode] - * - */ - public function dx_sample_shortcode() { - add_shortcode( 'dxsampcode', array( $this, 'dx_sample_shortcode_body' ) ); - } - - /** - * Returns the content of the sample shortcode, like [dxsamplcode] - * @param array $attr arguments passed to array, like [dxsamcode attr1="one" attr2="two"] - * @param string $content optional, could be used for a content to be wrapped, such as [dxsamcode]somecontnet[/dxsamcode] - */ - public function dx_sample_shortcode_body( $attr, $content = null ) { - /* - * Manage the attributes and the content as per your request and return the result - */ - return __( 'Sample Output', 'dxbase'); - } - - /** - * Hook for including a sample widget with options - */ - public function dx_sample_widget() { - include_once DXP_PATH_INCLUDES . '/dx-sample-widget.class.php'; - } - - /** - * Add textdomain for plugin - */ - public function dx_add_textdomain() { - load_plugin_textdomain( 'dxbase', false, dirname( plugin_basename( __FILE__ ) ) . '/lang/' ); - } - - /** - * Callback for saving a simple AJAX option with no page reload - */ - public function store_ajax_value() { - if( isset( $_POST['data'] ) && isset( $_POST['data']['dx_option_from_ajax'] ) ) { - update_option( 'dx_option_from_ajax' , $_POST['data']['dx_option_from_ajax'] ); - } - die(); - } - - /** - * Callback for getting a URL and fetching it's content in the admin page - */ - public function fetch_ajax_url_http() { + */ + public function dx_add_admin_CSS( $hook ) { + wp_register_style( 'samplestyle-admin', $this->url_assets . 'css/samplestyle-admin.css', array(), '1.0', 'screen' ); + wp_enqueue_style( 'samplestyle-admin' ); + + if( 'toplevel_page_dx-plugin-base' === $hook ) { + wp_register_style('dx_help_page', $this->url_assets . 'css/help-page.css' ); + wp_enqueue_style('dx_help_page'); + } + } + + /** + * + * Callback for registering pages + * + * This demo registers a custom page for the plugin and a subpage + * + */ + public function dx_admin_pages_callback() { + add_menu_page(__( "Plugin Base Admin", DXP_TD ), __( "Plugin Base Admin", DXP_TD ), 'edit_themes', 'dx-plugin-base', array( $this, 'dx_plugin_base' ) ); + add_submenu_page( 'dx-plugin-base', __( "Base Subpage", DXP_TD ), __( "Base Subpage", DXP_TD ), 'edit_themes', 'dx-base-subpage', array( $this, 'dx_plugin_subpage' ) ); + add_submenu_page( 'dx-plugin-base', __( "Remote Subpage", DXP_TD ), __( "Remote Subpage", DXP_TD ), 'edit_themes', 'dx-remote-subpage', array( $this, 'dx_plugin_side_access_page' ) ); + } + + /** + * + * The content of the base page + * + */ + public function dx_plugin_base() { + include_once( $this->path_template . 'base-page-template.php' ); + } + + public function dx_plugin_side_access_page() { + include_once( $this->path_template . 'remote-page-template.php' ); + } + + /** + * + * The content of the subpage + * + * Use some default UI from WordPress guidelines echoed here (the sample above is with a template) + * + * @see http://www.onextrapixel.com/2009/07/01/how-to-design-and-style-your-wordpress-plugin-admin-panel/ + * + */ + public function dx_plugin_subpage() { + echo '
'; + _e( "

DX Plugin Subpage

", DXP_TD ); + _e( "I'm a subpage and I know it!", DXP_TD ); + echo '
'; + } + + /** + * + * Adding right and bottom meta boxes to Pages + * + */ + public function dx_meta_boxes_callback() { + // register side box + add_meta_box( + 'dx_side_meta_box', + __( "DX Side Box", DXP_TD ), + array( $this, 'dx_side_meta_box' ), + 'pluginbase', // leave empty quotes as '' if you want it on all custom post add/edit screens + 'side', + 'high' + ); + + // register bottom box + add_meta_box( + 'dx_bottom_meta_box', + __( "DX Bottom Box", DXP_TD ), + array( $this, 'dx_bottom_meta_box' ), + '' // leave empty quotes as '' if you want it on all custom post add/edit screens or add a post type slug + ); + } + + /** + * + * Init right side meta box here + * @param post $post the post object of the given page + * @param metabox $metabox metabox data + */ + public function dx_side_meta_box( $post, $metabox) { + _e("

Side meta content here

", DXP_TD); + + // Add some test data here - a custom field, that is + $dx_test_input = ''; + if ( ! empty ( $post ) ) { + // Read the database record if we've saved that before + $dx_test_input = get_post_meta( $post->ID, 'dx_test_input', true ); + } + ?> + + + Bottom meta content here

", DXP_TD ); + } + + /** + * Register custom post types + * + */ + public function dx_custom_post_types_callback() { + register_post_type( 'pluginbase', array( + 'labels' => array( + 'name' => __("Base Items", DXP_TD), + 'singular_name' => __("Base Item", DXP_TD), + 'add_new' => _x("Add New", 'pluginbase', DXP_TD ), + 'add_new_item' => __("Add New Base Item", DXP_TD ), + 'edit_item' => __("Edit Base Item", DXP_TD ), + 'new_item' => __("New Base Item", DXP_TD ), + 'view_item' => __("View Base Item", DXP_TD ), + 'search_items' => __("Search Base Items", DXP_TD ), + 'not_found' => __("No base items found", DXP_TD ), + 'not_found_in_trash' => __("No base items found in Trash", DXP_TD ), + ), + 'description' => __("Base Items for the demo", DXP_TD), + 'public' => true, + 'publicly_queryable' => true, + 'query_var' => true, + 'rewrite' => true, + 'exclude_from_search' => true, + 'show_ui' => true, + 'show_in_menu' => true, + 'menu_position' => 40, // probably have to change, many plugins use this + 'supports' => array( + 'title', + 'editor', + 'thumbnail', + 'custom-fields', + 'page-attributes', + ), + 'taxonomies' => array( 'post_tag' ) + )); + } + + + /** + * Register custom taxonomies + * + */ + public function dx_custom_taxonomies_callback() { + register_taxonomy( 'pluginbase_taxonomy', 'pluginbase', array( + 'hierarchical' => true, + 'labels' => array( + 'name' => _x( "Base Item Taxonomies", 'taxonomy general name', DXP_TD ), + 'singular_name' => _x( "Base Item Taxonomy", 'taxonomy singular name', DXP_TD ), + 'search_items' => __( "Search Taxonomies", DXP_TD ), + 'popular_items' => __( "Popular Taxonomies", DXP_TD ), + 'all_items' => __( "All Taxonomies", DXP_TD ), + 'parent_item' => null, + 'parent_item_colon' => null, + 'edit_item' => __( "Edit Base Item Taxonomy", DXP_TD ), + 'update_item' => __( "Update Base Item Taxonomy", DXP_TD ), + 'add_new_item' => __( "Add New Base Item Taxonomy", DXP_TD ), + 'new_item_name' => __( "New Base Item Taxonomy Name", DXP_TD ), + 'separate_items_with_commas' => __( "Separate Base Item taxonomies with commas", DXP_TD ), + 'add_or_remove_items' => __( "Add or remove Base Item taxonomy", DXP_TD ), + 'choose_from_most_used' => __( "Choose from the most used Base Item taxonomies", DXP_TD ) + ), + 'show_ui' => true, + 'query_var' => true, + 'rewrite' => true, + )); + + register_taxonomy_for_object_type( 'pluginbase_taxonomy', 'pluginbase' ); + } + + /** + * Initialize the Settings class + * + * Register a settings section with a field for a secure WordPress admin option creation. + * + */ + public function dx_register_settings() { + require_once( $this->path_include . '/class-dx-plugin-settings.php' ); + new DX_Plugin_Settings(); + } + + /** + * Register a sample shortcode to be used + * + * First parameter is the shortcode name, would be used like: [dxsampcode] + * + */ + public function dx_sample_shortcode() { + add_shortcode( 'dxsampcode', array( $this, 'dx_sample_shortcode_body' ) ); + } + + /** + * Returns the content of the sample shortcode, like [dxsamplcode] + * @param array $attr arguments passed to array, like [dxsamcode attr1="one" attr2="two"] + * @param string $content optional, could be used for a content to be wrapped, such as [dxsamcode]somecontnet[/dxsamcode] + */ + public function dx_sample_shortcode_body( $attr, $content = null ) { + /* + * Manage the attributes and the content as per your request and return the result + */ + return __( 'Sample Output', DXP_TD); + } + + /** + * Hook for including a sample widget with options + */ + public function dx_sample_widget() { + include_once $this->path_include . 'dx-sample-widget.class.php'; + } + + /** + * Add textdomain for plugin + */ + public function dx_add_textdomain() { + load_plugin_textdomain( DXP_TD, false, dirname( plugin_basename( __FILE__ ) ) . '/lang/' ); + } + + /** + * Callback for saving a simple AJAX option with no page reload + */ + public function store_ajax_value() { + if( isset( $_POST['data'] ) && isset( $_POST['data']['dx_option_from_ajax'] ) ) { + update_option( 'dx_option_from_ajax' , $_POST['data']['dx_option_from_ajax'] ); + } + die(); + } + + /** + * Callback for getting a URL and fetching it's content in the admin page + */ + public function fetch_ajax_url_http() { if( isset( $_POST['data'] ) && isset( $_POST['data']['dx_url_for_ajax'] ) ) { - $ajax_url = $_POST['data']['dx_url_for_ajax']; - - $response = wp_remote_get( $ajax_url ); - - if( is_wp_error( $response ) ) { - echo json_encode( __( 'Invalid HTTP resource', 'dxbase' ) ); - die(); - } - - if( isset( $response['body'] ) ) { - if( preg_match( '/(.*)<\/title>/', $response['body'], $matches ) ) { - echo json_encode( $matches[1] ); - die(); - } + $ajax_url = $_POST['data']['dx_url_for_ajax']; + + $response = wp_remote_get( $ajax_url ); + + if( is_wp_error( $response ) ) { + echo json_encode( __( 'Invalid HTTP resource', DXP_TD ) ); + die(); + } + + if( isset( $response['body'] ) ) { + if( preg_match( '/<title>(.*)<\/title>/', $response['body'], $matches ) ) { + echo json_encode( $matches[1] ); + die(); + } } - } - echo json_encode( __( 'No title found or site was not fetched properly', 'dxbase' ) ); - die(); - } - -} - + } + echo json_encode( __( 'No title found or site was not fetched properly', DXP_TD ) ); + die(); + } + +} + /** * Register activation hook @@ -448,7 +503,7 @@ function dx_on_activate_callback() { */ function dx_on_deactivate_callback() { // do something when deactivated -} - -// Initialize everything -$dx_plugin_base = new DX_Plugin_Base(); +} + +// Initialize everything +$dx_plugin_base = new DX_Plugin_Base(); diff --git a/dx-plugin-settings.class.php b/inc/class-dx-plugin-settings.php similarity index 89% rename from dx-plugin-settings.class.php rename to inc/class-dx-plugin-settings.php index ca2485d..80daa15 100755 --- a/dx-plugin-settings.class.php +++ b/inc/class-dx-plugin-settings.php @@ -25,14 +25,14 @@ public function register_settings() { add_settings_section( 'dx_settings_section', // ID used to identify this section and with which to register options - __( "Enable DX Templates", 'dxbase' ), // Title to be displayed on the administration page + __( "Enable DX Templates", DXP_TD ), // Title to be displayed on the administration page array($this, 'dx_settings_callback'), // Callback used to render the description of the section 'dx-plugin-base' // Page on which to add this section of options ); add_settings_field( 'dx_opt_in', // ID used to identify the field throughout the theme - __( "Active: ", 'dxbase' ), // The label to the left of the option interface element + __( "Active: ", DXP_TD ), // The label to the left of the option interface element array( $this, 'dx_opt_in_callback' ), // The name of the function responsible for rendering the option interface 'dx-plugin-base', // The page on which this option will be displayed 'dx_settings_section' // The name of the section to which this field belongs @@ -40,7 +40,7 @@ public function register_settings() { add_settings_field( 'dx_sample_text', // ID used to identify the field throughout the theme - __( "DX Sample: ", 'dxbase' ), // The label to the left of the option interface element + __( "DX Sample: ", DXP_TD ), // The label to the left of the option interface element array( $this, 'dx_sample_text_callback' ), // The name of the function responsible for rendering the option interface 'dx-plugin-base', // The page on which this option will be displayed 'dx_settings_section' // The name of the section to which this field belongs @@ -48,7 +48,7 @@ public function register_settings() { } public function dx_settings_callback() { - echo _e( "Enable me", 'dxbase' ); + echo _e( "Enable me", DXP_TD ); } public function dx_opt_in_callback() { diff --git a/inc/dx-sample-widget.class.php b/inc/dx-sample-widget.class.php index 2773644..d39af08 100755 --- a/inc/dx-sample-widget.class.php +++ b/inc/dx-sample-widget.class.php @@ -15,8 +15,8 @@ class DX_Sample_Widget extends WP_Widget { public function __construct() { parent::__construct( 'dx_sample_widget', - __("DX Sample Widget", 'dxbase'), - array( 'classname' => 'dx_widget_sample_single', 'description' => __( "Display a sample DX Widget", 'dxbase' ) ), + __("DX Sample Widget", DXP_TD), + array( 'classname' => 'dx_widget_sample_single', 'description' => __( "Display a sample DX Widget", DXP_TD ) ), array( ) // you can pass width/height as parameters with values here ); } @@ -86,18 +86,18 @@ public function form ( $instance ) { $sample_dropdown = esc_attr( $instance[ 'sample_dropdown' ] ); ?> - <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e( "Title:", 'dxbase'); ?></label> <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" /></p> - <p><label for="<?php echo $this->get_field_id('sample_text'); ?>"><?php _e( "Sample Text:", 'dxbase'); ?></label> <input class="widefat" id="<?php echo $this->get_field_id('sample_text'); ?>" name="<?php echo $this->get_field_name('sample_text'); ?>" type="text" value="<?php echo $sample_text; ?>" /></p> + <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e( "Title:", DXP_TD); ?></label> <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" /></p> + <p><label for="<?php echo $this->get_field_id('sample_text'); ?>"><?php _e( "Sample Text:", DXP_TD); ?></label> <input class="widefat" id="<?php echo $this->get_field_id('sample_text'); ?>" name="<?php echo $this->get_field_name('sample_text'); ?>" type="text" value="<?php echo $sample_text; ?>" /></p> <p> - <label for="<?php echo $this->get_field_id('sample_dropdown'); ?>"><?php _e( "Areas:", 'dxbase' ); ?></label> + <label for="<?php echo $this->get_field_id('sample_dropdown'); ?>"><?php _e( "Areas:", DXP_TD ); ?></label> <select name="<?php echo $this->get_field_name('sample_dropdown'); ?>" id="<?php echo $this->get_field_id('sample_dropdown'); ?>" class="widefat"> - <option value="asia"<?php selected( $instance['sample_dropdown'], 'asia' ); ?>><?php _e( "Asia", 'dxbase' ); ?></option> - <option value="africa"<?php selected( $instance['sample_dropdown'], 'africa' ); ?>><?php _e( "Africa", 'dxbase' ); ?></option> - <option value="north_america"<?php selected( $instance['sample_dropdown'], 'north_america' ); ?>><?php _e( "North America", 'dxbase' ); ?></option> - <option value="south_america"<?php selected( $instance['sample_dropdown'], 'south_america' ); ?>><?php _e( "South America", 'dxbase' ); ?></option> - <option value="antarctica"<?php selected( $instance['sample_dropdown'], 'antarctica' ); ?>><?php _e( "Antarctica", 'dxbase' ); ?></option> - <option value="europe"<?php selected( $instance['sample_dropdown'], 'europe' ); ?>><?php _e( "Europe", 'dxbase' ); ?></option> - <option value="australia"<?php selected( $instance['sample_dropdown'], 'australia' ); ?>><?php _e( "Australia", 'dxbase' ); ?></option> + <option value="asia"<?php selected( $instance['sample_dropdown'], 'asia' ); ?>><?php _e( "Asia", DXP_TD ); ?></option> + <option value="africa"<?php selected( $instance['sample_dropdown'], 'africa' ); ?>><?php _e( "Africa", DXP_TD ); ?></option> + <option value="north_america"<?php selected( $instance['sample_dropdown'], 'north_america' ); ?>><?php _e( "North America", DXP_TD ); ?></option> + <option value="south_america"<?php selected( $instance['sample_dropdown'], 'south_america' ); ?>><?php _e( "South America", DXP_TD ); ?></option> + <option value="antarctica"<?php selected( $instance['sample_dropdown'], 'antarctica' ); ?>><?php _e( "Antarctica", DXP_TD ); ?></option> + <option value="europe"<?php selected( $instance['sample_dropdown'], 'europe' ); ?>><?php _e( "Europe", DXP_TD ); ?></option> + <option value="australia"<?php selected( $instance['sample_dropdown'], 'australia' ); ?>><?php _e( "Australia", DXP_TD ); ?></option> </select> </p> <?php diff --git a/inc/index.php b/inc/index.php new file mode 100644 index 0000000..6220032 --- /dev/null +++ b/inc/index.php @@ -0,0 +1,2 @@ +<?php +// Silence is golden. diff --git a/index.php b/index.php new file mode 100644 index 0000000..6220032 --- /dev/null +++ b/index.php @@ -0,0 +1,2 @@ +<?php +// Silence is golden. diff --git a/js/samplescript.js b/js/samplescript.js deleted file mode 100755 index 51e1a51..0000000 --- a/js/samplescript.js +++ /dev/null @@ -1 +0,0 @@ -/* sample script here */ \ No newline at end of file diff --git a/lang/index.php b/lang/index.php new file mode 100644 index 0000000..6220032 --- /dev/null +++ b/lang/index.php @@ -0,0 +1,2 @@ +<?php +// Silence is golden. diff --git a/inc/base-page-template.php b/template/base-page-template.php similarity index 62% rename from inc/base-page-template.php rename to template/base-page-template.php index d47cfc2..86d27a6 100644 --- a/inc/base-page-template.php +++ b/template/base-page-template.php @@ -1,14 +1,14 @@ <div class="wrap"> <div id="icon-edit" class="icon32 icon32-base-template"><br></div> - <h2><?php _e( "Base plugin page", 'dxbase' ); ?></h2> + <h2><?php _e( "Base plugin page", DXP_TD ); ?></h2> - <p><?php _e( "Sample base plugin page", 'dxbase' ); ?></p> + <p><?php _e( "Sample base plugin page", DXP_TD ); ?></p> <form id="dx-plugin-base-form" action="options.php" method="POST"> <?php settings_fields( 'dx_setting' ) ?> <?php do_settings_sections( 'dx-plugin-base' ) ?> - <input type="submit" value="<?php _e( "Save", 'dxbase' ); ?>" /> + <input type="submit" value="<?php _e( "Save", DXP_TD ); ?>" /> </form> <!-- end of #dxtemplate-form --> </div> \ No newline at end of file diff --git a/template/index.php b/template/index.php new file mode 100644 index 0000000..6220032 --- /dev/null +++ b/template/index.php @@ -0,0 +1,2 @@ +<?php +// Silence is golden. diff --git a/inc/remote-page-template.php b/template/remote-page-template.php similarity index 76% rename from inc/remote-page-template.php rename to template/remote-page-template.php index 2bfcdb8..5149629 100644 --- a/inc/remote-page-template.php +++ b/template/remote-page-template.php @@ -1,26 +1,26 @@ <div class="wrap"> <div id="icon-edit" class="icon32 icon32-base-template"><br></div> - <h2><?php _e( "Remote plugin page", 'dxbase' ); ?></h2> + <h2><?php _e( "Remote plugin page", DXP_TD ); ?></h2> - <p><?php _e( "Performing side activities - AJAX and HTTP fetch", 'dxbase' ); ?></p> + <p><?php _e( "Performing side activities - AJAX and HTTP fetch", DXP_TD ); ?></p> <div id="dx_page_messages"></div> <?php $dx_ajax_value = get_option( 'dx_option_from_ajax', '' ); ?> - <h3><?php _e( 'Store a Database option with AJAX', 'dxbase' ); ?></h3> + <h3><?php _e( 'Store a Database option with AJAX', DXP_TD ); ?></h3> <form id="dx-plugin-base-ajax-form" action="options.php" method="POST"> <input type="text" id="dx_option_from_ajax" name="dx_option_from_ajax" value="<?php echo $dx_ajax_value; ?>" /> - <input type="submit" value="<?php _e( "Save with AJAX", 'dxbase' ); ?>" /> + <input type="submit" value="<?php _e( "Save with AJAX", DXP_TD ); ?>" /> </form> <!-- end of #dx-plugin-base-ajax-form --> - <h3><?php _e( 'Fetch a title from URL with HTTP call through AJAX', 'dxbase' ); ?></h3> + <h3><?php _e( 'Fetch a title from URL with HTTP call through AJAX', DXP_TD ); ?></h3> <form id="dx-plugin-base-http-form" action="options.php" method="POST"> <input type="text" id="dx_url_for_ajax" name="dx_url_for_ajax" value="http://wordpress.org" /> - <input type="submit" value="<?php _e( "Fetch URL title with AJAX", 'dxbase' ); ?>" /> + <input type="submit" value="<?php _e( "Fetch URL title with AJAX", DXP_TD ); ?>" /> </form> <!-- end of #dx-plugin-base-http-form --> <div id="resource-window"> From 1dcc0edd4673a0e22108ab73c958e1d8d370636e Mon Sep 17 00:00:00 2001 From: MeatMAN <skylineflash@rambler.ru> Date: Wed, 1 Jun 2016 13:29:28 +0300 Subject: [PATCH 02/12] add uninstall hook --- dx-plugin-base.php | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/dx-plugin-base.php b/dx-plugin-base.php index 72f64ab..a04d69e 100755 --- a/dx-plugin-base.php +++ b/dx-plugin-base.php @@ -105,10 +105,6 @@ public function __construct() { add_action( 'init', array( $this, 'dx_custom_post_types_callback' ), 5 ); add_action( 'init', array( $this, 'dx_custom_taxonomies_callback' ), 6 ); - // Register activation and deactivation hooks - register_activation_hook( __FILE__, 'dx_on_activate_callback' ); - register_deactivation_hook( __FILE__, 'dx_on_deactivate_callback' ); - // Translation-ready add_action( 'plugins_loaded', array( $this, 'dx_add_textdomain' ) ); @@ -496,6 +492,7 @@ public function fetch_ajax_url_http() { function dx_on_activate_callback() { // do something on activation } +register_activation_hook( __FILE__, 'dx_on_activate_callback' ); /** * Register deactivation hook @@ -504,6 +501,16 @@ function dx_on_activate_callback() { function dx_on_deactivate_callback() { // do something when deactivated } +register_deactivation_hook( __FILE__, 'dx_on_deactivate_callback' ); + +/** + * Register uninstall hook + * + */ +function dx_on_uninstall_callback(){ + // do something when plugin is uninstalling +} +register_uninstall_hook( __FILE__, 'dx_on_uninstall_callback' ); // Initialize everything $dx_plugin_base = new DX_Plugin_Base(); From a7ea632bced83ae8f5b782d6a5b6153cd57028dd Mon Sep 17 00:00:00 2001 From: MeatMAN <skylineflash@rambler.ru> Date: Wed, 1 Jun 2016 13:55:44 +0300 Subject: [PATCH 03/12] add custom hooks in base page template for example --- dx-plugin-base.php | 25 +++++++++++++++++++++++++ template/base-page-template.php | 5 ++++- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/dx-plugin-base.php b/dx-plugin-base.php index a04d69e..97d38c7 100755 --- a/dx-plugin-base.php +++ b/dx-plugin-base.php @@ -127,6 +127,12 @@ public function __construct() { add_action( 'wp_ajax_store_ajax_value', array( $this, 'store_ajax_value' ) ); add_action( 'wp_ajax_fetch_ajax_url_http', array( $this, 'fetch_ajax_url_http' ) ); + //hook to the base page setting page form , and add content in form before and after the elements + add_action( 'dx_base_page_template_form_before_settings', array( $this, 'base_page_template_form_before' ) ); + add_action( 'dx_base_page_template_form_after_settings', array( $this, 'base_page_template_form_after' ) ); + + //change the to the base page setting page form submit button text (value) + add_filter( 'dx_base_page_template_form_submit_button', array( $this, 'base_page_template_form_submit_button' ) ); } /** @@ -154,6 +160,25 @@ protected function setup() { endif; } + /** + * Adding the content in form before it elemnts + */ + public function base_page_template_form_before() { + _e( 'Sample example, of adding a custom hook. This will add conternt before form elements.', DXP_TD ); + } + + /** + * Adding the content in form after it elemnts + */ + public function base_page_template_form_after() { + _e( 'Sample example, of adding a custom hook. This will add conternt after form elements.', DXP_TD ); + echo '<br>'; + } + + public function base_page_template_form_submit_button( $value ) { + return __( 'Save settings', DXP_TD ); + } + /** * * Adding JavaScript scripts diff --git a/template/base-page-template.php b/template/base-page-template.php index 86d27a6..f29f549 100644 --- a/template/base-page-template.php +++ b/template/base-page-template.php @@ -5,10 +5,13 @@ <p><?php _e( "Sample base plugin page", DXP_TD ); ?></p> <form id="dx-plugin-base-form" action="options.php" method="POST"> + <?php do_action( 'dx_base_page_template_form_before_settings' ) ?> <?php settings_fields( 'dx_setting' ) ?> <?php do_settings_sections( 'dx-plugin-base' ) ?> - <input type="submit" value="<?php _e( "Save", DXP_TD ); ?>" /> + <?php do_action( 'dx_base_page_template_form_after_settings' ) ?> + + <input type="submit" value="<?php echo apply_filters( 'dx_base_page_template_form_submit_button', __( "Save", DXP_TD ) ) ?>" /> </form> <!-- end of #dxtemplate-form --> </div> \ No newline at end of file From e6ac1b2e7a2e09750493fd08035b6effc77808c9 Mon Sep 17 00:00:00 2001 From: MeatMAN <skylineflash@rambler.ru> Date: Wed, 1 Jun 2016 14:00:20 +0300 Subject: [PATCH 04/12] file are not used --- samplescript.js | 1 - samplestyle.css | 3 --- 2 files changed, 4 deletions(-) delete mode 100755 samplescript.js delete mode 100755 samplestyle.css diff --git a/samplescript.js b/samplescript.js deleted file mode 100755 index 51e1a51..0000000 --- a/samplescript.js +++ /dev/null @@ -1 +0,0 @@ -/* sample script here */ \ No newline at end of file diff --git a/samplestyle.css b/samplestyle.css deleted file mode 100755 index c0342a7..0000000 --- a/samplestyle.css +++ /dev/null @@ -1,3 +0,0 @@ -@CHARSET "UTF-8"; - -/* Sample CSS styles below */ \ No newline at end of file From e1537aa44c3a05cb5dee95d20f32fc957ba0db17 Mon Sep 17 00:00:00 2001 From: MeatMAN <skylineflash@rambler.ru> Date: Wed, 1 Jun 2016 14:15:38 +0300 Subject: [PATCH 05/12] add info abot custom hooks in readme --- README.md | 42 ++++++++++++++++++++++++++++++++++++++++++ dx-plugin-base.php | 6 ++++++ 2 files changed, 48 insertions(+) diff --git a/README.md b/README.md index b9335d3..edc5f90 100755 --- a/README.md +++ b/README.md @@ -358,6 +358,48 @@ Arguments are available in the `$attr` array - the first argument of the callbac return __( 'Sample Output', DXP_TD); } ``` +### Creating a Custom Hooks + +There are can be created the custom hooks in plugin code for the access from others plugins, of from it self +It can be done, using the do_action and apply_filters function in needed place by adding: + +```php + <?php do_action( 'dx_base_page_template_form_before_settings' ) ?> +``` + And + +```php + <input type="submit" value="<?php echo apply_filters( 'dx_base_page_template_form_submit_button', __( "Save", DXP_TD ) ) ?>" /> +``` + +Thats hooks can be called from the plugin or theme functions.php. + +For example + +```php + add_action( 'dx_base_page_template_form_before_settings', array( $this, 'base_page_template_form_before' ) ); +``` + +And the callback for the hook + +```php + public function base_page_template_form_before() { + _e( 'Sample example, of adding a custom hook. This will add conternt before form elements.', DXP_TD ); + } +``` + +Also there are can be changed the submit button value (text) + +```php + add_filter( 'dx_base_page_template_form_submit_button', array( $this, 'base_page_template_form_submit_button' ) ); +``` +The callback + +```php + public function base_page_template_form_submit_button( $value ) { + return __( 'Save settings', DXP_TD ); + } +``` ### Fetching AJAX Data Remotely diff --git a/dx-plugin-base.php b/dx-plugin-base.php index 97d38c7..14fabab 100755 --- a/dx-plugin-base.php +++ b/dx-plugin-base.php @@ -175,6 +175,12 @@ public function base_page_template_form_after() { echo '<br>'; } + /** + * Callback for the filter the submit button text in base page template + * + * @param string $value + * @return string The new value of the submit button + */ public function base_page_template_form_submit_button( $value ) { return __( 'Save settings', DXP_TD ); } From 84a83825568a805ccdea0804ef2fde0afa6fe7c5 Mon Sep 17 00:00:00 2001 From: MeatMAN <skylineflash@rambler.ru> Date: Wed, 1 Jun 2016 15:05:06 +0300 Subject: [PATCH 06/12] fix typo --- dx-plugin-base.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/dx-plugin-base.php b/dx-plugin-base.php index 14fabab..0a3ea48 100755 --- a/dx-plugin-base.php +++ b/dx-plugin-base.php @@ -164,14 +164,14 @@ protected function setup() { * Adding the content in form before it elemnts */ public function base_page_template_form_before() { - _e( 'Sample example, of adding a custom hook. This will add conternt before form elements.', DXP_TD ); + _e( 'Sample example, of adding a custom hook. This will add content before form elements.', DXP_TD ); } /** * Adding the content in form after it elemnts */ public function base_page_template_form_after() { - _e( 'Sample example, of adding a custom hook. This will add conternt after form elements.', DXP_TD ); + _e( 'Sample example, of adding a custom hook. This will add content after form elements.', DXP_TD ); echo '<br>'; } From b392ad47b5ed4d0e6a7ded1d9d07125c14916fd1 Mon Sep 17 00:00:00 2001 From: MeatMAN <skylineflash@rambler.ru> Date: Wed, 1 Jun 2016 15:06:38 +0300 Subject: [PATCH 07/12] add check of the user input and the sanitize it --- inc/class-dx-plugin-settings.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/inc/class-dx-plugin-settings.php b/inc/class-dx-plugin-settings.php index 80daa15..9c15ade 100755 --- a/inc/class-dx-plugin-settings.php +++ b/inc/class-dx-plugin-settings.php @@ -107,7 +107,9 @@ public function is_enabled() { * @param array $input */ public function dx_validate_settings( $input ) { - + array_walk_recursive($input, function( &$item, $key ){//senitizy the user input before saving to the DB + $item = esc_attr( $item ); + }); return $input; } } From 2b8252339aa39da83a28f30510795d85675e538e Mon Sep 17 00:00:00 2001 From: MeatMAN <skylineflash@rambler.ru> Date: Wed, 1 Jun 2016 15:36:28 +0300 Subject: [PATCH 08/12] add sample description for input fields --- inc/class-dx-plugin-settings.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/inc/class-dx-plugin-settings.php b/inc/class-dx-plugin-settings.php index 9c15ade..9374c1d 100755 --- a/inc/class-dx-plugin-settings.php +++ b/inc/class-dx-plugin-settings.php @@ -67,6 +67,8 @@ public function dx_opt_in_callback() { $out = '<input type="checkbox" id="dx_opt_in" name="dx_setting[dx_opt_in]" />'; } + $out .= sprintf( '<p>%s</p>', __( 'Cehckbox description text sample.', DXP_TD ) ); + echo $out; } @@ -80,6 +82,7 @@ public function dx_sample_text_callback() { } $out = '<input type="text" id="dx_sample_text" name="dx_setting[dx_sample_text]" value="' . $val . '" />'; + $out .= sprintf( '<p>%s</p>', __( 'Input description text sample.', DXP_TD ) ); echo $out; } From a26eaca86cfb043001b62ff2efda84bff33b67cb Mon Sep 17 00:00:00 2001 From: MeatMAN <skylineflash@rambler.ru> Date: Wed, 1 Jun 2016 18:22:49 +0300 Subject: [PATCH 09/12] add wp_query, wp_user_query and meta query for example Implement , using the hierarchical model of shortcodes --- README.md | 155 ++++++++++++++++++++++++++++++++++++++++++++- dx-plugin-base.php | 139 +++++++++++++++++++++++++++++++++++++++- 2 files changed, 291 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index edc5f90..e7ad9a1 100755 --- a/README.md +++ b/README.md @@ -343,6 +343,14 @@ The shortcode callback can interact with WordPress and all underlying APIs and r [sample_shortcode arg1="first" arg2="second"]content[/sample_shortcode] ``` +Also You can add another shortcodes in shortcode like: + +[dxsampcode] +[dxsampcode_wp_query]Query Title[/dxsampcode_wp_query] +[dxsampcode_wp_user_query]User Query Title[/dxsampcode_wp_user_query] +[dxsampcode_wp_meta_query]Meta Query Title[/dxsampcode_wp_meta_query] +[/dxsampcode] + Arguments are available in the `$attr` array - the first argument of the callback, and a `$content` is also provided if you wrap a chunk of text with your shortcode opening and closing tags. ```php @@ -355,9 +363,154 @@ Arguments are available in the `$attr` array - the first argument of the callbac /* * Manage the attributes and the content as per your request and return the result */ - return __( 'Sample Output', DXP_TD); + ob_start(); + ?> + <div class="dx-sample-shortcode-main-wrap"> + <?php echo do_shortcode( $content );?> + </div> + <?php + + return ob_get_clean(); + } +``` + +The other shortcodes will be rendered in the main one, and return content like post: + +```php + /** + * Returns the latest posts, using WP_Query + * @param array $attr arguments passed to array, like [dxsampcode_wp_query posts_per_page="10" orderby="title"] + * @param string $content optional, could be used for a content to be a title, such as [dxsampcode_wp_query]somecontnet[/dxsampcode_wp_query] + */ + public function dx_sample_shortcode_wp_query_body( $attr, $content = null ) { + /* + * Manage the attributes and the content as per your request and return the result + */ + $current_post = get_the_ID(); + $default = array( + 'post_type' => 'post', + 'orderby' => 'date', + 'order' => 'DESC', + 'posts_per_page' => 3, + 'post__not_in' => array( $current_post ), + ); + $attr = wp_parse_args( $attr, $default ); + $wp_query = new WP_Query( $attr ); + + ob_start(); + ?> + <?php if( $wp_query->have_posts() ): ?> + <div class="dx-sample-shortcode-wp-query"> + <?php if( !empty( $content ) ):?> + <h2><?php echo $content;?></h2> + <?php endif;?> + <?php while ( $wp_query->have_posts() ): ?> + <?php $wp_query->the_post(); ?> + <div class="dx-sample-post"> + <h3 class="dx-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3> + <?php the_content(); ?> + </div> + <?php endwhile; ?> + </div> + <?php wp_reset_query(); ?> + <?php endif; ?> + <?php + return ob_get_clean(); + } +``` + +Or WP_User query : + +```php + /** + * Returns the list of the top Users + * @param array $attr arguments passed to array, like [dxsampcode_wp_user_query number="3" orderby="post_count"] + * @param string $content optional, could be used for a content to be wrapped, such as [dxsampcode_wp_user_query]somecontnet[/dxsampcode_wp_user_query] + */ + public function dx_sample_shortcode_wp_user_query_body( $attr, $content = null ) { + /* + * Manage the attributes and the content as per your request and return the result + */ + $default = array( + 'orderby' => 'post_count', + 'order' => 'DESC', + 'number' => 3,//nubmer of users + ); + $attr = wp_parse_args( $attr, $default ); + $top_users = new WP_User_Query( $attr ); + + ob_start(); + ?> + <?php if( !empty( $top_users->results ) ): ?> + <div class="dx-sample-shortcode-wp-user-query"> + <?php if( !empty( $content ) ):?> + <h2><?php echo $content;?></h2> + <?php endif;?> + <ul class="dx-sample--top-users"> + <?php foreach($top_users->results as $user ): ?> + <?php $userdata = get_userdata( $user->ID ); ?> + <li><?php echo $user->data->display_name; ?></li> + <?php endforeach; ?> + </ul> + </div> + <?php endif; ?> + <?php + return ob_get_clean(); } ``` + +Or meta query for the post with needed meta value: + +```php + /** + * Returns the posts, that has not empty dx_test_input metabox + * @param array $attr arguments passed to array, like [dxsampcode_wp_meta_query key="one" value="two"] + * @param string $content If not empty will display as title of sectionm, like [dxsampcode_wp_meta_query]somecontnet[/dxsampcode_wp_meta_query] + */ + public function dx_sample_shortcode_wp_meta_query_body( $attr, $content = null ) { + /* + * Manage the attributes and the content as per your request and return the result + */ + + $current_post = get_the_ID(); + $default = array( + 'post_type' => 'pluginbase', + 'orderby' => 'date', + 'order' => 'DESC', + 'posts_per_page' => 10, + 'post__not_in' => array( $current_post ), + 'meta_query' => array( + array( + 'key' => 'dx_test_input', + 'value' => array(''), + 'compare' => 'NOT IN', + ), + ), + ); + $attr = wp_parse_args( $attr, $default ); + $wp_meta_query = new WP_Query( $attr ); + + ob_start(); + ?> + <?php if( $wp_meta_query->have_posts() ): ?> + <div class="dx-sample-shortcode-wp-meta-query"> + <?php if( !empty( $content ) ):?> + <h2><?php echo $content;?></h2> + <?php endif;?> + <?php while( $wp_meta_query->have_posts() ): ?> + <?php $wp_meta_query->the_post(); ?> + <div class="dx-sample-post"> + <h3 class="dx-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3> + <?php the_content(); ?> + </div> + <?php endwhile; ?> + </div> + <?php endif; ?> + <?php + return ob_get_clean(); + } +``` + ### Creating a Custom Hooks There are can be created the custom hooks in plugin code for the access from others plugins, of from it self diff --git a/dx-plugin-base.php b/dx-plugin-base.php index 0a3ea48..3d8c487 100755 --- a/dx-plugin-base.php +++ b/dx-plugin-base.php @@ -450,6 +450,9 @@ public function dx_register_settings() { */ public function dx_sample_shortcode() { add_shortcode( 'dxsampcode', array( $this, 'dx_sample_shortcode_body' ) ); + add_shortcode( 'dxsampcode_wp_query', array( $this, 'dx_sample_shortcode_wp_query_body' ) ); + add_shortcode( 'dxsampcode_wp_user_query', array( $this, 'dx_sample_shortcode_wp_user_query_body' ) ); + add_shortcode( 'dxsampcode_wp_meta_query', array( $this, 'dx_sample_shortcode_wp_meta_query_body' ) ); } /** @@ -461,9 +464,141 @@ public function dx_sample_shortcode_body( $attr, $content = null ) { /* * Manage the attributes and the content as per your request and return the result */ - return __( 'Sample Output', DXP_TD); + ob_start(); + ?> + <div class="dx-sample-shortcode-main-wrap"> + <?php echo do_shortcode( $content );?> + </div> + <?php + + return ob_get_clean(); + } + + /** + * Returns the latest posts, using WP_Query + * @param array $attr arguments passed to array, like [dxsampcode_wp_query posts_per_page="10" orderby="title"] + * @param string $content optional, could be used for a content to be a title, such as [dxsampcode_wp_query]somecontnet[/dxsampcode_wp_query] + */ + public function dx_sample_shortcode_wp_query_body( $attr, $content = null ) { + /* + * Manage the attributes and the content as per your request and return the result + */ + $current_post = get_the_ID(); + $default = array( + 'post_type' => 'post', + 'orderby' => 'date', + 'order' => 'DESC', + 'posts_per_page' => 3, + 'post__not_in' => array( $current_post ), + ); + $attr = wp_parse_args( $attr, $default ); + $wp_query = new WP_Query( $attr ); + + ob_start(); + ?> + <?php if( $wp_query->have_posts() ): ?> + <div class="dx-sample-shortcode-wp-query"> + <?php if( !empty( $content ) ):?> + <h2><?php echo $content;?></h2> + <?php endif;?> + <?php while ( $wp_query->have_posts() ): ?> + <?php $wp_query->the_post(); ?> + <div class="dx-sample-post"> + <h3 class="dx-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3> + <?php the_content(); ?> + </div> + <?php endwhile; ?> + </div> + <?php wp_reset_query(); ?> + <?php endif; ?> + <?php + return ob_get_clean(); + } + + /** + * Returns the list of the top Users + * @param array $attr arguments passed to array, like [dxsampcode_wp_user_query number="3" orderby="post_count"] + * @param string $content optional, could be used for a content to be wrapped, such as [dxsampcode_wp_user_query]somecontnet[/dxsampcode_wp_user_query] + */ + public function dx_sample_shortcode_wp_user_query_body( $attr, $content = null ) { + /* + * Manage the attributes and the content as per your request and return the result + */ + $default = array( + 'orderby' => 'post_count', + 'order' => 'DESC', + 'number' => 3,//nubmer of users + ); + $attr = wp_parse_args( $attr, $default ); + $top_users = new WP_User_Query( $attr ); + + ob_start(); + ?> + <?php if( !empty( $top_users->results ) ): ?> + <div class="dx-sample-shortcode-wp-user-query"> + <?php if( !empty( $content ) ):?> + <h2><?php echo $content;?></h2> + <?php endif;?> + <ul class="dx-sample--top-users"> + <?php foreach($top_users->results as $user ): ?> + <?php $userdata = get_userdata( $user->ID ); ?> + <li><?php echo $user->data->display_name; ?></li> + <?php endforeach; ?> + </ul> + </div> + <?php endif; ?> + <?php + return ob_get_clean(); } + /** + * Returns the posts, that has not empty dx_test_input metabox + * @param array $attr arguments passed to array, like [dxsampcode_wp_meta_query key="one" value="two"] + * @param string $content If not empty will display as title of sectionm, like [dxsampcode_wp_meta_query]somecontnet[/dxsampcode_wp_meta_query] + */ + public function dx_sample_shortcode_wp_meta_query_body( $attr, $content = null ) { + /* + * Manage the attributes and the content as per your request and return the result + */ + + $current_post = get_the_ID(); + $default = array( + 'post_type' => 'pluginbase', + 'orderby' => 'date', + 'order' => 'DESC', + 'posts_per_page' => 10, + 'post__not_in' => array( $current_post ), + 'meta_query' => array( + array( + 'key' => 'dx_test_input', + 'value' => array(''), + 'compare' => 'NOT IN', + ), + ), + ); + $attr = wp_parse_args( $attr, $default ); + $wp_meta_query = new WP_Query( $attr ); + + ob_start(); + ?> + <?php if( $wp_meta_query->have_posts() ): ?> + <div class="dx-sample-shortcode-wp-meta-query"> + <?php if( !empty( $content ) ):?> + <h2><?php echo $content;?></h2> + <?php endif;?> + <?php while( $wp_meta_query->have_posts() ): ?> + <?php $wp_meta_query->the_post(); ?> + <div class="dx-sample-post"> + <h3 class="dx-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3> + <?php the_content(); ?> + </div> + <?php endwhile; ?> + </div> + <?php endif; ?> + <?php + return ob_get_clean(); + } + /** * Hook for including a sample widget with options */ @@ -544,4 +679,4 @@ function dx_on_uninstall_callback(){ register_uninstall_hook( __FILE__, 'dx_on_uninstall_callback' ); // Initialize everything -$dx_plugin_base = new DX_Plugin_Base(); +$dx_plugin_base = new DX_Plugin_Base(); \ No newline at end of file From 16d8c8f89e9dca956913ec698ced7a59c87e8f42 Mon Sep 17 00:00:00 2001 From: MeatMAN <skylineflash@rambler.ru> Date: Wed, 1 Jun 2016 18:32:22 +0300 Subject: [PATCH 10/12] update readme txt --- readme.txt | 155 ++++++++++++++++++++++++++++------------------------- 1 file changed, 83 insertions(+), 72 deletions(-) diff --git a/readme.txt b/readme.txt index a7bdfac..a7b3b90 100755 --- a/readme.txt +++ b/readme.txt @@ -1,72 +1,83 @@ -=== DX Plugin Base === -Contributors: nofearinc, devrix -Tags: plugin, base, startup, framework, foundation, skeleton, backbone, starter, sample, example -Requires at least: 3.1 -Tested up to: 4.3 -Stable tag: 1.6 -License: GPLv2 or later - -Startup plugin code for new plugin, including the archetype of standard features, admin and core functions to be used in new plugins. - -== Description == - -This is a fully-functional sample skeleton plugin for plugin developers. - -[youtube https://www.youtube.com/watch?v=FfQpGD_MUbk] - -It serves as a startup code providing reference and working codebase for: - -* defining custom post types and taxonomies -* creating admin pages -* sample code of the Settings API implementation for admin pages -* registration of activate/deactivate hooks -* adding metaboxes on pages -* creating sample shortcode -* creating sample widget -* creating AJAX requests storing sample data in the database -* creating AJAX requests for fetching remote data with the HTTP API -* adding frontend styles/scripts the right way -* adding admin styles/scripts the right way -* defining common constants for further use - -and more. **Use it freely as your plugin startup snippet**. - -Take the code as is for test and learning or use it when creating plugins for a solid base. To be extended with -widget and shortcode samples. - -== Installation == - -Upload the DX Plugin Base plugin to your blog and activate it. It would work as is. - -Extend or comment whenever appropriate based on your assignment. - -== FAQ == - -= Is it compatible with latest WordPress? = - -Yes, it is, as well as with the latest PHP. - -I've removed the 'pass-by-reference' call for all array( $this, ... ) entries as it's deprecated since 5.3.0. If you happen to use 5.2.4, you can replace all $this in arrays with &$this or better update PHP version. - -== Changelog == - -= 1.6 = -* Widget update for PHP compatibility with 4.3 - -= 1.5 = -* Add custom fields to metaboxes -* Save custom fields - -= 1.4 = -* Refreshing - -= 1.3 = -* Add page with AJAX options -* Introduce AJAX call for storing data to DB -* Introduce AJAX call to fetch title from remote HTTP resouce - -= 1.2 = -* Fix small issues and prevent some unexpected activities - -= 1.0 = -* Initial commit +=== DX Plugin Base === +Contributors: nofearinc, devrix +Tags: plugin, base, startup, framework, foundation, skeleton, backbone, starter, sample, example +Requires at least: 3.1 +Tested up to: 4.5.2 +Stable tag: 1.7 +License: GPLv2 or later + +Startup plugin code for new plugin, including the archetype of standard features, admin and core functions to be used in new plugins. + +== Description == + +This is a fully-functional sample skeleton plugin for plugin developers. + +[youtube https://www.youtube.com/watch?v=FfQpGD_MUbk] + +It serves as a startup code providing reference and working codebase for: + +* defining custom post types and taxonomies +* creating admin pages +* sample code of the Settings API implementation for admin pages +* registration of activate/deactivate hooks +* adding metaboxes on pages +* creating sample shortcode +* creating sample widget +* creating AJAX requests storing sample data in the database +* creating AJAX requests for fetching remote data with the HTTP API +* adding frontend styles/scripts the right way +* adding admin styles/scripts the right way +* defining common constants for further use + +and more. **Use it freely as your plugin startup snippet**. + +Take the code as is for test and learning or use it when creating plugins for a solid base. To be extended with +widget and shortcode samples. + +== Installation == + +Upload the DX Plugin Base plugin to your blog and activate it. It would work as is. + +Extend or comment whenever appropriate based on your assignment. + +== FAQ == + += Is it compatible with latest WordPress? = + +Yes, it is, as well as with the latest PHP. + +I've removed the 'pass-by-reference' call for all array( $this, ... ) entries as it's deprecated since 5.3.0. If you happen to use 5.2.4, you can replace all $this in arrays with &$this or better update PHP version. + +== Changelog == + += 1.7 = +* Add properties with the plugin dir/url to not call every time according functions +* Add WP_Query, WP_User_Query and meta query for example +* Add hierarchical shortcodes example +* Move template files to template folder +* Move css/js files to assets folder +* Sanitizing the user input before save to the DB +* Add example of the custom hooks in to the base page template (action), and the filter for the save button value +* Add uninstall hook +* Add the description example for the inputs in the base page template (settings page) + += 1.6 = +* Widget update for PHP compatibility with 4.3 + += 1.5 = +* Add custom fields to metaboxes +* Save custom fields + += 1.4 = +* Refreshing + += 1.3 = +* Add page with AJAX options +* Introduce AJAX call for storing data to DB +* Introduce AJAX call to fetch title from remote HTTP resouce + += 1.2 = +* Fix small issues and prevent some unexpected activities + += 1.0 = +* Initial commit From 0c6fd32b308a794076424b02b9f1fda1deba9aee Mon Sep 17 00:00:00 2001 From: MeatMAN <skylineflash@rambler.ru> Date: Wed, 1 Jun 2016 18:45:50 +0300 Subject: [PATCH 11/12] update readme md --- README.md | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index e7ad9a1..b2a5175 100755 --- a/README.md +++ b/README.md @@ -345,11 +345,13 @@ The shortcode callback can interact with WordPress and all underlying APIs and r Also You can add another shortcodes in shortcode like: -[dxsampcode] -[dxsampcode_wp_query]Query Title[/dxsampcode_wp_query] -[dxsampcode_wp_user_query]User Query Title[/dxsampcode_wp_user_query] -[dxsampcode_wp_meta_query]Meta Query Title[/dxsampcode_wp_meta_query] -[/dxsampcode] +```php + [dxsampcode] + [dxsampcode_wp_query]Query Title[/dxsampcode_wp_query] + [dxsampcode_wp_user_query]User Query Title[/dxsampcode_wp_user_query] + [dxsampcode_wp_meta_query]Meta Query Title[/dxsampcode_wp_meta_query] + [/dxsampcode] +``` Arguments are available in the `$attr` array - the first argument of the callback, and a `$content` is also provided if you wrap a chunk of text with your shortcode opening and closing tags. From 743e3a33f448825b8e4c50bfca3c35170197cc26 Mon Sep 17 00:00:00 2001 From: MeatMAN <skylineflash@rambler.ru> Date: Mon, 13 Jun 2016 13:27:57 +0300 Subject: [PATCH 12/12] add options page example --- dx-plugin-base.php | 23 ++++++++++++++ inc/class-dx-plugin-settings.php | 54 ++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+) diff --git a/dx-plugin-base.php b/dx-plugin-base.php index 3d8c487..40c36ba 100755 --- a/dx-plugin-base.php +++ b/dx-plugin-base.php @@ -249,6 +249,12 @@ public function dx_admin_pages_callback() { add_menu_page(__( "Plugin Base Admin", DXP_TD ), __( "Plugin Base Admin", DXP_TD ), 'edit_themes', 'dx-plugin-base', array( $this, 'dx_plugin_base' ) ); add_submenu_page( 'dx-plugin-base', __( "Base Subpage", DXP_TD ), __( "Base Subpage", DXP_TD ), 'edit_themes', 'dx-base-subpage', array( $this, 'dx_plugin_subpage' ) ); add_submenu_page( 'dx-plugin-base', __( "Remote Subpage", DXP_TD ), __( "Remote Subpage", DXP_TD ), 'edit_themes', 'dx-remote-subpage', array( $this, 'dx_plugin_side_access_page' ) ); + + /** + * Add options page + * @todo change me + */ + add_options_page( __( 'Options of base plugin', DXP_TD ), __( 'Base Plugin options', DXP_TD ), 'manage_options', 'dx_base_plugin_options', array( $this, 'dx_base_plugin_options_cb' ) ); } /** @@ -263,6 +269,23 @@ public function dx_plugin_base() { public function dx_plugin_side_access_page() { include_once( $this->path_template . 'remote-page-template.php' ); } + + /** + * Callback for the add options page + * + * @since 1.7 + */ + public function dx_base_plugin_options_cb() { + ?> + <form id="dx_options_settings_section" action="options.php" method="post"> + <?php + settings_fields( 'dx_options_setting' ); + do_settings_sections( 'dx_base_plugin_options' ); + submit_button(); + ?> + </form> + <?php + } /** * diff --git a/inc/class-dx-plugin-settings.php b/inc/class-dx-plugin-settings.php index 9374c1d..31fb049 100755 --- a/inc/class-dx-plugin-settings.php +++ b/inc/class-dx-plugin-settings.php @@ -45,6 +45,31 @@ public function register_settings() { 'dx-plugin-base', // The page on which this option will be displayed 'dx_settings_section' // The name of the section to which this field belongs ); + //options page settings + register_setting( 'dx_options_setting', 'dx_options_setting', array( $this, 'dx_options_settings_validate' ) ); + add_settings_section( + 'dx_options_settings_section', // ID used to identify this section and with which to register options + __( "Plugin Base Options", DXP_TD ), // Title to be displayed on the administration page + array( $this, 'dx_options_settings_callback' ), // Callback used to render the description of the section + 'dx_base_plugin_options' // Page on which to add this section of options + ); + + add_settings_field( + 'dx_options_checkbox', // ID used to identify the field throughout the theme + __( "Checkbox: ", DXP_TD ), // The label to the left of the option interface element + array( $this, 'dx_options_checkbox_callback' ), // The name of the function responsible for rendering the option interface + 'dx_base_plugin_options', // The page on which this option will be displayed + 'dx_options_settings_section' // The name of the section to which this field belongs + ); + + add_settings_field( + 'dx_options_text', // ID used to identify the field throughout the theme + __( "DX Sample: ", DXP_TD ), // The label to the left of the option interface element + array( $this, 'dx_options_text_callback' ), // The name of the function responsible for rendering the option interface + 'dx_base_plugin_options', // The page on which this option will be displayed + 'dx_options_settings_section' // The name of the section to which this field belongs + ); + } public function dx_settings_callback() { @@ -115,4 +140,33 @@ public function dx_validate_settings( $input ) { }); return $input; } + + public function dx_options_settings_callback() { + _e( 'Options page settings', DXP_TD ); + } + + public function dx_options_checkbox_callback() { + $dx_options_setting = get_option( 'dx_options_setting' ); + $checkbox = !empty( $dx_options_setting['dx_options_checkbox'] )? $dx_options_setting['dx_options_checkbox'] : ''; + ?> + <input type="checkbox" id="dx_options_checkbox" name="dx_options_setting[dx_options_checkbox]" value="1" <?php checked( 1, $checkbox ); ?> /> + <label for="dx_options_checkbox"><?php _e( 'Checkbox description', DXP_TD ); ?></label> + <?php + } + + public function dx_options_text_callback() { + $dx_options_setting = get_option( 'dx_options_setting' ); + $text = !empty( $dx_options_setting['dx_options_text'] )? $dx_options_setting['dx_options_text'] : ''; + ?> + <input type="text" id="dx_options_text" name="dx_options_setting[dx_options_text]" value="<?php esc_attr_e( $text ); ?>" /> + <label for="dx_options_text"><?php _e( 'Text input description', DXP_TD ); ?></label> + <?php + } + + public function dx_options_settings_validate( $input ) { + array_walk_recursive($input, function( &$item, $key ){//senitizy the user input before saving to the DB + $item = esc_attr( $item ); + }); + return $input; + } }