| @@ -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,51 @@ | ||
| 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 | + * @param bool $update Whether this is an existing post being updated. | |
| 59 | + * | |
| 60 | + * @return void | |
| 61 | + */ | |
| 62 | + public function on_save_post( $post_ID, $post ) { | |
| 63 | + // Only proceed on update and specific post type. | |
| 64 | + if ( BuilderFns::$post_type_tb !== $post->post_type ) { | |
| 65 | + return; | |
| 66 | + } | |
| 67 | + if ( 'elementor' !== Fns::page_edit_with( $post_ID ) ) { | |
| 68 | + return; | |
| 69 | + } | |
| 70 | + global $wpdb; | |
| 71 | + // Directly update the post_content to empty. | |
| 72 | + // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery | |
| 73 | + $wpdb->update( | |
| 74 | + $wpdb->posts, | |
| 75 | + [ 'post_content' => '' ], | |
| 76 | + [ 'ID' => $post_ID ] | |
| 77 | + ); | |
| 78 | + // Clear cache so changes reflect properly. | |
| 79 | + clean_post_cache( $post_ID ); | |
| 45 | 80 | } |
| 46 | 81 | |
| 47 | - | |
| 48 | 82 | /** |
| 49 | 83 | * Public function add_filter. |
| 50 | 84 | * Added search filter for type of template |
| 51 | 85 | * |
| @@ -68,9 +102,8 @@ | ||
| 68 | 102 | <option value="<?php echo esc_attr( $key ); ?>" <?php selected( $key, $selected ); ?>><?php echo esc_html( $value ); ?></option> |
| 69 | 103 | <?php } ?> |
| 70 | 104 | </select> |
| 71 | 105 | <?php |
| 72 | - | |
| 73 | 106 | } |
| 74 | 107 | |
| 75 | 108 | /** |
| 76 | 109 | * Post type function |
| @@ -130,10 +163,10 @@ | ||
| 130 | 163 | 'with_front' => true, |
| 131 | 164 | 'feeds' => false, |
| 132 | 165 | ], |
| 133 | 166 | 'query_var' => true, |
| 134 | - 'publicly_queryable' => apply_filters( 'rtsb/builder/args/publicly_queryable', true ), | |
| 135 | - 'capability_type' => 'product', | |
| 167 | + 'publicly_queryable' => true, | |
| 168 | + // 'capability_type' => 'product', Will Remove this line in the future. | |
| 136 | 169 | 'show_in_rest' => true, |
| 137 | 170 | 'rest_base' => BuilderFns::$post_type_tb, |
| 138 | 171 | ]; |
| 139 | 172 | |
| @@ -142,9 +175,8 @@ | ||
| 142 | 175 | register_post_type( BuilderFns::$post_type_tb, $tb_args ); |
| 143 | 176 | |
| 144 | 177 | // Flash rewrite rules. |
| 145 | 178 | $this->flush_rewrite_rules(); |
| 146 | - | |
| 147 | 179 | } |
| 148 | 180 | |
| 149 | 181 | /** |
| 150 | 182 | * Flush rewrite |
| @@ -189,16 +221,16 @@ | ||
| 189 | 221 | * |
| 190 | 222 | * @return void |
| 191 | 223 | */ |
| 192 | 224 | 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 ); | |
| 225 | + $post_id = absint( $post_id ); | |
| 226 | + $types = BuilderFns::builder_page_types(); | |
| 227 | + $template_type = BuilderFns::builder_type( $post_id ) ? BuilderFns::builder_type( $post_id ) : array_key_first( $types ); | |
| 196 | 228 | |
| 197 | 229 | switch ( $column ) { |
| 198 | 230 | case 'type': |
| 199 | - $the_type = ! empty( $types[ $type ] ) ? esc_html( ucwords( $types[ $type ] ) ) : ''; | |
| 200 | - Fns::print_html( apply_filters( 'rtsb/columns/template/types/text', $the_type, $type, $post_id ) ); | |
| 231 | + $the_type = ! empty( $types[ $template_type ] ) ? '<b>' . esc_html( ucwords( $types[ $template_type ] ) ) . '</b>' : ''; | |
| 232 | + Fns::print_html( apply_filters( 'rtsb/columns/template/types/text', $the_type, $template_type, $post_id ) ); | |
| 201 | 233 | break; |
| 202 | 234 | case 'edit_with': |
| 203 | 235 | $edit_by = Fns::page_edit_with( $post_id ); |
| 204 | 236 | echo esc_html( ucfirst( $edit_by ) ); |
| @@ -203,20 +235,42 @@ | ||
| 203 | 235 | $edit_by = Fns::page_edit_with( $post_id ); |
| 204 | 236 | echo esc_html( ucfirst( $edit_by ) ); |
| 205 | 237 | break; |
| 206 | 238 | case 'set_default': |
| 207 | - $is_set_default = absint( BuilderFns::builder_page_id_by_type( $type ) ); | |
| 239 | + $is_set_default = absint( BuilderFns::builder_page_id_by_type( $template_type ) ); | |
| 208 | 240 | $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'; | |
| 241 | + $page_type_for = $template_type; | |
| 242 | + if ( 'product' === $template_type ) { | |
| 243 | + $product_page_for = get_post_meta( $post_id, '_is_product_page_template_for', true ); | |
| 244 | + switch ( $product_page_for ) { | |
| 245 | + case 'specific_products': | |
| 246 | + $page_type_for = 'template-' . $post_id . '-specific-products'; | |
| 247 | + $set_default = BuilderFns::get_specific_product_as_default( $post_id ); | |
| 248 | + if ( $set_default ) { | |
| 249 | + $is_set_default = $post_id; | |
| 250 | + } | |
| 251 | + break; | |
| 252 | + case 'product_cats': | |
| 253 | + $page_type_for = 'product-page-template-' . $post_id . '-specific-category'; | |
| 254 | + $set_default_option_name = BuilderFns::option_name_product_page_specific_cat_set_default( $post_id ); | |
| 255 | + $pp_cat_set_default = TemplateSettings::instance()->get_option( $set_default_option_name ); | |
| 256 | + if ( $pp_cat_set_default ) { | |
| 257 | + $is_set_default = $post_id; | |
| 258 | + } | |
| 259 | + break; | |
| 260 | + case 'product_tags': | |
| 261 | + $page_type_for = 'product-page-template-' . $post_id . '-specific-tag'; | |
| 262 | + $set_default_option_name = BuilderFns::option_name_product_page_specific_tag_set_default( $post_id ); | |
| 263 | + $pp_tag_set_default = TemplateSettings::instance()->get_option( $set_default_option_name ); | |
| 264 | + if ( $pp_tag_set_default ) { | |
| 265 | + $is_set_default = $post_id; | |
| 266 | + } | |
| 267 | + break; | |
| 268 | + default: | |
| 269 | + break; | |
| 216 | 270 | } |
| 217 | - } elseif ( 'archive' === $type ) { | |
| 218 | - $categories_name = apply_filters( 'rtsb/archive/page/builder/selected/categories', [], $post_id ); | |
| 271 | + } elseif ( 'archive' === $template_type ) { | |
| 272 | + $categories_name = apply_filters( 'rtsb/template/builder/selected/categories', [], $post_id, $template_type ); | |
| 219 | 273 | $set_default = BuilderFns::get_specific_category_as_default( $post_id ); |
| 220 | 274 | if ( ! empty( $categories_name ) && $set_default ) { |
| 221 | 275 | $is_set_default = $post_id; |
| 222 | 276 | $page_type_for = 'template-' . $post_id . '-specific-category'; |
| @@ -224,10 +278,10 @@ | ||
| 224 | 278 | } |
| 225 | 279 | ?> |
| 226 | 280 | <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 | 281 | <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> | |
| 282 | + <?php do_action( 'rtsb/set/default/column/extra/field', $post_id, $template_type, $is_set_default, $is_published ); ?> | |
| 283 | + <div class="rtsb_template_type" data-template_type="<?php echo esc_attr( $template_type ); ?>" style='display:none;'></div> | |
| 230 | 284 | <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 | 285 | <span class="rtsb-slider rtsb-round "> |
| 232 | 286 | <span class="rtsb-loader"></span> |
| 233 | 287 | </span> |
| @@ -310,9 +364,9 @@ | ||
| 310 | 364 | * @return string |
| 311 | 365 | */ |
| 312 | 366 | public function builder_template( $template ) { |
| 313 | 367 | $builder_page_id = BuilderFns::is_builder_preview() ? get_the_ID() : BuilderFns::builder_page_id_by_page(); |
| 314 | - if ( $builder_page_id ) { | |
| 368 | + if ( defined( 'ELEMENTOR_VERSION' ) && $builder_page_id ) { // Elementor Check. | |
| 315 | 369 | $page_template = get_post_meta( $builder_page_id, '_wp_page_template', true ); |
| 316 | 370 | $new_template = RTSB_ABSPATH . 'page-templates/builder-template.php'; |
| 317 | 371 | if ( 'elementor_canvas' === $page_template ) { |
| 318 | 372 | $new_template = RTSB_ABSPATH . 'page-templates/elementor-canvas.php'; |
| @@ -327,8 +381,25 @@ | ||
| 327 | 381 | return $template; |
| 328 | 382 | } |
| 329 | 383 | |
| 330 | 384 | /** |
| 385 | + * Restrict the preview access. | |
| 386 | + * | |
| 387 | + * @return void | |
| 388 | + */ | |
| 389 | + public function restrict_preview_access() { | |
| 390 | + if ( ! apply_filters( 'rtsb/builder/preview/access', true ) ) { | |
| 391 | + return; | |
| 392 | + } | |
| 393 | + | |
| 394 | + if ( is_singular( 'rtsb_builder' ) ) { | |
| 395 | + if ( ! is_user_logged_in() ) { | |
| 396 | + wp_safe_redirect( home_url() ); | |
| 397 | + } | |
| 398 | + } | |
| 399 | + } | |
| 400 | + | |
| 401 | + /** | |
| 331 | 402 | * Add/Remove edit link in dashboard. |
| 332 | 403 | * |
| 333 | 404 | * Add or remove an edit link to the post/page action links on the post/pages list table. |
| 334 | 405 | * |
| @@ -343,20 +414,13 @@ | ||
| 343 | 414 | public function filter_post_row_actions( $actions ) { |
| 344 | 415 | if ( $this->is_current_screen() ) { |
| 345 | 416 | // unset( $actions['view'] ); |
| 346 | 417 | unset( $actions['inline hide-if-no-js'] ); |
| 418 | + | |
| 419 | + if ( isset( $actions['view'] ) ) { | |
| 420 | + $actions['view'] = str_replace( __( 'View', 'shopbuilder' ), __( 'Preview', 'shopbuilder' ), $actions['view'] ); | |
| 421 | + } | |
| 347 | 422 | } |
| 348 | 423 | |
| 349 | 424 | return $actions; |
| 350 | - } | |
| 351 | - | |
| 352 | - /** | |
| 353 | - * Remove admin notices | |
| 354 | - */ | |
| 355 | - public function remove_all_notices() { | |
| 356 | - $screen = get_current_screen(); | |
| 357 | - if ( 'edit-rtsb_builder' === $screen->id ) { | |
| 358 | - remove_all_actions( 'admin_notices' ); | |
| 359 | - remove_all_actions( 'all_admin_notices' ); | |
| 360 | - } | |
| 361 | 425 | } |
| 362 | 426 | } |