init(); } return self::$instance; } /** * Method init * * @return void */ public function init() { if ( $this->sp_eap_is_shortcode_block_visible() ) { $this->sp_eab_shortcode_block_register(); // add block assets. add_action( 'enqueue_block_assets', array( $this, 'eab_shortcode_block_enqueue_file' ) ); } } /** * Method eab_shortcode_block_enqueue_file. * * @return void */ public function eab_shortcode_block_enqueue_file() { wp_register_script( 'sp_eab_admin_index_js', plugin_dir_url( __FILE__ ) . '../build/ea-blocks.js', array(), SP_EA_VERSION, true ); wp_localize_script( 'sp_eab_admin_index_js', 'sp_easy_accordion_pro', array( 'shortCodeList' => $this->sp_easy_accordion_pro_post_list(), ) ); } /** * Shortcode list. * * @return array */ public function sp_easy_accordion_pro_post_list() { $shortcodes = get_posts( array( 'post_type' => 'sp_easy_accordion', 'post_status' => 'publish', 'posts_per_page' => 999, ) ); if ( count( $shortcodes ) < 1 ) { return array(); } return array_map( function ( $shortcode ) { return (object) array( 'id' => absint( $shortcode->ID ), 'title' => esc_html( $shortcode->post_title ), ); }, $shortcodes ); } /** * Register Gutenberg shortcode block. */ public function sp_eab_shortcode_block_register() { $attributes = array( 'shortcode' => array( 'type' => 'string', 'default' => '', ), 'showInputShortcode' => array( 'type' => 'boolean', 'default' => true, ), 'preview' => array( 'type' => 'boolean', 'default' => false, ), 'is_admin' => array( 'type' => 'boolean', 'default' => is_admin(), ), 'align' => array( 'type' => 'string', 'default' => 'none', ), ); register_block_type( 'sp-easy-accordion-pro/shortcode', array( 'attributes' => $attributes, 'editor_script' => array( 'sp_eab_admin_index_js' ), 'render_callback' => array( $this, 'sp_eap_render_shortcode' ), ) ); } /** * Render callback. * * @param string $attributes Shortcode. * @return string */ public function sp_eap_render_shortcode( $attributes ) { // class name. $align = sanitize_key( $attributes['align'] ?? 'none' ); $class_name = 'align' . $align; return '