PluginProbe
Image Source Control Lite – Show Image Credits and Captions / 1.10
Image Source Control Lite – Show Image Credits and Captions v1.10
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 / gutenberg / gutenberg.php

gutenberg.php in Image Source Control Lite – Show Image Credits and Captions 1.10, at gutenberg/gutenberg.php

102 lines 3.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 function isc_licences_text_to_array($licences = '') {
4 if($licences == '') return false;
5 // split the text by line
6 $licences_array = preg_split('/\r?\n/', trim($licences));
7 if(count($licences_array) == 0 ) return false;
8 // create the array with licence => url
9 $new_licences = array();
10 foreach($licences_array as $_licence) {
11 if(trim($_licence) != '') {
12 $temp = explode('|', $_licence);
13 $new_licences[$temp[0]] = array();
14 if( isset($temp[1]))
15 $new_licences[$temp[0]]['url'] = esc_url ($temp[1]);
16 }
17 }
18
19 if($new_licences == array()) return false;
20 else return $new_licences;
21 }
22
23 function isc_addSourceToCoreImageBlock() {
24 wp_enqueue_script(
25 'source-to-core-image-block',
26 plugin_dir_url(__FILE__) . 'source-to-core-image-block.js',
27 array('jquery', 'wp-api', 'lodash', 'wp-blocks', 'wp-editor', 'wp-element', 'wp-i18n',),
28 true
29 );
30
31 //Reading the options and making them avaialable for our Gutenberg block
32 $options = get_option('isc_options');
33 $licenses_js_array = array();
34 if($options['enable_licences'] && $licences = isc_licences_text_to_array($options['licences'])) {
35 $licenses_js_array[] = array('value' => '', 'label' => '--');
36 foreach($licences as $_licence_name => $_licence_data) {
37 $licenses_js_array[] = array('value' => $_licence_name, 'label' => $_licence_name);
38 }
39 }
40 wp_localize_script('source-to-core-image-block', 'isc_options', $licenses_js_array );
41 if(function_exists('wp_set_script_translations')) {
42 wp_set_script_translations('image-source-control-isc', 'image-source-control-isc');
43 }
44 }
45
46 //Making our custom meta fields available to wp rest api.
47 function isc_register_api_fields() {
48 register_rest_field( 'attachment', 'isc_image_source',
49 array(
50 'get_callback' => 'isc_get_attachment_meta',
51 'update_callback' => 'slug_update_attachment_meta',
52 'schema' => null,
53 'auth_callback' => function(){
54 return current_user_can('edit_posts');
55 }
56 )
57 );
58 register_rest_field( 'attachment', 'isc_image_source_own',
59 array(
60 'get_callback' => 'isc_get_attachment_meta',
61 'update_callback' => 'slug_update_attachment_meta',
62 'schema' => null,
63 'auth_callback' => function(){
64 return current_user_can('edit_posts');
65 }
66 )
67 );
68 register_rest_field( 'attachment', 'isc_image_source_url',
69 array(
70 'get_callback' => 'isc_get_attachment_meta',
71 'update_callback' => 'slug_update_attachment_meta',
72 'schema' => null,
73 'auth_callback' => function(){
74 return current_user_can('edit_posts');
75 }
76 )
77 );
78 register_rest_field( 'attachment', 'isc_image_licence',
79 array(
80 'get_callback' => 'isc_get_attachment_meta',
81 'update_callback' => 'slug_update_attachment_meta',
82 'schema' => null,
83 'auth_callback' => function(){
84 return current_user_can('edit_posts');
85 }
86 )
87 );
88 }
89
90 //Function to get a meta field, otherwise it will return a null value
91 function isc_get_attachment_meta($object, $field_name, $request) {
92 return get_post_meta($object['id'], $field_name, true);
93 }
94 //Function to get a meta field, otherwise it becomes read only.
95 function slug_update_attachment_meta($value, $object, $field_name ) {
96 return update_post_meta($object->ID, $field_name, $value);
97 }
98
99 add_action('init', function() { wp_enqueue_script( 'wp-api' ); });
100 add_action('rest_api_init', 'isc_register_api_fields');
101 add_action('enqueue_block_editor_assets', 'isc_addSourceToCoreImageBlock');
102