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