| 1 |
<?php |
| 2 |
|
| 3 |
if (!defined('ABSPATH')) { |
| 4 |
exit; |
| 5 |
} |
| 6 |
|
| 7 |
/** |
| 8 |
* Register the block using block.json (modern method) |
| 9 |
* Temporarily commented out to test legacy method |
| 10 |
*/ |
| 11 |
// function codepen_block_init() { |
| 12 |
// register_block_type( __DIR__ . '/block.json' ); |
| 13 |
// } |
| 14 |
// add_action( 'init', 'codepen_block_init' ); |
| 15 |
|
| 16 |
/** |
| 17 |
* Backward compatibility: Register the block the old way for existing blocks |
| 18 |
*/ |
| 19 |
function codepen_block_assets() { |
| 20 |
$options = get_option('cp_embed_options', array()); |
| 21 |
$theme_id = 1; |
| 22 |
|
| 23 |
if (is_array($options) && isset($options['cp_embed_field_pill']) && is_scalar($options['cp_embed_field_pill'])) { |
| 24 |
$theme_id = absint($options['cp_embed_field_pill']); |
| 25 |
$theme_id = $theme_id > 0 ? $theme_id : 1; |
| 26 |
} |
| 27 |
|
| 28 |
// Register the script |
| 29 |
wp_register_script( |
| 30 |
'codepen-embed-block-js', |
| 31 |
plugins_url('/build/index.js', dirname(__FILE__)), |
| 32 |
array('wp-blocks', 'wp-i18n', 'wp-element', 'wp-editor', 'wp-components', 'wp-block-editor'), |
| 33 |
filemtime(plugin_dir_path(__DIR__) . 'build/index.js'), |
| 34 |
true |
| 35 |
); |
| 36 |
|
| 37 |
// WP Localized globals. Use dynamic PHP stuff in JavaScript via `cgbGlobal` object. |
| 38 |
wp_localize_script( |
| 39 |
'codepen-embed-block-js', |
| 40 |
'cgbGlobal', // Array containing dynamic data for a JS Global. |
| 41 |
[ |
| 42 |
'pluginDirPath' => plugin_dir_path(__DIR__), |
| 43 |
'pluginDirUrl' => plugin_dir_url(__DIR__), |
| 44 |
// if not set, make sure returns 1 |
| 45 |
'cpDefaultThemeId' => $theme_id, |
| 46 |
// Add more data here that you want to access from `cgbGlobal` object. |
| 47 |
] |
| 48 |
); |
| 49 |
|
| 50 |
// Register the block type |
| 51 |
register_block_type( |
| 52 |
'cp/codepen-gutenberg-embed-block', |
| 53 |
array( |
| 54 |
'editor_script' => 'codepen-embed-block-js', |
| 55 |
) |
| 56 |
); |
| 57 |
} |
| 58 |
|
| 59 |
add_action('init', 'codepen_block_assets'); |
| 60 |
|
| 61 |
function cp_embed_block_settings_init() { |
| 62 |
register_setting('cp_embed', 'cp_embed_options', array( |
| 63 |
'sanitize_callback' => 'cp_embed_sanitize_options' |
| 64 |
)); |
| 65 |
|
| 66 |
add_settings_section( |
| 67 |
'cp_embed_section_developers', |
| 68 |
__('Theme Settings', 'cp_embed'), 'cp_embed_section_developers_callback', |
| 69 |
'cp_embed' |
| 70 |
); |
| 71 |
|
| 72 |
add_settings_field( |
| 73 |
'cp_embed_field_pill', |
| 74 |
__('Theme ID', 'cp_embed'), |
| 75 |
'cp_embed_field_pill_cb', |
| 76 |
'cp_embed', |
| 77 |
'cp_embed_section_developers', |
| 78 |
array( |
| 79 |
'label_for' => 'cp_embed_field_pill', |
| 80 |
'wporg_custom_data' => 'custom', |
| 81 |
) |
| 82 |
); |
| 83 |
|
| 84 |
// IMPROVE: Add a "force override theme ID" setting |
| 85 |
} |
| 86 |
|
| 87 |
add_action('admin_init', 'cp_embed_block_settings_init'); |
| 88 |
|
| 89 |
/** |
| 90 |
* Sanitize the options before saving |
| 91 |
* |
| 92 |
* @param array $input The input array from the form |
| 93 |
* @return array The sanitized input array |
| 94 |
*/ |
| 95 |
function cp_embed_sanitize_options($input) { |
| 96 |
$theme_id = 1; |
| 97 |
|
| 98 |
if (is_array($input) && isset($input['cp_embed_field_pill']) && is_scalar($input['cp_embed_field_pill'])) { |
| 99 |
$theme_id = absint($input['cp_embed_field_pill']); |
| 100 |
$theme_id = $theme_id > 0 ? $theme_id : 1; |
| 101 |
} |
| 102 |
|
| 103 |
return array( |
| 104 |
'cp_embed_field_pill' => $theme_id, |
| 105 |
); |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
/** |
| 110 |
* Developers section callback function. |
| 111 |
* |
| 112 |
* @param array $args The settings array, defining title, id, callback. |
| 113 |
*/ |
| 114 |
function cp_embed_section_developers_callback($args) { |
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
* Pill field callback function. |
| 119 |
* |
| 120 |
* @param array $args |
| 121 |
*/ |
| 122 |
function cp_embed_field_pill_cb($args) { |
| 123 |
$options = get_option('cp_embed_options', array()); |
| 124 |
$field_value = 1; |
| 125 |
|
| 126 |
if (is_array($options) && isset($options[$args['label_for']]) && is_scalar($options[$args['label_for']])) { |
| 127 |
$field_value = absint($options[$args['label_for']]); |
| 128 |
$field_value = $field_value > 0 ? $field_value : 1; |
| 129 |
} |
| 130 |
?> |
| 131 |
|
| 132 |
<input |
| 133 |
id="<?php echo esc_attr( $args['label_for'] ); ?>" |
| 134 |
type="number" |
| 135 |
name="cp_embed_options[<?php echo esc_attr( $args['label_for'] ); ?>]" |
| 136 |
value="<?php echo esc_attr( $field_value ); ?>" |
| 137 |
min="1" |
| 138 |
step="1" |
| 139 |
> |
| 140 |
|
| 141 |
<p class="description"> |
| 142 |
<?php esc_html_e('Integer', 'cp_embed'); ?> |
| 143 |
</p> |
| 144 |
<?php |
| 145 |
} |
| 146 |
|
| 147 |
/** |
| 148 |
* Add the top level menu page. |
| 149 |
*/ |
| 150 |
function wporg_options_page() { |
| 151 |
add_options_page( |
| 152 |
'CodePen Block Settings', |
| 153 |
'CodePen Embed', |
| 154 |
'manage_options', |
| 155 |
'cp_embed', |
| 156 |
'cp_embed_options_page_html' |
| 157 |
); |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
/** |
| 162 |
* Register our wporg_options_page to the admin_menu action hook. |
| 163 |
*/ |
| 164 |
add_action('admin_menu', 'wporg_options_page'); |
| 165 |
|
| 166 |
|
| 167 |
/** |
| 168 |
* Top level menu callback function |
| 169 |
*/ |
| 170 |
function cp_embed_options_page_html() { |
| 171 |
if (!current_user_can('manage_options')) { |
| 172 |
return; |
| 173 |
} |
| 174 |
|
| 175 |
if (isset($_GET['settings-updated'])) { |
| 176 |
add_settings_error('cp_embed_messages', 'cp_embed_message', __('Settings Saved', 'cp_embed'), 'updated'); |
| 177 |
} |
| 178 |
|
| 179 |
settings_errors('cp_embed_messages'); |
| 180 |
?> |
| 181 |
<div class="wrap"> |
| 182 |
<h1><?php echo esc_html(get_admin_page_title()); ?></h1> |
| 183 |
<form action="options.php" method="post"> |
| 184 |
<?php |
| 185 |
settings_fields('cp_embed'); |
| 186 |
do_settings_sections('cp_embed'); |
| 187 |
submit_button('Save Settings'); |
| 188 |
?> |
| 189 |
</form> |
| 190 |
</div> |
| 191 |
<?php |
| 192 |
} |
| 193 |
|