From 792a9a98ea42abd87264d8eefa890da6480bdd41 Mon Sep 17 00:00:00 2001 From: Sander van Dragt Date: Thu, 4 Jun 2020 16:56:10 +0100 Subject: [PATCH] initial code --- 404-fallback.php | 77 ++++++++++++++++++++++ lib/menu-site.php | 137 ++++++++++++++++++++++++++++++++++++++ lib/menus.php | 164 ++++++++++++++++++++++++++++++++++++++++++++++ readme.txt | 115 ++++++++++++++++++++++++++++++++ 4 files changed, 493 insertions(+) create mode 100755 404-fallback.php create mode 100644 lib/menu-site.php create mode 100644 lib/menus.php create mode 100755 readme.txt diff --git a/404-fallback.php b/404-fallback.php new file mode 100755 index 0000000..07f5948 --- /dev/null +++ b/404-fallback.php @@ -0,0 +1,77 @@ + 'fb404', + 'menu_slug' => 'fb404_page', + 'page_title' => '404 Fallback Settings', + 'menu_title' => '404 Fallback', + 'action' => 'fb404_page_update_handler', + ] ); + + wpc_menu_register_settings( 'fb404', [ + 'general' => [ + 'fb404_setting_fallback_url' => 'Fallback URL', + ], + ] ); + do_action( __FUNCTION__ ); +} + +add_action( 'init', 'fb404_add_settings_menu' ); + +/** + * Templating for our menu page. The function must match the 'menu_slug' in the menu config. + */ +function fb404_page() { + // Use the default page rendering instead of templating our page here. + wpc_menu_page_render( 'fb404' ); +} + +/** + * Handling the form submission. The function must match the 'action' key in the menu config. + */ +function fb404_page_update_handler() { + // Use the default page update handler instead of processing our options in a custom way. + wpc_menu_page_update_handler( 'fb404' ); +} + +function fb404_setting_fallback_url_render() { + $option_name = 'fb404_setting_fallback_url'; + $config = stripslashes( get_option( $option_name ) ); + printf( '', $option_name, $config ); +} diff --git a/lib/menu-site.php b/lib/menu-site.php new file mode 100644 index 0000000..f5d2248 --- /dev/null +++ b/lib/menu-site.php @@ -0,0 +1,137 @@ + $config['menu_slug'], + 'updated' => (string) $updated, + ], admin_url( 'admin.php' ) ) ); + exit; + +} + +/** + * Register a grouped setting to a menu page id. + * + * @example Add a setting to the general settings group of the cluster menu-page: + * wpc_menu_register_page_settings( 'cluster', [ + * 'general' => [ + * 'wpc_cluster_use_production_images' => 'Use Production Images', + * ], + * ] ); + * + * @param string|array $menu_id Menu identifier or configuration + * @param array $grouped_settings Array of setting groups. + */ +function wpc_menu_register_settings( $menu_id, $grouped_settings ) { + + add_filter( 'wpc_menu_page_configs', function ( $menus ) use ( $menu_id, $grouped_settings ) { + if ( empty( $menus[ $menu_id ]['setting_sections'] ) ) { + $menus[ $menu_id ]['setting_sections'] = []; + } + $menus[ $menu_id ]['setting_sections'] = array_merge_recursive( $menus[ $menu_id ]['setting_sections'], $grouped_settings ); + + return $menus; + }, 11 ); + +} + +/** + * Register a menu-page. + * + * @example Add a setting to the general settings group of the cluster menu-page: + * wpc_menu_register_page( [ + * 'id' => 'cluster', + * 'menu_slug' => 'wpc_network_cluster_page', + * 'page_title' => 'Cluster Settings', + * 'menu_title' => 'Cluster', + * ] ); + * + * The following keys are optional: + * - action: points to the function that will update the options instead + * of wpc_menu_page_update_handler() + * + * @param $config + */ +function wpc_menu_register_page( $config ) { + add_filter( 'wpc_menu_page_configs', function ( $menus ) use ( $config ) { + $menus[ $config['id'] ] = $config; + + return $menus; + } ); + + $defaults = [ + 'page_title' => 'My Menu Page', + 'menu_title' => 'My Menu', + ]; + $config = wp_parse_args( $config, $defaults ); + + add_action( 'admin_menu', function () use ( $config ) { + $menu_slug = $config['menu_slug']; + + if ( empty( $config['parent_slug'] ) ) { + add_menu_page( + $config['page_title'], + $config['menu_title'], + 'manage_options', + $menu_slug, + $menu_slug + ); + } else { + add_submenu_page( + $config['parent_slug'], + $config['page_title'], + $config['menu_title'], + 'manage_options', + $menu_slug, + $menu_slug + ); + } + } ); + + $action = $config['action']; + + /** + * This function here is hooked up to a special action and necessary to process the saving of the options. + */ + add_action( 'admin_post_' . $action, $action ); +} diff --git a/lib/menus.php b/lib/menus.php new file mode 100644 index 0000000..e073792 --- /dev/null +++ b/lib/menus.php @@ -0,0 +1,164 @@ + $settings ) { + $page = $config['menu_slug']; + add_settings_section( $section, strtoupper( $section ), null, $page ); + + foreach ( (array) $settings as $name => $title ) { + $sanitization = is_callable( $name . '_sanitize' ) ? $name . '_sanitize' : []; + register_setting( $section, $name, $sanitization ); + add_settings_field( $name, $title, + $name . '_render', $page, + $section ); + } + } + } + } +} + +/** + * Update a single option + * + * @param string $type Type: network or site + * @param bool $delete_missing Whether to delete the option if not submitted? + * @param string $option Option name + * + * @return bool False if value was not updated, true if updated. + */ +function wpc_menu_update_option( $type, $delete_missing, $option ): bool { + $updated = false; + + if ( isset( $_POST[ $option ] ) ) { + if ( 'network' === $type ) { + $updated = update_site_option( $option, $_POST[ $option ] ); + } else { + $updated = update_option( $option, $_POST[ $option ] ); + + } + } elseif ( $delete_missing ) { + if ( 'network' === $type ) { + $updated = delete_site_option( $option ); + } else { + $updated = delete_option( $option ); + } + } + + return $updated; +} + +/** + * Update the options for a given network page configuration + * + * @param string $type network|site configuration type + * @param array|string $config menu configuration or id of one. + * @param bool $delete_missing if missing options should be deleted or skipped + * + * @return bool if any of the options was updated or deleted + */ +function wpc_menu_update_options( $type, $config, $delete_missing ) { + global $new_whitelist_options; + + check_admin_referer( $config['menu_slug'] . '-options' ); + + $did_update = false; + + // Update or delete all the options + $sections = $config['setting_sections']; + if ( false === is_iterable( $sections ) ) { + return $did_update ? 'true' : 'false'; + } + foreach ( (array) $sections as $section => $options ) { + + $options = $new_whitelist_options[ $section ]; + foreach ( (array) $options as $option ) { + $updated = wpc_menu_update_option( $type, $delete_missing, $option ); + if ( $updated ) { + $did_update = true; + } + } + } + + return $did_update ? 'true' : 'false'; + +} + +function wpc_menu_render( $type, $config ) { + // Nonce is verified by wpc_menu_fields + if ( isset( $_GET['updated'] ) ): ?> +

+ 'edit.php?action=' . esc_attr( $config['action'] ), + 'site' => admin_url( 'admin-post.php' ), + ]; + ?> +
+
+

+
+ +
+ +
+
+ "; + } +} diff --git a/readme.txt b/readme.txt new file mode 100755 index 0000000..67518ee --- /dev/null +++ b/readme.txt @@ -0,0 +1,115 @@ +=== 404 Fallback === +Contributors: (this should be a list of wordpress.org userid's) +Donate link: https://example.com/ +Tags: comments, spam +Requires at least: 4.5 +Tested up to: 5.4.1 +Requires PHP: 5.6 +Stable tag: 0.1.0 +License: GPLv2 or later +License URI: https://www.gnu.org/licenses/gpl-2.0.html + +Here is a short description of the plugin. This should be no more than 150 characters. No markup here. + +== Description == + +This is the long description. No limit, and you can use Markdown (as well as in the following sections). + +For backwards compatibility, if this section is missing, the full length of the short description will be used, and +Markdown parsed. + +A few notes about the sections above: + +* "Contributors" is a comma separated list of wp.org/wp-plugins.org usernames +* "Tags" is a comma separated list of tags that apply to the plugin +* "Requires at least" is the lowest version that the plugin will work on +* "Tested up to" is the highest version that you've *successfully used to test the plugin*. Note that it might work on +higher versions... this is just the highest one you've verified. +* Stable tag should indicate the Subversion "tag" of the latest stable version, or "trunk," if you use `/trunk/` for +stable. + + Note that the `readme.txt` of the stable tag is the one that is considered the defining one for the plugin, so +if the `/trunk/readme.txt` file says that the stable tag is `4.3`, then it is `/tags/4.3/readme.txt` that'll be used +for displaying information about the plugin. In this situation, the only thing considered from the trunk `readme.txt` +is the stable tag pointer. Thus, if you develop in trunk, you can update the trunk `readme.txt` to reflect changes in +your in-development version, without having that information incorrectly disclosed about the current stable version +that lacks those changes -- as long as the trunk's `readme.txt` points to the correct stable tag. + + If no stable tag is provided, it is assumed that trunk is stable, but you should specify "trunk" if that's where +you put the stable version, in order to eliminate any doubt. + +== Installation == + +This section describes how to install the plugin and get it working. + +e.g. + +1. Upload `plugin-name.php` to the `/wp-content/plugins/` directory +1. Activate the plugin through the 'Plugins' menu in WordPress +1. Place `` in your templates + +== Frequently Asked Questions == + += A question that someone might have = + +An answer to that question. + += What about foo bar? = + +Answer to foo bar dilemma. + +== Screenshots == + +1. This screen shot description corresponds to screenshot-1.(png|jpg|jpeg|gif). Note that the screenshot is taken from +the /assets directory or the directory that contains the stable readme.txt (tags or trunk). Screenshots in the /assets +directory take precedence. For example, `/assets/screenshot-1.png` would win over `/tags/4.3/screenshot-1.png` +(or jpg, jpeg, gif). +2. This is the second screen shot + +== Changelog == + += 1.0 = +* A change since the previous version. +* Another change. + += 0.5 = +* List versions from most recent at top to oldest at bottom. + +== Upgrade Notice == + += 1.0 = +Upgrade notices describe the reason a user should upgrade. No more than 300 characters. + += 0.5 = +This version fixes a security related bug. Upgrade immediately. + +== Arbitrary section == + +You may provide arbitrary sections, in the same format as the ones above. This may be of use for extremely complicated +plugins where more information needs to be conveyed that doesn't fit into the categories of "description" or +"installation." Arbitrary sections will be shown below the built-in sections outlined above. + +== A brief Markdown Example == + +Ordered list: + +1. Some feature +1. Another feature +1. Something else about the plugin + +Unordered list: + +* something +* something else +* third thing + +Here's a link to [WordPress](https://wordpress.org/ "Your favorite software") and one to [Markdown's Syntax Documentation][markdown syntax]. +Titles are optional, naturally. + +[markdown syntax]: https://daringfireball.net/projects/markdown/syntax + "Markdown is what the parser uses to process much of the readme file" + +Markdown uses email style notation for blockquotes and I've been told: +> Asterisks for *emphasis*. Double it up for **strong**. + +``