0 ? $theme_id : 1; } // Register the script wp_register_script( 'codepen-embed-block-js', plugins_url('/build/index.js', dirname(__FILE__)), array('wp-blocks', 'wp-i18n', 'wp-element', 'wp-editor', 'wp-components', 'wp-block-editor'), filemtime(plugin_dir_path(__DIR__) . 'build/index.js'), true ); // WP Localized globals. Use dynamic PHP stuff in JavaScript via `cgbGlobal` object. wp_localize_script( 'codepen-embed-block-js', 'cgbGlobal', // Array containing dynamic data for a JS Global. [ 'pluginDirPath' => plugin_dir_path(__DIR__), 'pluginDirUrl' => plugin_dir_url(__DIR__), // if not set, make sure returns 1 'cpDefaultThemeId' => $theme_id, // Add more data here that you want to access from `cgbGlobal` object. ] ); // Register the block type register_block_type( 'cp/codepen-gutenberg-embed-block', array( 'editor_script' => 'codepen-embed-block-js', ) ); } add_action('init', 'codepen_block_assets'); function cp_embed_block_settings_init() { register_setting('cp_embed', 'cp_embed_options', array( 'sanitize_callback' => 'cp_embed_sanitize_options' )); add_settings_section( 'cp_embed_section_developers', __('Theme Settings', 'cp_embed'), 'cp_embed_section_developers_callback', 'cp_embed' ); add_settings_field( 'cp_embed_field_pill', __('Theme ID', 'cp_embed'), 'cp_embed_field_pill_cb', 'cp_embed', 'cp_embed_section_developers', array( 'label_for' => 'cp_embed_field_pill', 'wporg_custom_data' => 'custom', ) ); // IMPROVE: Add a "force override theme ID" setting } add_action('admin_init', 'cp_embed_block_settings_init'); /** * Sanitize the options before saving * * @param array $input The input array from the form * @return array The sanitized input array */ function cp_embed_sanitize_options($input) { $theme_id = 1; if (is_array($input) && isset($input['cp_embed_field_pill']) && is_scalar($input['cp_embed_field_pill'])) { $theme_id = absint($input['cp_embed_field_pill']); $theme_id = $theme_id > 0 ? $theme_id : 1; } return array( 'cp_embed_field_pill' => $theme_id, ); } /** * Developers section callback function. * * @param array $args The settings array, defining title, id, callback. */ function cp_embed_section_developers_callback($args) { } /** * Pill field callback function. * * @param array $args */ function cp_embed_field_pill_cb($args) { $options = get_option('cp_embed_options', array()); $field_value = 1; if (is_array($options) && isset($options[$args['label_for']]) && is_scalar($options[$args['label_for']])) { $field_value = absint($options[$args['label_for']]); $field_value = $field_value > 0 ? $field_value : 1; } ?>