# codepen-embed-block/trunk/src/init.php

CodePen Embed Block, version trunk. 193 lines.

- Page: https://pluginprobe.com/plugins/codepen-embed-block/trunk/code/src/init.php
- Raw: https://pluginprobe.com/plugins/codepen-embed-block/trunk/raw/src/init.php
- Modified: 2026-08-21T23:50:58+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/codepen-embed-block/trunk/code/src/init.php#L10-L20`.

```php
<?php

if (!defined('ABSPATH')) {
	exit;
}

/**
 * Register the block using block.json (modern method)
 * Temporarily commented out to test legacy method
 */
// function codepen_block_init() {
// 	register_block_type( __DIR__ . '/block.json' );
// }
// add_action( 'init', 'codepen_block_init' );

/**
 * Backward compatibility: Register the block the old way for existing blocks
 */
function codepen_block_assets() { 
	$options  = get_option('cp_embed_options', array());
	$theme_id = 1;

	if (is_array($options) && isset($options['cp_embed_field_pill']) && is_scalar($options['cp_embed_field_pill'])) {
		$theme_id = absint($options['cp_embed_field_pill']);
		$theme_id = $theme_id > 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;
	}
	?>

	<input
		id="<?php echo esc_attr( $args['label_for'] ); ?>"
		type="number"
		name="cp_embed_options[<?php echo esc_attr( $args['label_for'] ); ?>]"
		value="<?php echo esc_attr( $field_value ); ?>"
		min="1"
		step="1"
	>

	<p class="description">
		<?php esc_html_e('Integer', 'cp_embed'); ?>
	</p>
	<?php
}

/**
 * Add the top level menu page.
 */
function wporg_options_page() {
	add_options_page(
		'CodePen Block Settings',
		'CodePen Embed',
		'manage_options',
		'cp_embed',
		'cp_embed_options_page_html'
	);
}


/**
 * Register our wporg_options_page to the admin_menu action hook.
 */
add_action('admin_menu', 'wporg_options_page');


/**
 * Top level menu callback function
 */
function cp_embed_options_page_html() {
	if (!current_user_can('manage_options')) {
		return;
	}

	if (isset($_GET['settings-updated'])) {
		add_settings_error('cp_embed_messages', 'cp_embed_message', __('Settings Saved', 'cp_embed'), 'updated');
	}

	settings_errors('cp_embed_messages');
	?>
	<div class="wrap">
		<h1><?php echo esc_html(get_admin_page_title()); ?></h1>
		<form action="options.php" method="post">
			<?php
				settings_fields('cp_embed');
				do_settings_sections('cp_embed');
				submit_button('Save Settings');
			?>
		</form>
	</div>
	<?php
}

```
