| @@ -8,8 +8,9 @@ | ||
| 8 | 8 | namespace RadiusTheme\SB\Controllers\Builder; |
| 9 | 9 | |
| 10 | 10 | defined( 'ABSPATH' ) || exit(); |
| 11 | 11 | |
| 12 | +use RadiusTheme\SB\Models\TemplateSettings; | |
| 12 | 13 | use WP_Query; |
| 13 | 14 | use RadiusTheme\SB\Helpers\Fns; |
| 14 | 15 | use RadiusTheme\SB\Helpers\BuilderFns; |
| 15 | 16 | use RadiusTheme\SB\Traits\SingletonTrait; |
| @@ -34,18 +35,50 @@ | ||
| 34 | 35 | add_action( 'manage_' . BuilderFns::$post_type_tb . '_posts_custom_column', [ $this, 'custom_columns' ], 10, 2 ); |
| 35 | 36 | add_filter( 'manage_edit-' . BuilderFns::$post_type_tb . '_sortable_columns', [ $this, 'register_sortable_columns' ] ); |
| 36 | 37 | add_action( 'pre_get_posts', [ $this, 'sortable_columns_query' ] ); |
| 37 | 38 | add_filter( 'template_include', [ $this, 'builder_template' ], 99 ); |
| 39 | + add_filter( 'template_redirect', [ $this, 'restrict_preview_access' ], 99 ); | |
| 38 | 40 | |
| 39 | 41 | add_filter( 'parse_query', [ $this, 'query_filter' ] ); |
| 40 | - // add filter for search | |
| 42 | + // Add filter for search. | |
| 41 | 43 | add_action( 'restrict_manage_posts', [ $this, 'add_filter' ] ); |
| 44 | + // Post Content Remove For Elementor Builder. | |
| 45 | + add_action( 'save_post_' . BuilderFns::$post_type_tb, [ $this, 'on_save_post' ], 10, 2 ); | |
| 46 | + } | |
| 42 | 47 | |
| 43 | - add_action( 'in_admin_header', [ $this, 'remove_all_notices' ], 1000 ); | |
| 44 | - | |
| 48 | + /** | |
| 49 | + * Hook into post save to clear post content for specific post type and editor. | |
| 50 | + * | |
| 51 | + * This method checks if the post is being updated (not newly created), | |
| 52 | + * and if the post type matches the target type defined in BuilderFns::$post_type_tb. | |
| 53 | + * If the post is edited with Elementor, it clears the post_content directly | |
| 54 | + * from the database and clears the post cache. | |
| 55 | + * | |
| 56 | + * @param int $post_ID ID of the post being saved. | |
| 57 | + * @param WP_Post $post Post object. | |
| 58 | + * | |
| 59 | + * @return void | |
| 60 | + */ | |
| 61 | + public function on_save_post( $post_ID, $post ) { | |
| 62 | + // Only proceed on update and specific post type. | |
| 63 | + if ( BuilderFns::$post_type_tb !== $post->post_type ) { | |
| 64 | + return; | |
| 65 | + } | |
| 66 | + if ( 'elementor' !== Fns::page_edit_with( $post_ID ) ) { | |
| 67 | + return; | |
| 68 | + } | |
| 69 | + global $wpdb; | |
| 70 | + // Directly update the post_content to empty. | |
| 71 | + // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery | |
| 72 | + $wpdb->update( | |
| 73 | + $wpdb->posts, | |
| 74 | + [ 'post_content' => '' ], | |
| 75 | + [ 'ID' => $post_ID ] | |
| 76 | + ); | |
| 77 | + // Clear cache so changes reflect properly. | |
| 78 | + clean_post_cache( $post_ID ); | |
| 45 | 79 | } |
| 46 | 80 | |
| 47 | - | |
| 48 | 81 | /** |
| 49 | 82 | * Public function add_filter. |
| 50 | 83 | * Added search filter for type of template |
| 51 | 84 | * |
| @@ -54,12 +87,12 @@ | ||
| 54 | 87 | public function add_filter() { |
| 55 | 88 | |
| 56 | 89 | global $typenow; |
| 57 | 90 | |
| 58 | - if ( $typenow != BuilderFns::$post_type_tb ) { | |
| 91 | + if ( BuilderFns::$post_type_tb != $typenow ) { | |
| 59 | 92 | return; |
| 60 | 93 | } |
| 61 | - // phpcs:ignore WordPress.Security.NonceVerification.Recommended. No need nonce. | |
| 94 | + // phpcs:ignore WordPress.Security.NonceVerification.Recommended | |
| 62 | 95 | $selected = isset( $_GET['type'] ) ? sanitize_key( $_GET['type'] ) : ''; |
| 63 | 96 | $builder_page_types = BuilderFns::builder_page_types(); |
| 64 | 97 | ?> |
| 65 | 98 | <select name="template_type" id="type"> |
| @@ -68,9 +101,8 @@ | ||
| 68 | 101 | <option value="<?php echo esc_attr( $key ); ?>" <?php selected( $key, $selected ); ?>><?php echo esc_html( $value ); ?></option> |
| 69 | 102 | <?php } ?> |
| 70 | 103 | </select> |
| 71 | 104 | <?php |
| 72 | - | |
| 73 | 105 | } |
| 74 | 106 | |
| 75 | 107 | /** |
| 76 | 108 | * Post type function |
| @@ -130,10 +162,10 @@ | ||
| 130 | 162 | 'with_front' => true, |
| 131 | 163 | 'feeds' => false, |
| 132 | 164 | ], |
| 133 | 165 | 'query_var' => true, |
| 134 | - 'publicly_queryable' => apply_filters( 'rtsb/builder/args/publicly_queryable', true ), | |
| 135 | - 'capability_type' => 'product', | |
| 166 | + 'publicly_queryable' => true, | |
| 167 | + // 'capability_type' => 'product', Will Remove this line in the future. | |
| 136 | 168 | 'show_in_rest' => true, |
| 137 | 169 | 'rest_base' => BuilderFns::$post_type_tb, |
| 138 | 170 | ]; |
| 139 | 171 | |
| @@ -142,9 +174,8 @@ | ||
| 142 | 174 | register_post_type( BuilderFns::$post_type_tb, $tb_args ); |
| 143 | 175 | |
| 144 | 176 | // Flash rewrite rules. |
| 145 | 177 | $this->flush_rewrite_rules(); |
| 146 | - | |
| 147 | 178 | } |
| 148 | 179 | |
| 149 | 180 | /** |
| 150 | 181 | * Flush rewrite |
| @@ -188,17 +219,17 @@ | ||
| 188 | 219 | * @param string $post_id Post id. |
| 189 | 220 | * |
| 190 | 221 | * @return void |
| 191 | 222 | */ |
| 192 | - public function custom_columns( $column, $post_id ) { | |
| 193 | - $post_id = absint( $post_id ); | |
| 194 | - $types = BuilderFns::builder_page_types(); | |
| 195 | - $type = BuilderFns::builder_type( $post_id ) ? BuilderFns::builder_type( $post_id ) : array_key_first( $types ); | |
| 223 | + public function custom_columns( $column, $post_id ) { // phpcs:ignore Generic.Metrics.NestingLevel.TooHigh | |
| 224 | + $post_id = absint( $post_id ); | |
| 225 | + $types = BuilderFns::builder_page_types(); | |
| 226 | + $template_type = BuilderFns::builder_type( $post_id ) ? BuilderFns::builder_type( $post_id ) : array_key_first( $types ); | |
| 196 | 227 | |
| 197 | 228 | switch ( $column ) { |
| 198 | 229 | case 'type': |
| 199 | - $the_type = ! empty( $types[ $type ] ) ? esc_html( ucwords( $types[ $type ] ) ) : ''; | |
| 200 | - echo apply_filters( 'rtsb/columns/template/types/text', $the_type, $type, $post_id ); | |
| 230 | + $the_type = ! empty( $types[ $template_type ] ) ? '<b>' . esc_html( ucwords( $types[ $template_type ] ) ) . '</b>' : ''; | |
| 231 | + Fns::print_html( apply_filters( 'rtsb/columns/template/types/text', $the_type, $template_type, $post_id ) ); | |
| 201 | 232 | break; |
| 202 | 233 | case 'edit_with': |
| 203 | 234 | $edit_by = Fns::page_edit_with( $post_id ); |
| 204 | 235 | echo esc_html( ucfirst( $edit_by ) ); |
| @@ -203,31 +234,63 @@ | ||
| 203 | 234 | $edit_by = Fns::page_edit_with( $post_id ); |
| 204 | 235 | echo esc_html( ucfirst( $edit_by ) ); |
| 205 | 236 | break; |
| 206 | 237 | case 'set_default': |
| 207 | - $is_set_default = absint( BuilderFns::builder_page_id_by_type( $type ) ); | |
| 238 | + $is_set_default = absint( BuilderFns::builder_page_id_by_type( $template_type ) ); | |
| 208 | 239 | $is_published = 'publish' === get_post_status( $post_id ); |
| 209 | - $page_type_for = $type; | |
| 210 | - if ( 'product' === $type ) { | |
| 211 | - $product_ids = apply_filters( 'rtsb/product/page/builder/selected/products', [], $post_id ); | |
| 212 | - $set_default = BuilderFns::get_specific_product_as_default( $post_id ); | |
| 213 | - if ( ! empty( $product_ids ) && $set_default ) { | |
| 214 | - $is_set_default = $post_id; | |
| 215 | - $page_type_for = 'template-' . $post_id . '-specific-products'; | |
| 240 | + $page_type_for = $template_type; | |
| 241 | + if ( 'product' === $template_type ) { | |
| 242 | + $product_page_for = get_post_meta( $post_id, '_is_product_page_template_for', true ); | |
| 243 | + switch ( $product_page_for ) { | |
| 244 | + case 'specific_products': | |
| 245 | + $page_type_for = 'template-' . $post_id . '-specific-products'; | |
| 246 | + $set_default = BuilderFns::get_specific_product_as_default( $post_id ); | |
| 247 | + if ( $set_default ) { | |
| 248 | + $is_set_default = $post_id; | |
| 249 | + } | |
| 250 | + if ( ! $set_default && $is_set_default === $post_id ) { | |
| 251 | + $is_set_default = ''; | |
| 252 | + } | |
| 253 | + break; | |
| 254 | + case 'product_cats': | |
| 255 | + $page_type_for = 'product-page-template-' . $post_id . '-specific-category'; | |
| 256 | + $set_default_option_name = BuilderFns::option_name_product_page_specific_cat_set_default( $post_id ); | |
| 257 | + $pp_cat_set_default = TemplateSettings::instance()->get_option( $set_default_option_name ); | |
| 258 | + if ( $pp_cat_set_default ) { | |
| 259 | + $is_set_default = $post_id; | |
| 260 | + } | |
| 261 | + break; | |
| 262 | + case 'product_tags': | |
| 263 | + $page_type_for = 'product-page-template-' . $post_id . '-specific-tag'; | |
| 264 | + $set_default_option_name = BuilderFns::option_name_product_page_specific_tag_set_default( $post_id ); | |
| 265 | + $pp_tag_set_default = TemplateSettings::instance()->get_option( $set_default_option_name ); | |
| 266 | + if ( $pp_tag_set_default ) { | |
| 267 | + $is_set_default = $post_id; | |
| 268 | + } | |
| 269 | + break; | |
| 270 | + default: | |
| 271 | + break; | |
| 216 | 272 | } |
| 217 | - } elseif ( 'archive' === $type ) { | |
| 218 | - $categories_name = apply_filters( 'rtsb/archive/page/builder/selected/categories', [], $post_id ); | |
| 273 | + } elseif ( 'archive' === $template_type ) { | |
| 274 | + $categories_name = apply_filters( 'rtsb/template/builder/selected/categories', [], $post_id, $template_type ); | |
| 219 | 275 | $set_default = BuilderFns::get_specific_category_as_default( $post_id ); |
| 220 | 276 | if ( ! empty( $categories_name ) && $set_default ) { |
| 221 | 277 | $is_set_default = $post_id; |
| 222 | 278 | $page_type_for = 'template-' . $post_id . '-specific-category'; |
| 279 | + } else { | |
| 280 | + $option_name = BuilderFns::option_name( $template_type ); | |
| 281 | + $is_set_default = TemplateSettings::instance()->get_option( $option_name ); | |
| 282 | + $set_default = $is_set_default; | |
| 223 | 283 | } |
| 284 | + if ( ! $set_default && $is_set_default === $post_id ) { | |
| 285 | + $is_set_default = ''; | |
| 286 | + } | |
| 224 | 287 | } |
| 225 | 288 | ?> |
| 226 | 289 | <span class="rtsb-switch-wrapper page-type-<?php echo esc_attr( $page_type_for ); ?>" <?php echo ! $is_published ? 'style="pointer-events: none;"' : null; ?> title="<?php esc_html_e( 'Only publish post can set default ', 'shopbuilder' ); ?>"> |
| 227 | 290 | <label class="rtsb-switch"> |
| 228 | - <?php do_action( 'rtsb/set/default/column/extra/field', $post_id, $type, $is_set_default, $is_published ); ?> | |
| 229 | - <div class="rtsb_template_type" data-template_type="<?php echo esc_attr( $type ); ?>" style='display:none;'></div> | |
| 291 | + <?php do_action( 'rtsb/set/default/column/extra/field', $post_id, $template_type, $is_set_default, $is_published ); ?> | |
| 292 | + <div class="rtsb_template_type" data-template_type="<?php echo esc_attr( $template_type ); ?>" style='display:none;'></div> | |
| 230 | 293 | <input disabled="disabled" value="<?php echo absint( $post_id ); ?>" class="rtsb_set_default" name="set_default" type="checkbox" <?php echo esc_attr( $post_id === $is_set_default ? 'checked' : '' ); ?> > |
| 231 | 294 | <span class="rtsb-slider rtsb-round "> |
| 232 | 295 | <span class="rtsb-loader"></span> |
| 233 | 296 | </span> |
| @@ -291,13 +354,14 @@ | ||
| 291 | 354 | |
| 292 | 355 | if ( ! is_admin() || ! $this->is_current_screen() || ! empty( $query->query_vars['meta_key'] ) ) { |
| 293 | 356 | return; |
| 294 | 357 | } |
| 358 | + // phpcs:ignore WordPress.Security.NonceVerification.Recommended | |
| 295 | 359 | $type = isset( $_GET['template_type'] ) ? sanitize_key( $_GET['template_type'] ) : ''; |
| 296 | 360 | |
| 297 | 361 | if ( '' != $type && 'all' != $type ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 298 | 362 | $query->query_vars['meta_key'] = BuilderFns::template_type_meta_key(); // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key |
| 299 | - $query->query_vars['meta_value'] = $type; | |
| 363 | + $query->query_vars['meta_value'] = $type; // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value | |
| 300 | 364 | $query->query_vars['meta_compare'] = '='; |
| 301 | 365 | } |
| 302 | 366 | } |
| 303 | 367 | |
| @@ -309,9 +373,9 @@ | ||
| 309 | 373 | * @return string |
| 310 | 374 | */ |
| 311 | 375 | public function builder_template( $template ) { |
| 312 | 376 | $builder_page_id = BuilderFns::is_builder_preview() ? get_the_ID() : BuilderFns::builder_page_id_by_page(); |
| 313 | - if ( $builder_page_id ) { | |
| 377 | + if ( defined( 'ELEMENTOR_VERSION' ) && $builder_page_id ) { // Elementor Check. | |
| 314 | 378 | $page_template = get_post_meta( $builder_page_id, '_wp_page_template', true ); |
| 315 | 379 | $new_template = RTSB_ABSPATH . 'page-templates/builder-template.php'; |
| 316 | 380 | if ( 'elementor_canvas' === $page_template ) { |
| 317 | 381 | $new_template = RTSB_ABSPATH . 'page-templates/elementor-canvas.php'; |
| @@ -326,8 +390,25 @@ | ||
| 326 | 390 | return $template; |
| 327 | 391 | } |
| 328 | 392 | |
| 329 | 393 | /** |
| 394 | + * Restrict the preview access. | |
| 395 | + * | |
| 396 | + * @return void | |
| 397 | + */ | |
| 398 | + public function restrict_preview_access() { | |
| 399 | + if ( ! apply_filters( 'rtsb/builder/preview/access', true ) ) { | |
| 400 | + return; | |
| 401 | + } | |
| 402 | + | |
| 403 | + if ( is_singular( 'rtsb_builder' ) ) { | |
| 404 | + if ( ! is_user_logged_in() ) { | |
| 405 | + wp_safe_redirect( home_url() ); | |
| 406 | + } | |
| 407 | + } | |
| 408 | + } | |
| 409 | + | |
| 410 | + /** | |
| 330 | 411 | * Add/Remove edit link in dashboard. |
| 331 | 412 | * |
| 332 | 413 | * Add or remove an edit link to the post/page action links on the post/pages list table. |
| 333 | 414 | * |
| @@ -340,22 +421,14 @@ | ||
| 340 | 421 | * @return array An updated array of row action links. |
| 341 | 422 | */ |
| 342 | 423 | public function filter_post_row_actions( $actions ) { |
| 343 | 424 | if ( $this->is_current_screen() ) { |
| 344 | - // unset( $actions['view'] ); | |
| 345 | 425 | unset( $actions['inline hide-if-no-js'] ); |
| 426 | + | |
| 427 | + if ( isset( $actions['view'] ) ) { | |
| 428 | + $actions['view'] = str_replace( __( 'View', 'shopbuilder' ), __( 'Preview', 'shopbuilder' ), $actions['view'] ); | |
| 429 | + } | |
| 346 | 430 | } |
| 347 | 431 | |
| 348 | 432 | return $actions; |
| 349 | - } | |
| 350 | - | |
| 351 | - /** | |
| 352 | - * Remove admin notices | |
| 353 | - */ | |
| 354 | - public function remove_all_notices() { | |
| 355 | - $screen = get_current_screen(); | |
| 356 | - if ( 'edit-rtsb_builder' === $screen->id ) { | |
| 357 | - remove_all_actions( 'admin_notices' ); | |
| 358 | - remove_all_actions( 'all_admin_notices' ); | |
| 359 | - } | |
| 360 | 433 | } |
| 361 | 434 | } |