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