| 1 |
<?php |
| 2 |
|
| 3 |
defined('ABSPATH') or die("Jog on!"); |
| 4 |
|
| 5 |
/** |
| 6 |
* Register the "Snippet Shortcode" block |
| 7 |
*/ |
| 8 |
function sh_cd_gutenberg_register_block() { |
| 9 |
|
| 10 |
wp_register_script( |
| 11 |
'sh-cd-gutenberg-block', |
| 12 |
plugins_url( '../assets/js/gutenberg-block.js', __FILE__ ), |
| 13 |
[ 'wp-blocks', 'wp-element', 'wp-block-editor', 'wp-components', 'wp-server-side-render', 'wp-i18n' ], |
| 14 |
SH_CD_PLUGIN_VERSION, |
| 15 |
true |
| 16 |
); |
| 17 |
|
| 18 |
register_block_type( 'sh-cd/shortcode', [ |
| 19 |
'title' => __( 'Snippet Shortcode', SH_CD_SLUG ), |
| 20 |
'description' => __( 'Insert one of your own shortcodes or a premade shortcode.', SH_CD_SLUG ), |
| 21 |
'category' => 'widgets', |
| 22 |
'icon' => 'shortcode', |
| 23 |
'attributes' => [ |
| 24 |
'slug' => [ |
| 25 |
'type' => 'string', |
| 26 |
'default' => '', |
| 27 |
], |
| 28 |
], |
| 29 |
'editor_script' => 'sh-cd-gutenberg-block', |
| 30 |
'render_callback' => 'sh_cd_gutenberg_render_block', |
| 31 |
] ); |
| 32 |
} |
| 33 |
add_action( 'init', 'sh_cd_gutenberg_register_block' ); |
| 34 |
|
| 35 |
/** |
| 36 |
* Render the block on the front-end (and for the editor's ServerSideRender preview) |
| 37 |
* |
| 38 |
* @param $attributes |
| 39 |
* |
| 40 |
* @return string |
| 41 |
*/ |
| 42 |
function sh_cd_gutenberg_render_block( $attributes ) { |
| 43 |
|
| 44 |
if ( true === empty( $attributes[ 'slug' ] ) ) { |
| 45 |
return ''; |
| 46 |
} |
| 47 |
|
| 48 |
return sh_cd_shortcode_render( [ 'slug' => sanitize_text_field( $attributes[ 'slug' ] ) ] ); |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Supply the block editor with the list of shortcodes to pick from |
| 53 |
*/ |
| 54 |
function sh_cd_gutenberg_enqueue_editor_assets() { |
| 55 |
|
| 56 |
$config = [ |
| 57 |
'yourShortcodes' => sh_cd_gutenberg_options_your_shortcodes(), |
| 58 |
'premadeShortcodes' => sh_cd_gutenberg_options_premade_shortcodes(), |
| 59 |
'manageUrl' => admin_url( 'admin.php?page=sh-cd-shortcode-variables-your-shortcodes' ), |
| 60 |
'i18n' => [ |
| 61 |
'placeholder-title' => __( 'Snippet Shortcode', SH_CD_SLUG ), |
| 62 |
'placeholder-text' => __( 'Select one of your own shortcodes, or a premade shortcode.', SH_CD_SLUG ), |
| 63 |
'your-shortcodes-label' => __( 'Your shortcodes', SH_CD_SLUG ), |
| 64 |
'premade-shortcodes-label' => __( 'Premade shortcodes', SH_CD_SLUG ), |
| 65 |
'no-shortcodes' => __( 'You haven\'t created any shortcodes yet.', SH_CD_SLUG ), |
| 66 |
'manage-shortcodes' => __( 'Manage your shortcodes', SH_CD_SLUG ), |
| 67 |
'change-shortcode' => __( 'Change shortcode', SH_CD_SLUG ), |
| 68 |
'select-option' => __( 'Select a shortcode…', SH_CD_SLUG ), |
| 69 |
'premium-label' => __( 'Premium', SH_CD_SLUG ), |
| 70 |
], |
| 71 |
]; |
| 72 |
|
| 73 |
wp_localize_script( 'sh-cd-gutenberg-block', 'shCdGutenberg', $config ); |
| 74 |
} |
| 75 |
add_action( 'enqueue_block_editor_assets', 'sh_cd_gutenberg_enqueue_editor_assets' ); |
| 76 |
|
| 77 |
/** |
| 78 |
* Build the "your shortcodes" option list for the block editor |
| 79 |
* |
| 80 |
* @return array |
| 81 |
*/ |
| 82 |
function sh_cd_gutenberg_options_your_shortcodes() { |
| 83 |
|
| 84 |
$shortcodes = sh_cd_db_shortcodes_all_enabled(); |
| 85 |
$slugs = wp_list_pluck( $shortcodes, 'slug' ); |
| 86 |
|
| 87 |
$options = []; |
| 88 |
|
| 89 |
foreach ( $slugs as $slug ) { |
| 90 |
$options[] = [ 'value' => $slug, 'label' => $slug, 'premium' => false ]; |
| 91 |
} |
| 92 |
|
| 93 |
return $options; |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* Build the "premade shortcodes" option list for the block editor |
| 98 |
* |
| 99 |
* @return array |
| 100 |
*/ |
| 101 |
function sh_cd_gutenberg_options_premade_shortcodes() { |
| 102 |
|
| 103 |
$presets = sh_cd_presets_both_lists(); |
| 104 |
|
| 105 |
$options = []; |
| 106 |
|
| 107 |
foreach ( $presets as $slug => $data ) { |
| 108 |
$options[] = [ 'value' => $slug, 'label' => $slug, 'premium' => false === empty( $data[ 'premium' ] ) ]; |
| 109 |
} |
| 110 |
|
| 111 |
return $options; |
| 112 |
} |
| 113 |
|