| @@ -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,15 +35,13 @@ | ||
| 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' ] ); |
| 42 | - | |
| 43 | - add_action( 'in_admin_header', [ $this, 'remove_all_notices' ], 1000 ); | |
| 44 | - | |
| 45 | 44 | } |
| 46 | 45 | |
| 47 | 46 | |
| 48 | 47 | /** |
| @@ -54,12 +53,12 @@ | ||
| 54 | 53 | public function add_filter() { |
| 55 | 54 | |
| 56 | 55 | global $typenow; |
| 57 | 56 | |
| 58 | - if ( $typenow != BuilderFns::$post_type_tb ) { | |
| 57 | + if ( BuilderFns::$post_type_tb != $typenow ) { | |
| 59 | 58 | return; |
| 60 | 59 | } |
| 61 | - // phpcs:ignore WordPress.Security.NonceVerification.Recommended. No need nonce. | |
| 60 | + // phpcs:ignore WordPress.Security.NonceVerification.Recommended | |
| 62 | 61 | $selected = isset( $_GET['type'] ) ? sanitize_key( $_GET['type'] ) : ''; |
| 63 | 62 | $builder_page_types = BuilderFns::builder_page_types(); |
| 64 | 63 | ?> |
| 65 | 64 | <select name="template_type" id="type"> |
| @@ -68,9 +67,8 @@ | ||
| 68 | 67 | <option value="<?php echo esc_attr( $key ); ?>" <?php selected( $key, $selected ); ?>><?php echo esc_html( $value ); ?></option> |
| 69 | 68 | <?php } ?> |
| 70 | 69 | </select> |
| 71 | 70 | <?php |
| 72 | - | |
| 73 | 71 | } |
| 74 | 72 | |
| 75 | 73 | /** |
| 76 | 74 | * Post type function |
| @@ -130,10 +128,10 @@ | ||
| 130 | 128 | 'with_front' => true, |
| 131 | 129 | 'feeds' => false, |
| 132 | 130 | ], |
| 133 | 131 | 'query_var' => true, |
| 134 | - 'publicly_queryable' => apply_filters( 'rtsb/builder/args/publicly_queryable', true ), | |
| 135 | - 'capability_type' => 'product', | |
| 132 | + 'publicly_queryable' => true, | |
| 133 | + // 'capability_type' => 'product', Will Remove this line in the future. | |
| 136 | 134 | 'show_in_rest' => true, |
| 137 | 135 | 'rest_base' => BuilderFns::$post_type_tb, |
| 138 | 136 | ]; |
| 139 | 137 | |
| @@ -142,9 +140,8 @@ | ||
| 142 | 140 | register_post_type( BuilderFns::$post_type_tb, $tb_args ); |
| 143 | 141 | |
| 144 | 142 | // Flash rewrite rules. |
| 145 | 143 | $this->flush_rewrite_rules(); |
| 146 | - | |
| 147 | 144 | } |
| 148 | 145 | |
| 149 | 146 | /** |
| 150 | 147 | * Flush rewrite |
| @@ -189,16 +186,16 @@ | ||
| 189 | 186 | * |
| 190 | 187 | * @return void |
| 191 | 188 | */ |
| 192 | 189 | 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 ); | |
| 190 | + $post_id = absint( $post_id ); | |
| 191 | + $types = BuilderFns::builder_page_types(); | |
| 192 | + $template_type = BuilderFns::builder_type( $post_id ) ? BuilderFns::builder_type( $post_id ) : array_key_first( $types ); | |
| 196 | 193 | |
| 197 | 194 | switch ( $column ) { |
| 198 | 195 | 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 ); | |
| 196 | + $the_type = ! empty( $types[ $template_type ] ) ? '<b>' . esc_html( ucwords( $types[ $template_type ] ) ) . '</b>' : ''; | |
| 197 | + Fns::print_html( apply_filters( 'rtsb/columns/template/types/text', $the_type, $template_type, $post_id ) ); | |
| 201 | 198 | break; |
| 202 | 199 | case 'edit_with': |
| 203 | 200 | $edit_by = Fns::page_edit_with( $post_id ); |
| 204 | 201 | echo esc_html( ucfirst( $edit_by ) ); |
| @@ -203,20 +200,42 @@ | ||
| 203 | 200 | $edit_by = Fns::page_edit_with( $post_id ); |
| 204 | 201 | echo esc_html( ucfirst( $edit_by ) ); |
| 205 | 202 | break; |
| 206 | 203 | case 'set_default': |
| 207 | - $is_set_default = absint( BuilderFns::builder_page_id_by_type( $type ) ); | |
| 204 | + $is_set_default = absint( BuilderFns::builder_page_id_by_type( $template_type ) ); | |
| 208 | 205 | $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'; | |
| 206 | + $page_type_for = $template_type; | |
| 207 | + if ( 'product' === $template_type ) { | |
| 208 | + $product_page_for = get_post_meta( $post_id, '_is_product_page_template_for', true ); | |
| 209 | + switch ( $product_page_for ) { | |
| 210 | + case 'specific_products': | |
| 211 | + $page_type_for = 'template-' . $post_id . '-specific-products'; | |
| 212 | + $set_default = BuilderFns::get_specific_product_as_default( $post_id ); | |
| 213 | + if ( $set_default ) { | |
| 214 | + $is_set_default = $post_id; | |
| 215 | + } | |
| 216 | + break; | |
| 217 | + case 'product_cats': | |
| 218 | + $page_type_for = 'product-page-template-' . $post_id . '-specific-category'; | |
| 219 | + $set_default_option_name = BuilderFns::option_name_product_page_specific_cat_set_default( $post_id ); | |
| 220 | + $pp_cat_set_default = TemplateSettings::instance()->get_option( $set_default_option_name ); | |
| 221 | + if ( $pp_cat_set_default ) { | |
| 222 | + $is_set_default = $post_id; | |
| 223 | + } | |
| 224 | + break; | |
| 225 | + case 'product_tags': | |
| 226 | + $page_type_for = 'product-page-template-' . $post_id . '-specific-tag'; | |
| 227 | + $set_default_option_name = BuilderFns::option_name_product_page_specific_tag_set_default( $post_id ); | |
| 228 | + $pp_tag_set_default = TemplateSettings::instance()->get_option( $set_default_option_name ); | |
| 229 | + if ( $pp_tag_set_default ) { | |
| 230 | + $is_set_default = $post_id; | |
| 231 | + } | |
| 232 | + break; | |
| 233 | + default: | |
| 234 | + break; | |
| 216 | 235 | } |
| 217 | - } elseif ( 'archive' === $type ) { | |
| 218 | - $categories_name = apply_filters( 'rtsb/archive/page/builder/selected/categories', [], $post_id ); | |
| 236 | + } elseif ( 'archive' === $template_type ) { | |
| 237 | + $categories_name = apply_filters( 'rtsb/template/builder/selected/categories', [], $post_id, $template_type ); | |
| 219 | 238 | $set_default = BuilderFns::get_specific_category_as_default( $post_id ); |
| 220 | 239 | if ( ! empty( $categories_name ) && $set_default ) { |
| 221 | 240 | $is_set_default = $post_id; |
| 222 | 241 | $page_type_for = 'template-' . $post_id . '-specific-category'; |
| @@ -224,10 +243,10 @@ | ||
| 224 | 243 | } |
| 225 | 244 | ?> |
| 226 | 245 | <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 | 246 | <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> | |
| 247 | + <?php do_action( 'rtsb/set/default/column/extra/field', $post_id, $template_type, $is_set_default, $is_published ); ?> | |
| 248 | + <div class="rtsb_template_type" data-template_type="<?php echo esc_attr( $template_type ); ?>" style='display:none;'></div> | |
| 230 | 249 | <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 | 250 | <span class="rtsb-slider rtsb-round "> |
| 232 | 251 | <span class="rtsb-loader"></span> |
| 233 | 252 | </span> |
| @@ -291,13 +310,14 @@ | ||
| 291 | 310 | |
| 292 | 311 | if ( ! is_admin() || ! $this->is_current_screen() || ! empty( $query->query_vars['meta_key'] ) ) { |
| 293 | 312 | return; |
| 294 | 313 | } |
| 314 | + // phpcs:ignore WordPress.Security.NonceVerification.Recommended | |
| 295 | 315 | $type = isset( $_GET['template_type'] ) ? sanitize_key( $_GET['template_type'] ) : ''; |
| 296 | 316 | |
| 297 | 317 | if ( '' != $type && 'all' != $type ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 298 | 318 | $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; | |
| 319 | + $query->query_vars['meta_value'] = $type; // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value | |
| 300 | 320 | $query->query_vars['meta_compare'] = '='; |
| 301 | 321 | } |
| 302 | 322 | } |
| 303 | 323 | |
| @@ -309,9 +329,9 @@ | ||
| 309 | 329 | * @return string |
| 310 | 330 | */ |
| 311 | 331 | public function builder_template( $template ) { |
| 312 | 332 | $builder_page_id = BuilderFns::is_builder_preview() ? get_the_ID() : BuilderFns::builder_page_id_by_page(); |
| 313 | - if ( $builder_page_id ) { | |
| 333 | + if ( defined( 'ELEMENTOR_VERSION' ) && $builder_page_id ) { // Elementor Check. | |
| 314 | 334 | $page_template = get_post_meta( $builder_page_id, '_wp_page_template', true ); |
| 315 | 335 | $new_template = RTSB_ABSPATH . 'page-templates/builder-template.php'; |
| 316 | 336 | if ( 'elementor_canvas' === $page_template ) { |
| 317 | 337 | $new_template = RTSB_ABSPATH . 'page-templates/elementor-canvas.php'; |
| @@ -326,8 +346,25 @@ | ||
| 326 | 346 | return $template; |
| 327 | 347 | } |
| 328 | 348 | |
| 329 | 349 | /** |
| 350 | + * Restrict the preview access. | |
| 351 | + * | |
| 352 | + * @return void | |
| 353 | + */ | |
| 354 | + public function restrict_preview_access() { | |
| 355 | + if ( ! apply_filters( 'rtsb/builder/preview/access', true ) ) { | |
| 356 | + return; | |
| 357 | + } | |
| 358 | + | |
| 359 | + if ( is_singular( 'rtsb_builder' ) ) { | |
| 360 | + if ( ! is_user_logged_in() ) { | |
| 361 | + wp_safe_redirect( home_url() ); | |
| 362 | + } | |
| 363 | + } | |
| 364 | + } | |
| 365 | + | |
| 366 | + /** | |
| 330 | 367 | * Add/Remove edit link in dashboard. |
| 331 | 368 | * |
| 332 | 369 | * Add or remove an edit link to the post/page action links on the post/pages list table. |
| 333 | 370 | * |
| @@ -342,20 +379,14 @@ | ||
| 342 | 379 | public function filter_post_row_actions( $actions ) { |
| 343 | 380 | if ( $this->is_current_screen() ) { |
| 344 | 381 | // unset( $actions['view'] ); |
| 345 | 382 | unset( $actions['inline hide-if-no-js'] ); |
| 383 | + | |
| 384 | + if ( isset( $actions['view'] ) ) { | |
| 385 | + $actions['view'] = str_replace( __( 'View', 'shopbuilder' ), __( 'Preview', 'shopbuilder' ), $actions['view'] ); | |
| 386 | + } | |
| 346 | 387 | } |
| 347 | 388 | |
| 348 | 389 | return $actions; |
| 349 | 390 | } |
| 350 | 391 | |
| 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 | - } | |
| 361 | 392 | } |