PluginProbe
Image Source Control Lite – Show Image Credits and Captions / 3.12.0
Image Source Control Lite – Show Image Credits and Captions v3.12.0
3.12.0 3.11.0 trunk 1.1 1.1.1 1.1.2 1.1.2.1 1.1.3 1.10 1.10.1 1.10.2 1.10.3 1.10.4 1.10.5 1.2 1.2.0.1 1.2.0.2 1.2.0.3 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.4.1 1.3.5 All 110 releases
image-source-control-isc / includes / block-options / block-options.php

block-options.php in Image Source Control Lite – Show Image Credits and Captions 3.12.0, at includes/block-options/block-options.php

235 lines 7.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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_Sources\Ai_Labels::META_KEY,
82 [
83 'sanitize_callback' => [ '\\ISC\\Image_Sources\\Ai_Labels', 'sanitize_label' ],
84 'show_in_rest' => true,
85 'single' => true,
86 'type' => 'string',
87 ]
88 );
89 register_post_meta(
90 'attachment',
91 'isc_image_source_own',
92 [
93 'show_in_rest' => true,
94 'single' => true,
95 'type' => 'boolean',
96 ]
97 );
98
99 // Post Types supporting the editor.
100 $post_types_with_editor = get_post_types_by_support( [ 'editor' ] );
101 // Get all post types with the REST API enabled
102 $post_types_with_block_editor = get_post_types( [ 'show_in_rest' => true ], 'names' );
103 // Get the intersection of both arrays.
104 $block_ready_post_types = array_intersect( $post_types_with_editor, $post_types_with_block_editor );
105
106 foreach ( $block_ready_post_types as $type ) {
107 // See https://developer.wordpress.org/reference/hooks/rest_after_insert_this-post_type/.
108 add_action( "rest_after_insert_{$type}", [ $this, 'save_post' ] );
109 }
110 }
111
112 /**
113 * Update ISC fields when saving the widgets page
114 *
115 * @param string $name option name.
116 * @param string|array|object $old_option option value before updating.
117 * @param string|array|object $new_option new option value.
118 *
119 * @return void
120 */
121 public function widgets_update_option( $name, $old_option, $new_option ) {
122 if ( $name !== 'widget_block' ) {
123 // Do not tamper with other options.
124 return;
125 }
126
127 foreach ( $new_option as $sidebar ) {
128 if ( isset( $sidebar['content'] ) ) {
129 $this->save_meta( $sidebar['content'] );
130 }
131 }
132 }
133
134 /**
135 * Grab ISC fields from a page|sidebar content then save the post meta
136 *
137 * @param string $content block editor content.
138 * @param int $post_id currently edited post (when not on widgets/customizer).
139 *
140 * @return void
141 */
142 private function save_meta( $content, $post_id = 0 ) {
143 preg_match_all( '#<!-- (wp:image|wp:media-text|wp:cover|wp:post-featured-image|wp:generateblocks/image)[^{]+({.+})#', $content, $results, PREG_SET_ORDER );
144
145 foreach ( $results as $match ) {
146 $attributes = json_decode( $match[2], true );
147 $image_id = isset( $attributes['id'] ) ? (int) $attributes['id'] : 0;
148
149 if ( isset( $attributes['mediaId'] ) ) {
150 // Media and text.
151 $image_id = (int) $attributes['mediaId'];
152 }
153
154 if ( $match[1] === 'wp:post-featured-image' && $post_id ) {
155 // Post featured image.
156 $image_id = get_post_thumbnail_id( $post_id );
157 }
158
159 if ( ! $image_id ) {
160 continue;
161 }
162
163 foreach ( [ 'isc_image_source', 'isc_image_source_url', 'isc_image_source_own', \ISC\Image_Sources\Ai_Labels::META_KEY, 'isc_image_licence' ] as $field ) {
164 if ( $field === 'isc_image_source_own' ) {
165 ISC_Model::update_post_meta( $image_id, $field, isset( $attributes[ $field ] ) && $attributes[ $field ] === true ? '1' : '' );
166 continue;
167 }
168 if ( $field === \ISC\Image_Sources\Ai_Labels::META_KEY ) {
169 ISC_Model::update_post_meta( $image_id, $field, \ISC\Image_Sources\Ai_Labels::sanitize_label( isset( $attributes[ $field ] ) ? $attributes[ $field ] : '' ) );
170 continue;
171 }
172 ISC_Model::update_post_meta( $image_id, $field, isset( $attributes[ $field ] ) ? $attributes[ $field ] : '' );
173 }
174 }
175 }
176
177 /**
178 * Update ISC fields when saving post using the REST API.
179 *
180 * @param WP_Post $post the currently edited post.
181 *
182 * @return void
183 */
184 public function save_post( $post ) {
185 $this->save_meta( $post->post_content, $post->ID );
186 }
187
188 /**
189 * Enqueue JS file and print all needed JS variables
190 */
191 public function editor_assets() {
192 $dependencies = [ 'jquery', 'wp-api', 'lodash', 'wp-blocks', 'wp-element', 'wp-i18n' ];
193 $screen = get_current_screen();
194 Helpers::enqueue_script( 'isc_attachment_compat', 'admin/assets/js/wp.media.view.AttachmentCompat.js', [ 'media-upload' ] );
195
196 if ( $screen && isset( $screen->base ) && $screen->base !== 'widgets' ) {
197 $dependencies[] = 'wp-editor';
198 }
199 Helpers::register_script(
200 'isc/image-block',
201 'includes/block-options/isc-image-block.js',
202 $dependencies,
203 );
204
205 $plugin_options = ISC\Plugin::get_options();
206
207 global $post, $pagenow;
208
209 if ( ( ! empty( $post ) && current_user_can( 'edit_post', $post->ID ) ) || in_array( $pagenow, [ 'widgets.php', 'customize.php', 'site-editor.php' ], true ) ) {
210 // The current user can edit the current post, or on widgets page or customizer.
211 $isc_data = [
212 'option' => $plugin_options,
213 'postmeta' => new stdClass(),
214 ];
215
216 // Add all our data as a variable in an inline script.
217 wp_add_inline_script( 'isc/image-block', 'var iscData = ' . wp_json_encode( $isc_data ) . ';', 'before' );
218 }
219 // separated from register_script since wp_add_inline_script needs a working handle
220 wp_enqueue_script( 'isc/image-block' );
221 wp_set_script_translations( 'isc/image-block', 'image-source-control-isc', apply_filters( 'isc_path_to_languages', '' ) );
222
223 Helpers::enqueue_script( 'isc/media-upload', 'admin/assets/js/media-upload.js', [ 'media-upload' ] );
224 }
225
226 /**
227 * Enqueue script to add an edit button to image blocks
228 */
229 public function edit_link_assets() {
230 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' ] );
231
232 wp_set_script_translations( 'isc/image-block-edit-link', 'image-source-control-isc', apply_filters( 'isc_path_to_languages', '' ) );
233 }
234 }
235