Changeset 2865958
- Timestamp:
- 02/15/2023 07:49:09 PM (3 years ago)
- Location:
- jane-menu/trunk/includes
- Files:
-
- 1 added
- 8 edited
- 1 moved
-
ajax.php (modified) (2 diffs)
-
compatibility.php (added)
-
core.php (modified) (6 diffs)
-
core__autoloader.php (modified) (1 diff)
-
db.php (modified) (3 diffs)
-
helpers.php (modified) (6 diffs)
-
request.php (modified) (1 diff)
-
sitemap.php (moved) (moved from jane-menu/trunk/includes/robots.php) (2 diffs)
-
store-configs.php (modified) (6 diffs)
-
store-configs__form.php (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
-
jane-menu/trunk/includes/ajax.php
r2854692 r2865958 14 14 * 15 15 * @uses IHeartJane\WebMenu\Helpers\get_page_relative_path() Helpers\get_page_relative_path() 16 * 17 * @since 1.3.0 16 18 * 17 19 * @return void Uses wp_send_json_success() to return a string or wp_send_json_error() to return an array … … 45 47 * Returns the full path for a specific Page ID 46 48 * 49 * @since 1.3.0 50 * 47 51 * @return void Uses wp_send_json_success() to return a string or wp_send_json_error() to return an array 48 52 */ -
jane-menu/trunk/includes/core.php
r2854692 r2865958 19 19 * @see https://developer.wordpress.org/reference/functions/add_option/ Codex, function: add_option 20 20 * 21 * @since 1.0.0 22 * @since 1.3.0 Cleaned 23 * 21 24 * @return void 22 25 */ … … 34 37 * @uses IHeartJane\WebMenu\Constants\PLUGIN_ID Constants\PLUGIN_ID 35 38 * 39 * @since 1.0.0 40 * @since 1.3.0 Not actually used 41 * 36 42 * @return void 37 43 */ … … 47 53 * @uses IHeartJane\WebMenu\Constants\OPTION_SITEMAP_ENABLED_NAME Constants\OPTION_SITEMAP_ENABLED_NAME 48 54 * @uses IHeartJane\WebMenu\DB\drop_config_table() DB\drop_config_table() 55 * 56 * @since 1.3.0 49 57 * 50 58 * @return void … … 67 75 * 68 76 * @see https://developer.wordpress.org/reference/hooks/admin_menu/ Codex, action: admin_menu 77 * 78 * @since 1.0.0 69 79 * 70 80 * @return void … … 106 116 * @uses IHeartJane\WebMenu\Constants\TEMPLATES_DIR Constants\TEMPLATES_DIR 107 117 * 118 * @since 1.0.0 119 * 108 120 * @return void 109 121 */ … … 128 140 * @uses IHeartJane\WebMenu\Helpers\enqueue_CSS() Helpers\enqueue_CSS() 129 141 * @uses IHeartJane\WebMenu\Helpers\enqueue_JS() Helpers\enqueue_JS() 142 * 143 * @since 1.3.0 130 144 * 131 145 * @return void -
jane-menu/trunk/includes/core__autoloader.php
r2854692 r2865958 14 14 * @param string $dir Full path from where the namespaces will have their relative folders 15 15 * @param array $namespaces Array of namespaces with FQCN base as the key and relative path as the value 16 * 17 * @since 1.3.0 16 18 * 17 19 * @return void -
jane-menu/trunk/includes/db.php
r2854692 r2865958 17 17 * 18 18 * @see https://developer.wordpress.org/reference/hooks/admin_init/ Codex, action: admin_init 19 * 20 * @since 1.3.0 19 21 * 20 22 * @return void … … 43 45 * 44 46 * @uses IHeartJane\WebMenu\Constants\DB_TABLE_NAME Constants\DB_TABLE_NAME 47 * 48 * @since 1.0.0 49 * @since 1.3.0 New structure 45 50 * 46 51 * @return string[] Array of strings containing the changes, format table_name.column_name => description of the performed action … … 110 115 * @uses IHeartJane\WebMenu\Constants\DB_TABLE_NAME Constants\DB_TABLE_NAME 111 116 * 117 * @since 1.1.0 118 * 112 119 * @return void 113 120 */ -
jane-menu/trunk/includes/helpers.php
r2854692 r2865958 12 12 * 13 13 * @param string Text that should be escaped 14 * 15 * @since 1.0.0 14 16 * 15 17 * @return string Escaped text … … 28 30 * @param string $replace Replace string 29 31 * @param string $subject Subject string, haystack 32 * 33 * @since 1.0.0 30 34 * 31 35 * @return string Altered string, in case the search string is not found - original string … … 51 55 * 52 56 * @param int $page_id Page ID 57 * 58 * @since 1.3.0 53 59 * 54 60 * @return string|false Relative path or false if page does not exist … … 79 85 * @param string $text Any descriptive text that helps you find your log 80 86 * @param bool $delete Append or overwrite the file, default: false (don't delete) 87 * 88 * @since 1.3.0 81 89 * 82 90 * @return void … … 110 118 * @param bool $is_footer should the script get loaded in the footer, default: true 111 119 * 120 * @since 1.3.0 121 * 112 122 * @return void 113 123 */ … … 132 142 * @param string[] $dependency list of dependency handles, default: empty array 133 143 * 144 * @since 1.3.0 145 * 134 146 * @return void 135 147 */ -
jane-menu/trunk/includes/request.php
r2854692 r2865958 9 9 use IHeartJane\WebMenu\DB; 10 10 use IHeartJane\WebMenu\StoreConfigs; 11 use IHeartJane\WebMenu\Helpers; 11 12 12 13 /** 13 * Creates the global variable with current Store Config ID14 * Creates the global variable with current Store Config data 14 15 * 15 16 * Allows us to perform the database check once and reuse the results in several places 16 17 * 17 * @uses IHeartJane\WebMenu\Constants\PLUGIN_ID Constants\PLUGIN_ID18 18 * @uses \IHeartJane\WebMenu\StoreConfigs\Current_Store_Config::get_instance() StoreConfigs\Current_Store_Config::get_instance() 19 19 * 20 20 * @see https://developer.wordpress.org/reference/hooks/init/ Codex, action: init 21 21 * 22 * @since 1.3.0 23 * 22 24 * @return void 23 25 */ 24 26 function initial_check(){ 25 27 26 28 global $jane__current_config; 27 29 28 30 $jane__current_config = StoreConfigs\Current_Store_Config::get_instance(); 29 30 31 } 31 32 add_action( 'init', __NAMESPACE__ . '\\' . 'initial_check' ); 32 33 33 34 /** 34 * Intercepts the paring of the request35 * Alter the WordPress request results 35 36 * 36 * If the current request is for a current config, don't allow it to become 404 37 * 38 * @uses \IHeartJane\WebMenu\StoreConfigs\Current_Store_Config::get_id() StoreConfigs\Current_Store_Config::get_id() 39 * 40 * @see https://developer.wordpress.org/reference/hooks/do_parse_request/ Codex, filter: do_parse_request 41 * 42 * @param bool $should_parse Whether or not to parse the request. Default true 43 * @param \WP $wp Current WordPress environment instance 44 * @param array|string $extra_query_vars Extra passed query variables 45 * 46 * @return bool If we're handling the Store Config request returns false so WordPress wouldn't handle it the usual way 47 */ 48 function intercept_parse_request( $should_parse, $wp, $extra_query_vars ){ 49 50 global $wp_query, $jane__current_config; 51 52 if( $jane__current_config->get_id() ){ 53 54 status_header( 200 ); 55 $wp_query->is_page = true; 56 $wp_query->is_singular = true; 57 $wp_query->is_404 = false; 58 59 return false; 60 } 61 62 return $should_parse; 63 } 64 add_filter( 'do_parse_request', __NAMESPACE__ . '\\' . 'intercept_parse_request', 10, 3 ); 65 66 /** 67 * Checks if the header request status code should get overriden 68 * 69 * This function is not required because of {@see \IHeartJane\WebMenu\Request\intercept_parse_request() Request\intercept_parse_request()} 70 * but is left active intentionally in case the {@see \IHeartJane\WebMenu\Request\intercept_parse_request() Request\intercept_parse_request()} doesn't get triggered 71 * 72 * @uses \IHeartJane\WebMenu\StoreConfigs\Current_Store_Config::get_id() StoreConfigs\Current_Store_Config::get_id() 37 * When a fictive subpage of a Jane Menu is requested, we have to simulate that the Jane Menu page was called. 73 38 * 74 39 * @see https://developer.wordpress.org/reference/hooks/parse_request/ Codex, action: parse_request 75 40 * 76 * @param \WP $wp Current WordPress environment instance41 * @param \WP $wp Current WordPress environment instance (passed by reference) 77 42 * 78 * @return void 43 * @since 1.3.0 44 * 45 * @return void The $wp is passed by reference 79 46 */ 80 function do_after_parse_request( $wp ){ 81 82 global $wp_query, $jane__current_config; 83 84 if( $jane__current_config->get_id() ){ 85 86 status_header( 200 ); 87 88 $wp_query->is_page = true; 89 $wp_query->is_singular = true; 90 $wp_query->is_404 = false; 91 92 } 93 } 94 add_action( 'parse_request', __NAMESPACE__ . '\\' . 'do_after_parse_request' ); 95 96 /** 97 * Intercepts handling of 404 header status codes 98 * 99 * This function is not required because of {@see \IHeartJane\WebMenu\Request\intercept_parse_request() Request\intercept_parse_request()} 100 * but is left active intentionally in case the {@see \IHeartJane\WebMenu\Request\intercept_parse_request() Request\intercept_parse_request()} doesn't get triggered 101 * 102 * @uses \IHeartJane\WebMenu\StoreConfigs\Current_Store_Config::get_id() StoreConfigs\Current_Store_Config::get_id() 103 * 104 * @see https://developer.wordpress.org/reference/hooks/pre_handle_404/ Codex, filter: pre_handle_404 105 * 106 * @param bool $preempt Whether to short-circuit default header status handling. Default false 107 * @param \WP_Query $wp_query WordPress Query object 108 * 109 * @return bool 110 */ 111 function intercept_404( $preempt, $wp_query ){ 47 function after_parse_request( $wp ){ 112 48 113 49 global $jane__current_config; 114 50 51 // Helpers\debug_log( $wp, "Request-after_parse_request-wp" ); 52 115 53 if( $jane__current_config->get_id() ){ 116 54 117 $preempt = true; 55 $query_vars = [ 56 "page" => null, 57 "pagename" => $jane__current_config->get_config()->store_path, 58 ]; 59 60 $wp->query_vars = $query_vars; 61 62 $wp->matched_rule = '(.?.+?)(?:/([0-9]+))?/?$'; 63 $wp->matched_query = urlencode( $jane__current_config->get_config()->store_path ) . '&page='; 64 118 65 } 119 66 120 return $preempt; 67 // Helpers\debug_log( $wp, "Request-after_parse_request-wp" ); 68 121 69 } 122 add_filter( 'pre_handle_404', __NAMESPACE__ . '\\' . 'intercept_404', 10, 2 ); 123 124 // if the config id exists, replace the template with our custom template 70 add_action( 'parse_request', __NAMESPACE__ . '\\' . 'after_parse_request', 10, 1 ); 125 71 126 72 /** 127 * Replaces the default template with our custom template73 * Alter the page content 128 74 * 129 * @uses \IHeartJane\WebMenu\StoreConfigs\Current_Store_Config::get_template_path() StoreConfigs\Current_Store_Config::get_template_path()75 * It will replace the shortcode if it exists, otherwise it will append our code at the end of the content. 130 76 * 131 * @see https://developer.wordpress.org/reference/hooks/t emplate_include/ Codex, filter: template_include77 * @see https://developer.wordpress.org/reference/hooks/the_content/ Codex, filter: the_content 132 78 * 133 * @param string $ template The path of the template to include79 * @param string $content The original content of the page 134 80 * 135 * @return string 81 * @since 1.3.0 82 * @since 1.3.2 The hook priority altered to fire late (as late as possible) 83 * 84 * @return string Altered content 136 85 */ 137 function replace_template( $template ){86 function inject_in_body( $content ) { 138 87 139 88 global $jane__current_config; 140 89 141 if( $jane__current_config->get_template_path() ){90 if( is_singular() && in_the_loop() && is_main_query() && ! empty( $jane__current_config->get_template_html_body() ) ){ 142 91 143 $template = $jane__current_config->get_template_path(); 92 if ( strpos( $content, '[jane_menu_shortcode]' ) !== false ){ 93 94 $content = str_replace( '[jane_menu_shortcode]', $jane__current_config->get_template_html_body(), $content );; 95 96 } else { 97 98 $content .= $jane__current_config->get_template_html_body(); 99 } 144 100 } 145 101 146 return $ template;102 return $content; 147 103 } 148 add_filter( 'template_include', __NAMESPACE__ . '\\' . 'replace_template' ); 104 add_filter( 'the_content', __NAMESPACE__ . '\\' . 'inject_in_body', PHP_INT_MAX ); 105 106 /** 107 * Append our code in the <head> tag 108 * 109 * @see https://developer.wordpress.org/reference/hooks/wp_head/ Codex, action: wp_head 110 * 111 * @since 1.3.0 112 * 113 * @return void The code is echoed 114 */ 115 function inject_in_head(){ 116 117 global $jane__current_config; 118 119 if ( ! empty( $jane__current_config->get_template_html_head() ) ){ 120 121 echo $jane__current_config->get_template_html_head(); 122 } 123 } 124 add_action( 'wp_head', __NAMESPACE__ . '\\' . 'inject_in_head' ); 125 126 127 128 129 130 -
jane-menu/trunk/includes/sitemap.php
r2865957 r2865958 1 1 <?php 2 2 /** 3 * Robots .txtrelated functions3 * Robots and sitemaps related functions 4 4 * 5 5 */ 6 6 7 namespace IHeartJane\WebMenu\ Robots;7 namespace IHeartJane\WebMenu\Sitemap; 8 8 9 9 use IHeartJane\WebMenu\Constants; 10 10 use IHeartJane\WebMenu\StoreConfigs; 11 use IHeartJane\WebMenu\Helpers; 11 12 12 13 /** 13 14 * Adds all Store Configs sitemap URLs to robots.txt 14 15 * 15 * @uses IHeartJane\WebMenu\Constants\OPTION_SITEMAP_ENABLED_NAME Constants\OPTION_SITEMAP_ENABLED_NAME 16 * @uses IHeartJane\WebMenu\StoreConfigs\get_all_configs() StoreConfigs\get_all_configs() 16 * @uses IHeartJane\WebMenu\Constants\OPTION_SITEMAP_ENABLED_NAME Constants\OPTION_SITEMAP_ENABLED_NAME 17 * @uses IHeartJane\WebMenu\Constants\SITEMAP_PATH Constants\SITEMAP_PATH 18 * @uses IHeartJane\WebMenu\Constants\SITEMAP_URL Constants\SITEMAP_URL 17 19 * 18 * @see https://developer.wordpress.org/reference/hooks/robots_txt/ Codex, filter: robots_txt20 * @see https://developer.wordpress.org/reference/hooks/robots_txt/ Codex, filter: robots_txt 19 21 * 20 22 * @param string $output The robots.txt output 21 23 * @param bool $public Whether the site is considered "public" 24 * 25 * @since 1.0.0 26 * @since 1.3.2 Only the local sitemap index is appended 22 27 * 23 28 * @return string Appended URLs at the end of the robots.txt file content … … 25 30 function append_to_robots_txt( $output, $public ){ 26 31 27 if( get_option( Constants\OPTION_SITEMAP_ENABLED_NAME ) ){32 if( get_option( Constants\OPTION_SITEMAP_ENABLED_NAME ) && file_exists( Constants\SITEMAP_PATH ) ){ 28 33 29 $store_configs = StoreConfigs\get_all_configs(); 30 31 foreach( $store_configs as $store_config ){ 32 33 // $sitemap_url = site_url( "/" . $store_config->store_path . "/" . Constants\SITEMAP_FILE_NAME_FULL ); 34 $sitemap_url = $store_config->sitemap_url; 35 36 $output .= sprintf( 'Sitemap: %s', $sitemap_url ) . PHP_EOL; 37 } 34 $output .= sprintf( 'Sitemap: %s', Constants\SITEMAP_URL ) . PHP_EOL; 38 35 } 39 36 40 37 return $output; 41 38 } 42 39 add_filter( 'robots_txt', __NAMESPACE__ . '\\' . 'append_to_robots_txt', 99, 2 ); 40 41 /** 42 * Creates or updates the sitemap index file 43 * 44 * The sitemap index file lists all the remote sitemap URLs from existing Store Configs 45 * 46 * @todo Needs to get triggered when a Store Config is deleted too 47 * 48 * @uses IHeartJane\WebMenu\Constants\OPTION_SITEMAP_ENABLED_NAME Constants\OPTION_SITEMAP_ENABLED_NAME 49 * @uses IHeartJane\WebMenu\Constants\SITEMAP_DIR Constants\SITEMAP_DIR 50 * @uses IHeartJane\WebMenu\Constants\SITEMAP_PATH Constants\SITEMAP_PATH 51 * @uses IHeartJane\WebMenu\StoreConfigs\get_all_configs() StoreConfigs\get_all_configs() 52 * 53 * @see https://developer.wordpress.org/reference/functions/wp_upload_dir/ Codex, function: wp_upload_dir 54 * @see https://developer.wordpress.org/reference/functions/wp_mkdir_p/ Codex, function: wp_mkdir_p 55 * 56 * @since 1.3.2 57 * 58 * @return bool True on success, otherwise false 59 */ 60 function update_remote_sitemap(){ 61 62 63 // empty and remove the existing folder 64 65 if( is_dir( Constants\SITEMAP_DIR ) ){ 66 67 $files = array_diff( scandir( Constants\SITEMAP_DIR ), array( '.', '..' ) ); 68 69 foreach( $files as $file ){ 70 71 if( ! is_dir( Constants\SITEMAP_DIR . $file ) ){ 72 73 unlink( Constants\SITEMAP_DIR . $file ); 74 } 75 76 } 77 78 rmdir( Constants\SITEMAP_DIR ); 79 } 80 81 82 // if the option is turned on, proceed 83 84 if( ! get_option( Constants\OPTION_SITEMAP_ENABLED_NAME ) ){ 85 86 return false; 87 } 88 89 90 // if we have anything to write, proceed 91 92 $store_configs = StoreConfigs\get_all_configs(); 93 94 if( empty( $store_configs ) ){ 95 return false; 96 } 97 98 99 // attempt to create the folder 100 101 $directory_exists = wp_mkdir_p( Constants\SITEMAP_DIR ); 102 103 if( ! $directory_exists ){ 104 105 return false; 106 } 107 108 109 // generate the xml content 110 111 $output = [ 112 '<?xml version="1.0" encoding="UTF-8"?>', 113 '<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">', 114 ]; 115 116 foreach( $store_configs as $store_config ){ 117 118 $sitemap_url = $store_config->sitemap_url; 119 120 $output[] = ' <sitemap>'; 121 $output[] = ' <loc>' . $store_config->sitemap_url . '</loc>'; 122 $output[] = ' </sitemap>'; 123 } 124 125 $output[] = '</sitemapindex>'; 126 127 $xml = implode( PHP_EOL, $output ); 128 129 // Helpers\debug_log( $xml, "sitemap-update_remote_sitemap-xml" ); 130 131 132 $success = file_put_contents( Constants\SITEMAP_PATH, $xml ); 133 134 return $success ? true : false; 135 } -
jane-menu/trunk/includes/store-configs.php
r2854692 r2865958 16 16 * 17 17 * @param array $args Array of Store Config properties, if ID is set, it will get updated, otherwise inserted 18 * 19 * @since 1.0.0 18 20 * 19 21 * @return int|\WP_Error|false The Store Config ID if it's inserted or updated, \WP_Error if initial validation fails, or false on error … … 93 95 * @param int[] $ids Array of Store Config IDs 94 96 * 97 * @since 1.0.0 98 * 95 99 * @return void The query simply deletes the rows of found IDs, if any 96 100 */ … … 114 118 * @param int $config_id Store Config ID 115 119 * 120 * @since 1.0.0 121 * 116 122 * @return object|null Store Config database row as an object, null if not found 117 123 */ … … 136 142 * @param array $args Optional array of optional arguments 137 143 * 144 * @since 1.0.0 145 * 138 146 * @return object[]|null Array of Store Config database rows as objects, null if not found 139 147 */ … … 205 213 * @uses IHeartJane\WebMenu\Constants\DB_TABLE_NAME Constants\DB_TABLE_NAME 206 214 * 215 * @since 1.0.0 216 * 207 217 * @return object[]|array Array of Store Config database rows as objects, empty array if none found. Array keys are Store Config ID 208 218 */ … … 223 233 * @uses IHeartJane\WebMenu\Constants\DB_TABLE_NAME Constants\DB_TABLE_NAME 224 234 * 235 * @since 1.0.0 236 * 225 237 * @return int Returns the count of Store Config IDs 226 238 */ -
jane-menu/trunk/includes/store-configs__form.php
r2854692 r2865958 8 8 use IHeartJane\WebMenu\Constants; 9 9 use IHeartJane\WebMenu\StoreConfigs; 10 use IHeartJane\WebMenu\Sitemap; 10 11 use IHeartJane\WebMenu\Helpers; 11 12 … … 19 20 * @uses IHeartJane\WebMenu\StoreConfigs\get_all_configs() StoreConfigs\get_all_configs() 20 21 * @uses IHeartJane\WebMenu\StoreConfigs\insert_config() StoreConfigs\insert_config() 22 * @uses IHeartJane\WebMenu\Sitemap\update_remote_sitemap() Sitemap\update_remote_sitemap() 21 23 * 22 24 * @see https://developer.wordpress.org/reference/hooks/admin_init/ Codex, action: admin_init 25 * 26 * @since 1.0.0 23 27 * 24 28 * @return void The result is handled through the add_settings_error() or wp_redirect() … … 140 144 // redirect if success 141 145 142 if( ! empty( $success ) ){ 146 if( ! empty( $success ) && ! is_wp_error( $success ) ){ 147 148 Sitemap\update_remote_sitemap(); 143 149 144 150 $redirect_to = add_query_arg( array( 'message' => 'success' ), $page_url ); … … 148 154 } 149 155 156 if( is_wp_error( $success ) ){ 157 158 add_settings_error( 'jane_store_config_error', 'jane_store_config_error', $success->get_error_message() ); 159 160 } else { 150 161 151 add_settings_error( 'jane_store_config_error', 'jane_store_config_error', "Error: Something went wrong" ); 162 add_settings_error( 'jane_store_config_error', 'jane_store_config_error', "Error: Something went wrong" ); 163 } 164 152 165 153 166 return;
Note: See TracChangeset
for help on using the changeset viewer.