| 1 |
<?php |
| 2 |
/** |
| 3 |
* Fired during plugin init |
| 4 |
* |
| 5 |
* @link https://wpsmartpost.com/ |
| 6 |
* @since 2.0.0 |
| 7 |
* |
| 8 |
* @package Smart_Post_Show_Pro |
| 9 |
* @subpackage Smart_Post_Show_Pro/includes |
| 10 |
*/ |
| 11 |
|
| 12 |
if ( ! defined( 'ABSPATH' ) ) { |
| 13 |
exit; // Exit if accessed directly. |
| 14 |
} |
| 15 |
|
| 16 |
use SmartPostShow\Blocks\Helper; |
| 17 |
use SmartPostShow\Blocks\Assets_Manager; |
| 18 |
/** |
| 19 |
* Custom post class to register the carousel. |
| 20 |
*/ |
| 21 |
class Smart_Post_Show_Saved_Template { |
| 22 |
|
| 23 |
/** |
| 24 |
* Smart Post Show Pro single instance of the class |
| 25 |
* |
| 26 |
* @var null The instance of the class. |
| 27 |
* @since 2.0 |
| 28 |
* |
| 29 |
* @return void |
| 30 |
*/ |
| 31 |
protected static $instance = null; |
| 32 |
|
| 33 |
/** |
| 34 |
* Main Smart_Post_Show_Pro_blocks Instance |
| 35 |
* |
| 36 |
* @since 2.0 |
| 37 |
* @static |
| 38 |
* |
| 39 |
* @return self help instance |
| 40 |
*/ |
| 41 |
public static function instance() { |
| 42 |
if ( is_null( self::$instance ) ) { |
| 43 |
self::$instance = new self(); |
| 44 |
} |
| 45 |
|
| 46 |
return self::$instance; |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Undocumented function |
| 51 |
*/ |
| 52 |
public function register_template_shortcode() { |
| 53 |
$this->smart_post_saved_template_post_type(); |
| 54 |
add_filter( 'manage_sp_post_template_posts_columns', array( $this, 'filter_saved_template_column' ) ); |
| 55 |
add_action( 'manage_sp_post_template_posts_custom_column', array( $this, 'templates_table_content' ), 10, 2 ); |
| 56 |
add_shortcode( 'smart_post', array( $this, 'smart_template_callback' ) ); |
| 57 |
add_filter( 'use_block_editor_for_post_type', array( $this, 'smart_post_force_block_editor_for_save_template' ), 100, 2 ); |
| 58 |
add_action( 'admin_init', array( $this, 'redirect_saved_template_edit_page' ) ); |
| 59 |
add_action( 'admin_init', array( $this, 'redirect_based_on_editor_preference' ) ); |
| 60 |
add_action( 'rest_api_init', array( $this, 'smart_saved_template_rest' ) ); |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Add saved template admin columns. |
| 65 |
* |
| 66 |
* @since 2.0.0 |
| 67 |
* @return statement |
| 68 |
*/ |
| 69 |
public function filter_saved_template_column() { |
| 70 |
$saved_columns = array(); |
| 71 |
$saved_columns['cb'] = '<input type="checkbox" />'; |
| 72 |
$saved_columns['title'] = __( 'Title', 'post-carousel' ); |
| 73 |
$saved_columns['shortcode'] = __( 'Shortcode', 'post-carousel' ); |
| 74 |
$saved_columns['date'] = __( 'Date', 'post-carousel' ); |
| 75 |
// $saved_columns['sps_action'] = __( 'Layout', 'post-carousel' ); |
| 76 |
|
| 77 |
return $saved_columns; |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Saved Template Column content |
| 82 |
* |
| 83 |
* @param string $column_name table column name. |
| 84 |
* @param init $post_id post id. |
| 85 |
* @return void |
| 86 |
*/ |
| 87 |
public function templates_table_content( $column_name, $post_id ) { |
| 88 |
echo '<code class="sp-smart-post-shortcode-copy">[smart_post id="' . esc_attr( $post_id ) . '"]</code>'; |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* Smart Post Saved Template post type |
| 93 |
*/ |
| 94 |
public function smart_post_saved_template_post_type() { |
| 95 |
if ( post_type_exists( 'sp_post_template' ) ) { |
| 96 |
return; |
| 97 |
} |
| 98 |
$capability = apply_filters( 'pcp_user_role_permission', 'manage_options' ); |
| 99 |
$show_ui = current_user_can( $capability ) ? true : false; |
| 100 |
// Set the Smart Post Saved Template post type labels. |
| 101 |
$labels = |
| 102 |
array( |
| 103 |
'name' => __( 'Saved Templates', 'post-carousel' ), |
| 104 |
'singular_name' => __( 'Saved Template', 'post-carousel' ), |
| 105 |
'menu_name' => __( 'Saved Templates', 'post-carousel' ), |
| 106 |
'all_items' => __( 'Saved Templates', 'post-carousel' ), |
| 107 |
'add_new' => __( 'Add New Template', 'post-carousel' ), |
| 108 |
'add_new_item' => __( 'Add New Template', 'post-carousel' ), |
| 109 |
'edit' => __( 'Edit', 'post-carousel' ), |
| 110 |
'edit_item' => __( 'Edit Template', 'post-carousel' ), |
| 111 |
'view_item' => __( 'View Template', 'post-carousel' ), |
| 112 |
'new_item' => __( 'New Templates', 'post-carousel' ), |
| 113 |
'search_items' => __( 'Search Template', 'post-carousel' ), |
| 114 |
'not_found' => __( 'No Template found', 'post-carousel' ), |
| 115 |
'not_found_in_trash' => __( 'No Template found in Trash', 'post-carousel' ), |
| 116 |
'item_published' => __( 'Template Published', 'post-carousel' ), |
| 117 |
'item_published_privately' => __( 'Template published privately.', 'post-carousel' ), |
| 118 |
'item_reverted_to_draft' => __( 'Template reverted to draft.', 'post-carousel' ), |
| 119 |
'item_scheduled' => __( 'Template scheduled.', 'post-carousel' ), |
| 120 |
'item_updated' => __( 'Template updated.', 'post-carousel' ), |
| 121 |
); |
| 122 |
// Set the Smart Post Saved Template post type arguments. |
| 123 |
$args = |
| 124 |
array( |
| 125 |
'labels' => $labels, |
| 126 |
'public' => false, |
| 127 |
'supports' => array( 'title', 'editor', 'revisions' ), |
| 128 |
'show_in_rest' => true, |
| 129 |
'hierarchical' => false, |
| 130 |
'rewrite' => false, |
| 131 |
'show_ui' => $show_ui, |
| 132 |
'show_in_menu' => false, |
| 133 |
'show_in_nav_menu' => false, |
| 134 |
'exclude_from_search' => true, |
| 135 |
'capability_type' => 'page', |
| 136 |
); |
| 137 |
|
| 138 |
register_post_type( 'sp_post_template', $args ); |
| 139 |
} |
| 140 |
|
| 141 |
/** |
| 142 |
* Undocumented function |
| 143 |
* |
| 144 |
* @param array $attributes shortcode attributes array. |
| 145 |
* @return string |
| 146 |
*/ |
| 147 |
public function smart_template_callback( $attributes ) { |
| 148 |
$saved_template_modules = Helper::get_modules_show_hide(); |
| 149 |
$saved_template_enabled = $saved_template_modules['saved-template']; |
| 150 |
|
| 151 |
$attributes = shortcode_atts( |
| 152 |
array( |
| 153 |
'id' => '', |
| 154 |
), |
| 155 |
$attributes |
| 156 |
); |
| 157 |
|
| 158 |
$id = $attributes['id']; |
| 159 |
$id = is_numeric( $id ) ? (float) $id : false; |
| 160 |
|
| 161 |
$content = ''; |
| 162 |
if ( $id ) { |
| 163 |
if ( ! $saved_template_enabled ) { |
| 164 |
return '<p>[smart_post id="' . $id . '"]</p>'; |
| 165 |
} |
| 166 |
|
| 167 |
$content_post = get_post( $id ); |
| 168 |
$post_status = isset( $content_post ) ? $content_post->post_status : ''; |
| 169 |
|
| 170 |
if ( 'publish' === $post_status ) { |
| 171 |
$style_url = $this->saved_template_style( $id ); |
| 172 |
$this->saved_template_static_style( $id ); |
| 173 |
$content .= $content_post ? $content_post->post_content : null; |
| 174 |
$content = do_blocks( $content ); |
| 175 |
$content = do_shortcode( $content ); |
| 176 |
$content = str_replace( ']]>', ']]>', $content ); |
| 177 |
$content = preg_replace( '%<p> \s*</p>%', '', $content ); |
| 178 |
$content = preg_replace( '/^(?:<br\s*\/?>\s*)+/', '', $content ); |
| 179 |
$content = preg_replace( '#<p>\s*</p>#', '', $content ); |
| 180 |
$content = preg_replace( '#<p>\s*<a[^>]*>\s*</a>\s*</p>#', '', $content ); |
| 181 |
return $content; |
| 182 |
} |
| 183 |
} |
| 184 |
return ''; |
| 185 |
} |
| 186 |
|
| 187 |
/** |
| 188 |
* Undocumented function |
| 189 |
* |
| 190 |
* @param [type] $use_block_editor |
| 191 |
* @param [type] $post_type |
| 192 |
* @return boolean $use_block_editor block editor true/false. |
| 193 |
*/ |
| 194 |
public function smart_post_force_block_editor_for_save_template( $use_block_editor, $post_type ) { |
| 195 |
if ( 'sp_post_template' === $post_type ) { |
| 196 |
return true; |
| 197 |
} |
| 198 |
return $use_block_editor; |
| 199 |
} |
| 200 |
|
| 201 |
/** |
| 202 |
* Undocumented function |
| 203 |
* |
| 204 |
* @return void |
| 205 |
*/ |
| 206 |
public function redirect_saved_template_edit_page() { |
| 207 |
|
| 208 |
// If we are on post.php and editing a post. |
| 209 |
if ( isset( $_GET['post'] ) && isset( $_GET['action'] ) && 'edit' === $_GET['action'] ) { |
| 210 |
|
| 211 |
$post_id = intval( $_GET['post'] ); |
| 212 |
$post_type = get_post_type( $post_id ); |
| 213 |
|
| 214 |
// If this is YOUR custom post type. |
| 215 |
if ( 'sp_post_template' === $post_type ) { |
| 216 |
return; |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
// When creating a new post (post-new.php). |
| 221 |
if ( isset( $_SERVER['REQUEST_URI'] ) && strpos( wp_unslash( $_SERVER['REQUEST_URI'] ), 'post-new.php' ) !== false ) { |
| 222 |
return; // DO NOT redirect. |
| 223 |
} |
| 224 |
|
| 225 |
// Redirect default list table edit.php?post_type=my_cpt. |
| 226 |
if ( isset( $_GET['post_type'] ) && 'sp_post_template' === $_GET['post_type'] ) { |
| 227 |
|
| 228 |
// avoid redirect loop from your own React page. |
| 229 |
if ( ! isset( $_GET['page'] ) || 'pcp_blocks#savedTemplate' !== $_GET['page'] ) { |
| 230 |
wp_safe_redirect( admin_url( 'edit.php?post_type=sp_post_carousel&page=pcp_help#savedTemplate' ) ); |
| 231 |
exit; |
| 232 |
} |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
/** |
| 237 |
* Redirect "Add New Carousel" to the block-editor Saved Template flow |
| 238 |
* when the user previously picked Block Editor in the welcome modal |
| 239 |
* and ticked "Remember this choice". |
| 240 |
* |
| 241 |
* @return void |
| 242 |
*/ |
| 243 |
public function redirect_based_on_editor_preference() { |
| 244 |
global $pagenow; |
| 245 |
|
| 246 |
if ( 'post-new.php' !== $pagenow ) { |
| 247 |
return; |
| 248 |
} |
| 249 |
|
| 250 |
$post_type = isset( $_GET['post_type'] ) ? sanitize_key( wp_unslash( $_GET['post_type'] ) ) : ''; |
| 251 |
if ( 'sp_post_carousel' !== $post_type ) { |
| 252 |
return; |
| 253 |
} |
| 254 |
|
| 255 |
if ( 'block_editor' !== get_option( 'spsp_blocks_promo_modal_choice' ) ) { |
| 256 |
return; |
| 257 |
} |
| 258 |
|
| 259 |
wp_safe_redirect( admin_url( 'post-new.php?post_type=sp_post_template&spblock_inserter' ) ); |
| 260 |
exit; |
| 261 |
} |
| 262 |
|
| 263 |
/** |
| 264 |
* Undocumented function |
| 265 |
*/ |
| 266 |
public function smart_saved_template_rest() { |
| 267 |
register_rest_route( |
| 268 |
'sp-smart-post/v2', |
| 269 |
'/saved-templates-duplicate/(?P<id>\d+)', |
| 270 |
array( |
| 271 |
'methods' => 'POST', |
| 272 |
'callback' => array( $this, 'duplicate_smart_saved_templates' ), |
| 273 |
'permission_callback' => function () { |
| 274 |
return current_user_can( 'edit_posts' ); |
| 275 |
}, |
| 276 |
) |
| 277 |
); |
| 278 |
} |
| 279 |
/** |
| 280 |
* Undocumented function |
| 281 |
* |
| 282 |
* @param WP_REST_Request $request request. |
| 283 |
* @return WP_REST_Response |
| 284 |
*/ |
| 285 |
public function duplicate_smart_saved_templates( WP_REST_Request $request ) { |
| 286 |
$post_id = (int) $request->get_param( 'id' ); |
| 287 |
|
| 288 |
$post = get_post( $post_id ); |
| 289 |
if ( ! $post ) { |
| 290 |
return new WP_Error( 'not_found', 'Post not found', array( 'status' => 404 ) ); |
| 291 |
} |
| 292 |
|
| 293 |
$args = array( |
| 294 |
'post_title' => $post->post_title . ' (Copy)', |
| 295 |
'post_content' => $post->post_content, |
| 296 |
'post_type' => $post->post_type, |
| 297 |
'post_status' => 'draft', |
| 298 |
); |
| 299 |
$new_post_id = wp_insert_post( $args ); |
| 300 |
if ( is_wp_error( $new_post_id ) ) { |
| 301 |
return new WP_Error( 'insert_failed', 'Failed to insert post', array( 'status' => 500 ) ); |
| 302 |
} |
| 303 |
|
| 304 |
// Copy post meta. |
| 305 |
$meta = get_post_meta( $post_id ); |
| 306 |
foreach ( $meta as $key => $values ) { |
| 307 |
foreach ( $values as $value ) { |
| 308 |
add_post_meta( $new_post_id, $key, maybe_unserialize( $value ) ); |
| 309 |
} |
| 310 |
} |
| 311 |
|
| 312 |
return rest_ensure_response( |
| 313 |
array( |
| 314 |
'new_post_id' => $new_post_id, |
| 315 |
'shortcode' => sprintf( '[smart_post id="%d"]', $new_post_id ), |
| 316 |
) |
| 317 |
); |
| 318 |
} |
| 319 |
|
| 320 |
public function saved_template_style( $post_id ) { |
| 321 |
if ( ! $post_id ) { |
| 322 |
return ''; |
| 323 |
} |
| 324 |
|
| 325 |
$upload_dir = wp_upload_dir(); |
| 326 |
|
| 327 |
$css_file_path = trailingslashit( $upload_dir['basedir'] ) . |
| 328 |
"smart-post-show/sp-post-{$post_id}.css"; |
| 329 |
|
| 330 |
$css_file_url = trailingslashit( |
| 331 |
set_url_scheme( $upload_dir['baseurl'] ) |
| 332 |
) . "smart-post-show/sp-post-{$post_id}.css"; |
| 333 |
|
| 334 |
if ( ! file_exists( $css_file_path ) ) { |
| 335 |
$css_from_meta = get_post_meta( $post_id, '_sp_smart_css', true ); |
| 336 |
|
| 337 |
wp_register_style( "sp-smart-post-$post_id-css", false, array(), SMART_POST_SHOW_VERSION ); |
| 338 |
wp_add_inline_style( "sp-smart-post-$post_id-css", $css_from_meta ); |
| 339 |
wp_enqueue_style( "sp-smart-post-$post_id-css" ); |
| 340 |
} |
| 341 |
|
| 342 |
wp_register_style( "sp-smart-post-$post_id-css", $css_file_url, array(), SMART_POST_SHOW_VERSION ); |
| 343 |
wp_enqueue_style( "sp-smart-post-$post_id-css" ); |
| 344 |
} |
| 345 |
|
| 346 |
public function saved_template_static_style( $post_id ) { |
| 347 |
$our_blocks_slug = Helper::get_block_slugs(); |
| 348 |
$our_blocks_list = array(); |
| 349 |
$our_blocks_list = array_map( |
| 350 |
function ( $block ) { |
| 351 |
return "sp-smart-post-show/{$block}"; |
| 352 |
}, |
| 353 |
$our_blocks_slug |
| 354 |
); |
| 355 |
$active_block_name = Assets_Manager::get_active_blocks( $post_id, $our_blocks_list ); |
| 356 |
// $content = get_post_field( 'post_content', $post_id ); |
| 357 |
|
| 358 |
$active_block_array = self::block_name_ids_array( $active_block_name, $post_id ); |
| 359 |
$assets_manager = new Assets_Manager( $our_blocks_slug ); |
| 360 |
$assets_manager->enqueue_combined_block_css( $active_block_array, $post_id ); |
| 361 |
} |
| 362 |
|
| 363 |
public static function block_name_ids_array( $active_block_name, $post_id ) { |
| 364 |
$prefix = 'sp-smart-post-show/'; |
| 365 |
$result = array(); |
| 366 |
|
| 367 |
foreach ( $active_block_name as $block ) { |
| 368 |
$result[] = str_replace( $prefix, '', $block ); |
| 369 |
} |
| 370 |
|
| 371 |
return array( |
| 372 |
$post_id => $result, |
| 373 |
); |
| 374 |
} |
| 375 |
} |
| 376 |
|