AHSC_Comments.php
5 months ago
AHSC_Deferer.php
5 months ago
AHSC_Plugins.php
5 months ago
AHSC_PostType.php
3 days ago
AHSC_Terms.php
3 days ago
AHSC_Themes.php
5 months ago
AHSC_PostType.php
304 lines
| 1 | <?php |
| 2 | if ( ! defined( 'ABSPATH' ) ) { |
| 3 | exit; |
| 4 | } |
| 5 | global $pagenow; |
| 6 | $ahsc_allowed_cases = array( 'publish', 'future', 'trash' ); |
| 7 | $ahsc_target=array(); |
| 8 | $ahsc_is_json=(defined('REST_REQUEST') && REST_REQUEST) || true === wp_is_json_request() || |
| 9 | (isset( $_SERVER['REQUEST_URI'] ) && strpos( sanitize_text_field( wp_unslash( $_SERVER['REQUEST_URI'] ) ), trailingslashit( rest_get_url_prefix() ) ) !== false) ; |
| 10 | /* |
| 11 | * Read-only routing check on WooCommerce's own AJAX parameter: it only decides whether the |
| 12 | * purge hooks get registered, nothing is written and no form is processed, so there is no |
| 13 | * nonce to verify — WooCommerce owns that request, not this plugin. |
| 14 | */ |
| 15 | // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- routing check only, see comment above. |
| 16 | $ahsc_wc_ajax = isset( $_REQUEST['wc-ajax'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['wc-ajax'] ) ) : ''; |
| 17 | |
| 18 | if( ( ( function_exists( 'wp_doing_ajax' ) && wp_doing_ajax() ) || defined( 'DOING_AJAX' ) ) && |
| 19 | ( '' !== $ahsc_wc_ajax && 'checkout' !== $ahsc_wc_ajax) || |
| 20 | 'nav-menus.php' !== $pagenow){ |
| 21 | if(is_array(AHSC_CONSTANT['ARUBA_HISPEED_CACHE_OPTIONS']) && AHSC_CONSTANT['ARUBA_HISPEED_CACHE_OPTIONS']['ahsc_purge_homepage_on_edit'] /*|| |
| 22 | AHSC_CONSTANT['ARUBA_HISPEED_CACHE_OPTIONS']['ahsc_purge_homepage_on_del'] || |
| 23 | AHSC_CONSTANT['ARUBA_HISPEED_CACHE_OPTIONS']['ahsc_purge_page_on_mod'] || |
| 24 | AHSC_CONSTANT['ARUBA_HISPEED_CACHE_OPTIONS']['ahsc_purge_archive_on_edit'] || |
| 25 | AHSC_CONSTANT['ARUBA_HISPEED_CACHE_OPTIONS']['ahsc_purge_archive_on_del']*/ |
| 26 | ){ |
| 27 | \add_action( 'post_updated', 'ahsc_post_updated' , 20, 2 ); |
| 28 | |
| 29 | if ( did_action( 'elementor/loaded' ) ) { |
| 30 | add_action( 'elementor/editor/after_save', 'ahsc_post_updated', 10 , 2 ); |
| 31 | } |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | if('nav-menus.php' !== $pagenow){ |
| 36 | if(is_array(AHSC_CONSTANT['ARUBA_HISPEED_CACHE_OPTIONS']) && AHSC_CONSTANT['ARUBA_HISPEED_CACHE_OPTIONS']['ahsc_purge_homepage_on_edit'] /*|| |
| 37 | AHSC_CONSTANT['ARUBA_HISPEED_CACHE_OPTIONS']['ahsc_purge_homepage_on_del'] || |
| 38 | AHSC_CONSTANT['ARUBA_HISPEED_CACHE_OPTIONS']['ahsc_purge_page_on_mod'] || |
| 39 | AHSC_CONSTANT['ARUBA_HISPEED_CACHE_OPTIONS']['ahsc_purge_archive_on_edit'] || |
| 40 | AHSC_CONSTANT['ARUBA_HISPEED_CACHE_OPTIONS']['ahsc_purge_archive_on_del']*/ |
| 41 | ){ |
| 42 | |
| 43 | //if ( ahsc_has_transient( 'ahsc_is_purged' ) ) { |
| 44 | \add_action( 'transition_post_status', 'ahsc_transition_post_status' , 20, 3 ); |
| 45 | \add_action( 'pre_post_update', 'ahsc_get_terms_target' , 20, 1 ); |
| 46 | //} |
| 47 | |
| 48 | } |
| 49 | } |
| 50 | /** |
| 51 | * Fires after a post type has been updated, and the term cache has been cleaned. |
| 52 | * |
| 53 | * @see https://developer.wordpress.org/reference/hooks/post_updated/ |
| 54 | * or |
| 55 | * @see https://github.com/WordPress/WordPress/blob/master/wp-includes/post.php |
| 56 | * |
| 57 | * param int $post_ID Post ID. |
| 58 | * param WP_Post $post_after Post object following the update. |
| 59 | * param WP_Post $post_before Post object before the update. |
| 60 | * |
| 61 | * @return void |
| 62 | */ |
| 63 | function ahsc_post_updated($post_ID, $data) { |
| 64 | |
| 65 | $ahsc_ptlist=array('post','page','product','shop_order','shop_coupon','shop_webhook','attachment','nav_menu_item','wp_template','wp_template_part'); |
| 66 | $ahsc_cpt=get_post_type($post_ID); |
| 67 | /* if(class_exists('ArubaSPA\HiSpeedCache\Debug\Logger')) { |
| 68 | AHSC_log( "list:" . implode(",",$ahsc_ptlist) . " - current:" . $ahsc_cpt ." ==".var_export(array_search($ahsc_cpt,$ahsc_ptlist),true)); |
| 69 | }*/ |
| 70 | if(array_search($ahsc_cpt,$ahsc_ptlist)!==false){ |
| 71 | /** |
| 72 | * I check whether the cleaning transient "ahsc_do_purge_deferred" is present in case I inhibit this action. |
| 73 | */ |
| 74 | $do_purge = ahsc_has_transient( 'ahsc_do_purge_deferred' ); |
| 75 | |
| 76 | if ( $do_purge ) { |
| 77 | return; |
| 78 | } |
| 79 | /** |
| 80 | * Disable the purge action if the transient 'ahsc_is_purged' is set. |
| 81 | */ |
| 82 | if ( ahsc_has_transient( 'ahsc_is_purged' ) ) { |
| 83 | return; |
| 84 | } |
| 85 | |
| 86 | //$cleaner =new \ArubaSPA\HiSpeedCache\Purger\WpPurger(); |
| 87 | //$cleaner->setPurger( AHSC_PURGER ); |
| 88 | |
| 89 | /** |
| 90 | * If home page cleaning is set, there is no point in going any further, the entire site cache will be cleaned. |
| 91 | */ |
| 92 | |
| 93 | /*if(class_exists('ArubaSPA\HiSpeedCache\Debug\Logger')){ |
| 94 | // Logger. |
| 95 | AHSC_log( 'hook::post_updated::home', __NAMESPACE__ . '::' . __FUNCTION__, 'debug' ); |
| 96 | // Logger. |
| 97 | }*/ |
| 98 | //$cleaner->purgeAll(); |
| 99 | ahsc_set_transient( 'ahsc_do_purge_deferred', \time(), MINUTE_IN_SECONDS ); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Fires after a post type has been updated, and the term cache has been cleaned. |
| 105 | * |
| 106 | * @see https://developer.wordpress.org/reference/hooks/transition_post_status/ |
| 107 | * |
| 108 | * @param int|string $new_status new status. |
| 109 | * @param int|string $old_status old status. |
| 110 | * @param \WP_Post $post Post object. |
| 111 | * |
| 112 | * @return void |
| 113 | */ |
| 114 | function ahsc_transition_post_status( $new_status, $old_status, $post ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClassBeforeLastUsed |
| 115 | global $ahsc_allowed_cases,$ahsc_is_json,$ahsc_target; |
| 116 | /** |
| 117 | * Disable the purge action if the transient 'ahsc_is_purged' is set. |
| 118 | */ |
| 119 | if ( ahsc_has_transient( 'ahsc_is_purged' ) ) { |
| 120 | return; |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Disable the purge action on auto save. |
| 125 | * |
| 126 | * @see https://developer.wordpress.org/reference/functions/wp_is_post_autosave/ |
| 127 | */ |
| 128 | if ( \wp_is_post_autosave( $post ) ) { |
| 129 | return; |
| 130 | } |
| 131 | |
| 132 | if ( !isset($ahsc_allowed_cases) || !is_array( $ahsc_allowed_cases ) ){ |
| 133 | $ahsc_allowed_cases = array( 'publish', 'future', 'trash' ); |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Disable the purge action if the new status is not present in allorave casese array. |
| 138 | */ |
| 139 | if ( ! \in_array( $new_status, $ahsc_allowed_cases, true ) ) { |
| 140 | return; |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * For json call |
| 145 | */ |
| 146 | if ( $ahsc_is_json ) { |
| 147 | $do_purge = ahsc_has_transient( 'ahsc_do_purge_deferred' ); |
| 148 | $do_purge_log_message = 'Transint presente'; |
| 149 | |
| 150 | if ( ! $do_purge ) { |
| 151 | $do_purge_log_message = 'Transint non presente presente lo imposto'; |
| 152 | ahsc_set_transient( 'ahsc_do_purge_deferred', \time(), MINUTE_IN_SECONDS ); |
| 153 | |
| 154 | $cache_warmer =new \ArubaSPA\HiSpeedCache\Purger\WpPurger(); |
| 155 | $cache_warmer->cache_warmer(); |
| 156 | } |
| 157 | if(class_exists('ArubaSPA\HiSpeedCache\Debug\Logger')){ |
| 158 | AHSC_log( $do_purge_log_message, 'deferred', 'info' ); |
| 159 | } |
| 160 | return; |
| 161 | } |
| 162 | |
| 163 | $cleaner = new \ArubaSPA\HiSpeedCache\Purger\WpPurger(); |
| 164 | $cleaner->setPurger( AHSC_PURGER ); |
| 165 | //$plugin_option = $this->container->get_service( 'ahsc_get_option' ); |
| 166 | |
| 167 | $options = array( |
| 168 | 'log_function' => __FUNCTION__, |
| 169 | 'log_option' => 'ahsc_purge', |
| 170 | 'is_publish_or_future' => \in_array( $new_status, array( 'publish', 'future' ), true ), |
| 171 | 'is_trashed' => 'trash' === $new_status, |
| 172 | 'post' => $post, |
| 173 | ); |
| 174 | |
| 175 | /** |
| 176 | * If home page cleaning is set, there is no point in going any further, the entire site cache will be cleaned. |
| 177 | */ |
| 178 | if ( true === $options['is_publish_or_future'] && AHSC_CONSTANT['ARUBA_HISPEED_CACHE_OPTIONS']['ahsc_purge_homepage_on_edit'] ) { |
| 179 | if(class_exists('ArubaSPA\HiSpeedCache\Debug\Logger')){ |
| 180 | // Logger. |
| 181 | AHSC_log( 'hook::transition_post_status::home::' . (string) $options['log_option'], __NAMESPACE__ . '::' . (string) $options['log_function'], 'debug' ); |
| 182 | // Logger. |
| 183 | } |
| 184 | $cleaner->purgeAll(); |
| 185 | ahsc_set_transient( 'ahsc_is_purged', \time(), MINUTE_IN_SECONDS ); |
| 186 | return; |
| 187 | } |
| 188 | |
| 189 | if ( true === $options['is_trashed'] && AHSC_CONSTANT['ARUBA_HISPEED_CACHE_OPTIONS']['ahsc_purge_homepage_on_del'] ) { |
| 190 | if(class_exists('ArubaSPA\HiSpeedCache\Debug\Logger')){ |
| 191 | // Logger. |
| 192 | AHSC_log( 'hook::transition_post_status::home::ahsc_purge_archive_on_del', __NAMESPACE__ . '::' . __FUNCTION__, 'debug' ); |
| 193 | // Logger. |
| 194 | } |
| 195 | $cleaner->purgeAll(); |
| 196 | |
| 197 | ahsc_set_transient( 'ahsc_is_purged', \time(), MINUTE_IN_SECONDS ); |
| 198 | return; |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | * Edit item |
| 203 | */ |
| 204 | if ( AHSC_CONSTANT['ARUBA_HISPEED_CACHE_OPTIONS']['ahsc_purge_page_on_mod'] ) { |
| 205 | $options['log_option'] = $options['log_option'] . '_page_on_mod'; |
| 206 | $options['target'] = \get_permalink( $post->ID ); |
| 207 | |
| 208 | ahsc_post_mod_cache_cleaner( $options ); |
| 209 | return; |
| 210 | } |
| 211 | |
| 212 | $taxonomies = \get_object_taxonomies( $post->post_type ); |
| 213 | if ( empty( $taxonomies ) ) { |
| 214 | return; |
| 215 | } |
| 216 | |
| 217 | if ( AHSC_CONSTANT['ARUBA_HISPEED_CACHE_OPTIONS']['ahsc_purge_archive_on_edit'] && true === $options['is_publish_or_future'] ) { |
| 218 | $options['log_option'] = $options['log_option'] . '_archive_on_edit'; |
| 219 | $options['target'] = $ahsc_target; |
| 220 | |
| 221 | ahsc_post_mod_cache_cleaner( $options ); |
| 222 | return; |
| 223 | } |
| 224 | |
| 225 | /** |
| 226 | * Delete items |
| 227 | */ |
| 228 | if ( AHSC_CONSTANT['ARUBA_HISPEED_CACHE_OPTIONS']['ahsc_purge_archive_on_del'] && true === $options['is_trashed'] ) { |
| 229 | $options['log_option'] = $options['log_option'] . '_archive_on_del'; |
| 230 | $options['target'] = $ahsc_target; |
| 231 | |
| 232 | ahsc_post_mod_cache_cleaner( $options ); |
| 233 | return; |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | /** |
| 238 | * Proxy cache cleaner based on passed options. |
| 239 | * |
| 240 | * @param array $arg . |
| 241 | * @return void |
| 242 | */ |
| 243 | function ahsc_post_mod_cache_cleaner( $arg ) { |
| 244 | $cleaner = new \ArubaSPA\HiSpeedCache\Purger\WpPurger(); |
| 245 | $cleaner->setPurger( AHSC_PURGER ); |
| 246 | $ahsc_target = $arg['target']; |
| 247 | |
| 248 | if ( ! \is_array( $ahsc_target ) ) { |
| 249 | |
| 250 | if(class_exists('ArubaSPA\HiSpeedCache\Debug\Logger')){ |
| 251 | // Logger. |
| 252 | AHSC_log( |
| 253 | 'hook::transition_post_status::' . (string) $arg['log_option'] . '::' . $ahsc_target, |
| 254 | __NAMESPACE__ . '::' . (string) $arg['log_function'], |
| 255 | 'debug' |
| 256 | ); |
| 257 | // Logger. |
| 258 | } |
| 259 | $cleaner->purgeUrl( $ahsc_target ); |
| 260 | } |
| 261 | |
| 262 | if ( \is_array( $ahsc_target ) ) { |
| 263 | // Logger. |
| 264 | if(class_exists('ArubaSPA\HiSpeedCache\Debug\Logger')) { |
| 265 | // Logger. |
| 266 | AHSC_log( |
| 267 | 'hook::transition_post_status::' . (string) $arg['log_option'] . "::\n" . \implode( "::\n", $ahsc_target ), |
| 268 | __NAMESPACE__ . '::' . (string) $arg['log_function'], |
| 269 | 'debug' |
| 270 | ); |
| 271 | // Logger. |
| 272 | } |
| 273 | $cleaner->purgeUrls( $ahsc_target ); |
| 274 | } |
| 275 | |
| 276 | ahsc_set_transient( 'ahsc_is_purged', \time(), MINUTE_IN_SECONDS ); |
| 277 | } |
| 278 | |
| 279 | /** |
| 280 | * Returns the list of taxonomies to be passed as targets. |
| 281 | * |
| 282 | * @param int $post_id The taxonomies lists. |
| 283 | * @return void |
| 284 | */ |
| 285 | function ahsc_get_terms_target( $post_id ) { |
| 286 | global $ahsc_target; |
| 287 | |
| 288 | $post_type = \get_post_type( $post_id ); |
| 289 | $taxonomies = \get_object_taxonomies( $post_type ); |
| 290 | $_ahsc_target = array(); |
| 291 | |
| 292 | foreach ( $taxonomies as $tax ) { |
| 293 | $post_term_list = \get_the_terms( $post_id, $tax ); |
| 294 | |
| 295 | if ( false === $post_term_list ) { |
| 296 | continue; |
| 297 | } |
| 298 | |
| 299 | foreach ( \get_the_terms( $post_id, $tax ) as $tt ) { |
| 300 | $_ahsc_target[] = \get_term_link( $tt->term_id, $tax ); |
| 301 | } |
| 302 | } |
| 303 | $ahsc_target = $_ahsc_target; |
| 304 | } |