| 1 |
<?php |
| 2 |
|
| 3 |
use ISC\Plugin; |
| 4 |
use ISC\Helpers; |
| 5 |
|
| 6 |
/** |
| 7 |
* Integration with the Block editor |
| 8 |
*/ |
| 9 |
class ISC_Block_Options { |
| 10 |
/** |
| 11 |
* Construct an instance of ISC_Block_Options |
| 12 |
*/ |
| 13 |
public function __construct() { |
| 14 |
add_action( 'init', [ $this, 'init' ] ); |
| 15 |
} |
| 16 |
|
| 17 |
/** |
| 18 |
* Check, if block options are enabled |
| 19 |
* |
| 20 |
* @return bool |
| 21 |
*/ |
| 22 |
public static function enabled(): bool { |
| 23 |
$options = Plugin::get_options(); |
| 24 |
// if settings don’t exist, block options are enabled by default |
| 25 |
if ( ! array_key_exists( 'block_options', $options ) |
| 26 |
|| $options['block_options'] |
| 27 |
|| apply_filters( 'isc_force_block_options', false ) ) { |
| 28 |
return true; |
| 29 |
} |
| 30 |
return false; |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Register ISC fields to be usable with the REST API |
| 35 |
* |
| 36 |
* @return void |
| 37 |
*/ |
| 38 |
public function init() { |
| 39 |
if ( ! Plugin::is_module_enabled( 'image_sources' ) ) { |
| 40 |
return; |
| 41 |
} |
| 42 |
|
| 43 |
if ( ! function_exists( 'register_block_type' ) || ! self::enabled() ) { |
| 44 |
// if block options are disabled, at least add a link to the media library where one can adjust the source |
| 45 |
add_action( 'enqueue_block_editor_assets', [ $this, 'edit_link_assets' ] ); |
| 46 |
return; |
| 47 |
} |
| 48 |
|
| 49 |
add_action( 'enqueue_block_editor_assets', [ $this, 'editor_assets' ] ); |
| 50 |
add_action( 'update_option', [ $this, 'widgets_update_option' ], 10, 3 ); |
| 51 |
|
| 52 |
register_post_meta( |
| 53 |
'attachment', |
| 54 |
'isc_image_source', |
| 55 |
[ |
| 56 |
'show_in_rest' => true, |
| 57 |
'single' => true, |
| 58 |
'type' => 'string', |
| 59 |
] |
| 60 |
); |
| 61 |
register_post_meta( |
| 62 |
'attachment', |
| 63 |
'isc_image_source_url', |
| 64 |
[ |
| 65 |
'show_in_rest' => true, |
| 66 |
'single' => true, |
| 67 |
'type' => 'string', |
| 68 |
] |
| 69 |
); |
| 70 |
register_post_meta( |
| 71 |
'attachment', |
| 72 |
'isc_image_licence', |
| 73 |
[ |
| 74 |
'show_in_rest' => true, |
| 75 |
'single' => true, |
| 76 |
'type' => 'string', |
| 77 |
] |
| 78 |
); |
| 79 |
register_post_meta( |
| 80 |
'attachment', |
| 81 |
'isc_image_source_own', |
| 82 |
[ |
| 83 |
'show_in_rest' => true, |
| 84 |
'single' => true, |
| 85 |
'type' => 'boolean', |
| 86 |
] |
| 87 |
); |
| 88 |
|
| 89 |
// Post Types supporting the editor. |
| 90 |
$post_types_with_editor = get_post_types_by_support( [ 'editor' ] ); |
| 91 |
// Get all post types with the REST API enabled |
| 92 |
$post_types_with_block_editor = get_post_types( [ 'show_in_rest' => true ], 'names' ); |
| 93 |
// Get the intersection of both arrays. |
| 94 |
$block_ready_post_types = array_intersect( $post_types_with_editor, $post_types_with_block_editor ); |
| 95 |
|
| 96 |
foreach ( $block_ready_post_types as $type ) { |
| 97 |
// See https://developer.wordpress.org/reference/hooks/rest_after_insert_this-post_type/. |
| 98 |
add_action( "rest_after_insert_{$type}", [ $this, 'save_post' ] ); |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Update ISC fields when saving the widgets page |
| 104 |
* |
| 105 |
* @param string $name option name. |
| 106 |
* @param string|array|object $old_option option value before updating. |
| 107 |
* @param string|array|object $new_option new option value. |
| 108 |
* |
| 109 |
* @return void |
| 110 |
*/ |
| 111 |
public function widgets_update_option( $name, $old_option, $new_option ) { |
| 112 |
if ( $name !== 'widget_block' ) { |
| 113 |
// Do not tamper with other options. |
| 114 |
return; |
| 115 |
} |
| 116 |
|
| 117 |
foreach ( $new_option as $sidebar ) { |
| 118 |
if ( isset( $sidebar['content'] ) ) { |
| 119 |
$this->save_meta( $sidebar['content'] ); |
| 120 |
} |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* Grab ISC fields from a page|sidebar content then save the post meta |
| 126 |
* |
| 127 |
* @param string $content block editor content. |
| 128 |
* @param int $post_id currently edited post (when not on widgets/customizer). |
| 129 |
* |
| 130 |
* @return void |
| 131 |
*/ |
| 132 |
private function save_meta( $content, $post_id = 0 ) { |
| 133 |
preg_match_all( '#<!-- (wp:image|wp:media-text|wp:cover|wp:post-featured-image|wp:generateblocks/image)[^{]+({.+})#', $content, $results, PREG_SET_ORDER ); |
| 134 |
|
| 135 |
foreach ( $results as $match ) { |
| 136 |
$attributes = json_decode( $match[2], true ); |
| 137 |
$image_id = isset( $attributes['id'] ) ? (int) $attributes['id'] : 0; |
| 138 |
|
| 139 |
if ( isset( $attributes['mediaId'] ) ) { |
| 140 |
// Media and text. |
| 141 |
$image_id = (int) $attributes['mediaId']; |
| 142 |
} |
| 143 |
|
| 144 |
if ( $match[1] === 'wp:post-featured-image' && $post_id ) { |
| 145 |
// Post featured image. |
| 146 |
$image_id = get_post_thumbnail_id( $post_id ); |
| 147 |
} |
| 148 |
|
| 149 |
if ( ! $image_id ) { |
| 150 |
continue; |
| 151 |
} |
| 152 |
|
| 153 |
foreach ( [ 'isc_image_source', 'isc_image_source_url', 'isc_image_source_own', 'isc_image_licence' ] as $field ) { |
| 154 |
if ( $field === 'isc_image_source_own' ) { |
| 155 |
ISC_Model::update_post_meta( $image_id, $field, isset( $attributes[ $field ] ) && $attributes[ $field ] === true ? '1' : '' ); |
| 156 |
continue; |
| 157 |
} |
| 158 |
ISC_Model::update_post_meta( $image_id, $field, isset( $attributes[ $field ] ) ? $attributes[ $field ] : '' ); |
| 159 |
} |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
/** |
| 164 |
* Update ISC fields when saving post using the REST API. |
| 165 |
* |
| 166 |
* @param WP_Post $post the currently edited post. |
| 167 |
* |
| 168 |
* @return void |
| 169 |
*/ |
| 170 |
public function save_post( $post ) { |
| 171 |
$this->save_meta( $post->post_content, $post->ID ); |
| 172 |
} |
| 173 |
|
| 174 |
/** |
| 175 |
* Enqueue JS file and print all needed JS variables |
| 176 |
*/ |
| 177 |
public function editor_assets() { |
| 178 |
$dependencies = [ 'jquery', 'wp-api', 'lodash', 'wp-blocks', 'wp-element', 'wp-i18n' ]; |
| 179 |
$screen = get_current_screen(); |
| 180 |
Helpers::enqueue_script( 'isc_attachment_compat', 'admin/assets/js/wp.media.view.AttachmentCompat.js', [ 'media-upload' ] ); |
| 181 |
|
| 182 |
if ( $screen && isset( $screen->base ) && $screen->base !== 'widgets' ) { |
| 183 |
$dependencies[] = 'wp-editor'; |
| 184 |
} |
| 185 |
Helpers::register_script( |
| 186 |
'isc/image-block', |
| 187 |
'includes/block-options/isc-image-block.js', |
| 188 |
$dependencies, |
| 189 |
); |
| 190 |
|
| 191 |
$plugin_options = ISC\Plugin::get_options(); |
| 192 |
|
| 193 |
global $post, $pagenow; |
| 194 |
|
| 195 |
if ( ( ! empty( $post ) && current_user_can( 'edit_post', $post->ID ) ) || in_array( $pagenow, [ 'widgets.php', 'customize.php', 'site-editor.php' ], true ) ) { |
| 196 |
// The current user can edit the current post, or on widgets page or customizer. |
| 197 |
$isc_data = [ |
| 198 |
'option' => $plugin_options, |
| 199 |
'postmeta' => new stdClass(), |
| 200 |
]; |
| 201 |
|
| 202 |
// Add all our data as a variable in an inline script. |
| 203 |
wp_add_inline_script( 'isc/image-block', 'var iscData = ' . wp_json_encode( $isc_data ) . ';', 'before' ); |
| 204 |
} |
| 205 |
// separated from register_script since wp_add_inline_script needs a working handle |
| 206 |
wp_enqueue_script( 'isc/image-block' ); |
| 207 |
wp_set_script_translations( 'isc/image-block', 'image-source-control-isc', apply_filters( 'isc_path_to_languages', '' ) ); |
| 208 |
|
| 209 |
Helpers::enqueue_script( 'isc/media-upload', 'admin/assets/js/media-upload.js', [ 'media-upload' ] ); |
| 210 |
} |
| 211 |
|
| 212 |
/** |
| 213 |
* Enqueue script to add an edit button to image blocks |
| 214 |
*/ |
| 215 |
public function edit_link_assets() { |
| 216 |
Helpers::enqueue_script( 'isc/image-block-edit-link', 'includes/block-options/isc-image-block-edit-link.js', [ 'wp-blocks', 'wp-i18n', 'wp-element', 'wp-editor' ] ); |
| 217 |
|
| 218 |
wp_set_script_translations( 'isc/image-block-edit-link', 'image-source-control-isc', apply_filters( 'isc_path_to_languages', '' ) ); |
| 219 |
} |
| 220 |
} |
| 221 |
|