| 1 |
<?php |
| 2 |
/** |
| 3 |
* The plugin gutenberg block Initializer. |
| 4 |
* |
| 5 |
* @link https://shapedplugin.com/ |
| 6 |
* @since 2.4.2 |
| 7 |
* |
| 8 |
* @package Smart_Post_Show |
| 9 |
* @subpackage Smart_Post_Show/Admin |
| 10 |
* @author ShapedPlugin <[email protected]> |
| 11 |
*/ |
| 12 |
|
| 13 |
// Exit if accessed directly. |
| 14 |
if ( ! defined( 'ABSPATH' ) ) { |
| 15 |
exit; |
| 16 |
} |
| 17 |
|
| 18 |
if ( ! class_exists( 'Smart_Post_Show_Gutenberg_Block_Init' ) ) { |
| 19 |
/** |
| 20 |
* Smart_Post_Show_Gutenberg_Block_Init class. |
| 21 |
*/ |
| 22 |
class Smart_Post_Show_Gutenberg_Block_Init { |
| 23 |
/** |
| 24 |
* Script and style suffix |
| 25 |
* |
| 26 |
* @since 2.4.2 |
| 27 |
* @access protected |
| 28 |
* @var string |
| 29 |
*/ |
| 30 |
protected $suffix; |
| 31 |
/** |
| 32 |
* Custom Gutenberg Block Initializer. |
| 33 |
*/ |
| 34 |
public function __construct() { |
| 35 |
$this->suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG || defined( 'WP_DEBUG' ) && WP_DEBUG ? '' : '.min'; |
| 36 |
add_action( 'init', array( $this, 'spsp_gutenberg_shortcode_block' ) ); |
| 37 |
add_action( 'enqueue_block_editor_assets', array( $this, 'spsp_block_editor_assets' ) ); |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Register block editor script for backend. |
| 42 |
*/ |
| 43 |
public function spsp_block_editor_assets() { |
| 44 |
wp_enqueue_script( |
| 45 |
'smart-post-show-shortcode-block', |
| 46 |
plugins_url( '/GutenbergBlock/build/index.js', dirname( __FILE__ ) ), |
| 47 |
array( 'jquery' ), |
| 48 |
SP_PC_VERSION, |
| 49 |
true |
| 50 |
); |
| 51 |
|
| 52 |
/** |
| 53 |
* Register block editor css file enqueue for backend. |
| 54 |
*/ |
| 55 |
wp_enqueue_style( 'font-awesome' ); |
| 56 |
wp_enqueue_style( 'pcp_swiper' ); |
| 57 |
wp_enqueue_style( 'pcp-style' ); |
| 58 |
} |
| 59 |
/** |
| 60 |
* Shortcode list. |
| 61 |
* |
| 62 |
* @return array |
| 63 |
*/ |
| 64 |
public function spsp_post_list() { |
| 65 |
$shortcodes = get_posts( |
| 66 |
array( |
| 67 |
'post_type' => 'sp_post_carousel', |
| 68 |
'post_status' => 'publish', |
| 69 |
'posts_per_page' => 9999, |
| 70 |
) |
| 71 |
); |
| 72 |
|
| 73 |
if ( count( $shortcodes ) < 1 ) { |
| 74 |
return array(); |
| 75 |
} |
| 76 |
|
| 77 |
return array_map( |
| 78 |
function ( $shortcode ) { |
| 79 |
return (object) array( |
| 80 |
'id' => absint( $shortcode->ID ), |
| 81 |
'title' => esc_html( $shortcode->post_title ), |
| 82 |
); |
| 83 |
}, |
| 84 |
$shortcodes |
| 85 |
); |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Register Gutenberg shortcode block. |
| 90 |
*/ |
| 91 |
public function spsp_gutenberg_shortcode_block() { |
| 92 |
/** |
| 93 |
* Register block editor js file enqueue for backend. |
| 94 |
*/ |
| 95 |
wp_register_script( 'pcp_script', SP_PC_URL . 'public/assets/js/scripts' . $this->suffix . '.js', array( 'jquery' ), SP_PC_VERSION, true ); |
| 96 |
if ( is_admin() ) { |
| 97 |
wp_localize_script( |
| 98 |
'pcp_script', |
| 99 |
'smartPostShowGbScript', |
| 100 |
array( |
| 101 |
'url' => SP_PC_URL, |
| 102 |
'loadScript' => SP_PC_URL . 'public/assets/js/scripts.js', |
| 103 |
'link' => admin_url( 'post-new.php?post_type=sp_post_carousel' ), |
| 104 |
'shortCodeList' => $this->spsp_post_list(), |
| 105 |
) |
| 106 |
); |
| 107 |
} |
| 108 |
/** |
| 109 |
* Register Gutenberg block on server-side. |
| 110 |
*/ |
| 111 |
register_block_type( |
| 112 |
'smart-post-show-pro/shortcode', |
| 113 |
array( |
| 114 |
'attributes' => array( |
| 115 |
'shortcodelist' => array( |
| 116 |
'type' => 'object', |
| 117 |
'default' => '', |
| 118 |
), |
| 119 |
'shortcode' => array( |
| 120 |
'type' => 'string', |
| 121 |
'default' => '', |
| 122 |
), |
| 123 |
'showInputShortcode' => array( |
| 124 |
'type' => 'boolean', |
| 125 |
'default' => true, |
| 126 |
), |
| 127 |
'preview' => array( |
| 128 |
'type' => 'boolean', |
| 129 |
'default' => false, |
| 130 |
), |
| 131 |
'is_admin' => array( |
| 132 |
'type' => 'boolean', |
| 133 |
'default' => is_admin(), |
| 134 |
), |
| 135 |
), |
| 136 |
'example' => array( |
| 137 |
'attributes' => array( |
| 138 |
'preview' => true, |
| 139 |
), |
| 140 |
), |
| 141 |
// Enqueue blocks.editor.build.js in the editor only. |
| 142 |
'editor_script' => array( |
| 143 |
'pcp_swiper', |
| 144 |
'pcp_script', |
| 145 |
), |
| 146 |
// Enqueue blocks.editor.build.css in the editor only. |
| 147 |
'editor_style' => array(), |
| 148 |
'render_callback' => array( $this, 'smart_post_show_render_shortcode' ), |
| 149 |
) |
| 150 |
); |
| 151 |
} |
| 152 |
|
| 153 |
/** |
| 154 |
* Render callback. |
| 155 |
* |
| 156 |
* @param string $attributes Shortcode. |
| 157 |
* @return string |
| 158 |
*/ |
| 159 |
public function smart_post_show_render_shortcode( $attributes ) { |
| 160 |
$class_name = ''; |
| 161 |
if ( ! empty( $attributes['className'] ) ) { |
| 162 |
$class_name = 'class="' . esc_attr( $attributes['className'] ) . '"'; |
| 163 |
} |
| 164 |
|
| 165 |
if ( ! $attributes['is_admin'] ) { |
| 166 |
return '<div ' . $class_name . '>' . do_shortcode( '[smart_post_show id="' . sanitize_text_field( $attributes['shortcode'] ) . '"]' ) . '</div>'; |
| 167 |
} |
| 168 |
|
| 169 |
$edit_page_link = get_edit_post_link( sanitize_text_field( $attributes['shortcode'] ) ); |
| 170 |
|
| 171 |
return '<div id="' . uniqid() . '" ' . $class_name . ' ><a href="' . $edit_page_link . '" target="_blank" class="sp_sps_block_edit_button">Edit View </a>' . do_shortcode( '[smart_post_show id="' . sanitize_text_field( $attributes['shortcode'] ) . '"]' ) . '</div>'; |
| 172 |
} |
| 173 |
} |
| 174 |
} |
| 175 |
|