PluginProbe
Photonic Gallery & Lightbox for Flickr, SmugMug & Others / 2.41
Photonic Gallery & Lightbox for Flickr, SmugMug & Others v2.41
3.36 3.35 3.34 3.33 2.19 2.20 2.21 2.22 2.23 2.24 2.25 2.26 2.27 2.28 2.29 2.30 2.31 2.32 2.33 2.34 2.40 2.41 2.42 2.43 2.44 All 141 releases
photonic / Admin / Admin.php

Admin.php in Photonic Gallery & Lightbox for Flickr, SmugMug & Others 2.41, at Admin/Admin.php

306 lines 12.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Photonic_Plugin\Admin;
3
4 use Photonic_Plugin\Admin\Wizard\Wizard;
5 use Photonic_Plugin\Core\Photonic;
6 use Photonic_Plugin\Modules\SmugMug;
7
8 class Admin {
9 function __construct() {
10 global $photonic_disable_flow_editor_global;
11
12 // General
13 add_action('admin_head', [$this, 'admin_head']);
14 add_action('admin_enqueue_scripts', [&$this, 'add_admin_scripts']);
15
16 // Vanilla Editor
17 add_filter('media_upload_tabs', [&$this, 'media_upload_tabs']);
18 add_action('media_upload_photonic', [&$this, 'media_upload_photonic']);
19
20 add_action('print_media_templates', [&$this, 'edit_gallery']);
21
22 // Gutenberg
23 add_action('admin_notices', [&$this, 'admin_notices']);
24 add_action('enqueue_block_editor_assets', [&$this, 'enqueue_gutenberg_assets']);
25
26 if (empty($photonic_disable_flow_editor_global)) {
27 add_action('media_buttons', [&$this, 'add_photonic_button']);
28 add_action('admin_action_photonic_wizard', [&$this, 'open_wizard']);
29 add_action('wp_ajax_photonic_wizard_next_screen', [&$this, 'wizard_next_screen']);
30 add_action('wp_ajax_nopriv_photonic_wizard_next_screen', [&$this, 'wizard_next_screen']);
31 add_action('wp_ajax_photonic_wizard_more', [&$this, 'flow_more']);
32 add_action('wp_ajax_nopriv_photonic_wizard_more', [&$this, 'flow_more']);
33 }
34
35 if (class_exists('\FLBuilderModel') && \FLBuilderModel::is_builder_active()) {
36 $this->enqueue_widget_scripts();
37 }
38 }
39
40 function admin_head() {
41 // check user permissions
42 if (!current_user_can('edit_posts') && !current_user_can('edit_pages')) {
43 return;
44 }
45
46 global $photonic_disable_editor, $photonic_disable_editor_post_type;
47 $disabled_types = explode(',', $photonic_disable_editor_post_type);
48 $screen = get_current_screen();
49 $post_type = empty($_REQUEST['post_type']) ? 'post' : $_REQUEST['post_type'];
50 // check if WYSIWYG is enabled
51 if ('true' == get_user_option('rich_editing') && empty($photonic_disable_editor) && !in_array($post_type, $disabled_types) && $screen->base == 'post') {
52 $this->prepare_mce_data();
53 add_filter('mce_external_plugins', [$this ,'mce_photonic'], 5);
54 add_filter('mce_buttons', [$this ,'mce_flow_button'], 5);
55 }
56 }
57
58 function add_photonic_button() {
59 add_thickbox();
60 $url = $this->get_wizard_thickbox_url();
61
62 printf('<a href="%1$s" class="button photonic-button thickbox" id="photonic-add-gallery" title="Photonic Gallery"><img class="wp-media-buttons-icon" src="'.PHOTONIC_URL.'include/images/Photonic-20.png'.'" alt="%2$s" /> %2$s</a>',
63 $url, esc_html__( 'Add / Edit Photonic Gallery', 'photonic'));
64 }
65
66 function open_wizard() {
67 define( 'IFRAME_REQUEST', true );
68 $this->enqueue_wizard_scripts();
69 iframe_header(esc_html__('Add / Edit Photonic Gallery', 'photonic'));
70 require_once(PHOTONIC_PATH.'/Admin/Wizard/Screen_Flow.php');
71 iframe_footer();
72 exit;
73 }
74
75 function enqueue_wizard_scripts() {
76 global $photonic_alternative_shortcode;
77 $wizard_js = [
78 'ajaxurl' => admin_url('admin-ajax.php'),
79 'shortcode' => empty($photonic_alternative_shortcode) ? 'gallery' : $photonic_alternative_shortcode,
80 'insert_gallery' => esc_html__('Insert Gallery', 'photonic'),
81 'update_gallery' => esc_html__('Update Gallery', 'photonic'),
82 'error_mandatory' => esc_html__('Please fill the mandatory fields. Mandatory fields are marked with a red "*".', 'photonic'),
83 'media_library_title' => esc_html__('Select from WordPress Media Library', 'photonic'),
84 'media_library_button' => esc_html__('Select', 'photonic'),
85 'info_editor_not_shortcode' => esc_html__('The text selected in the editor is not a Photonic shortcode. Creating a new shortcode.', 'photonic'),
86 'info_editor_block_select' => sprintf(esc_html__('%1$sHint:%2$s To edit an existing Photonic block simply click on the block.', 'photonic'), '<strong>', '</strong>'),
87 ];
88 if (!empty($_REQUEST['shortcode'])) {
89 $wizard_js['shortcode'] = $_REQUEST['shortcode'];
90 }
91 wp_enqueue_style('photonic-flow', PHOTONIC_URL.'include/css/admin/admin-flow.css', [], Photonic::get_version(PHOTONIC_PATH.'/include/css/admin/admin-flow.css'));
92 wp_enqueue_script('photonic-flow-js', PHOTONIC_URL.'include/scripts/admin/flow.js', ['jquery'], Photonic::get_version(PHOTONIC_PATH.'/include/scripts/admin/flow.js'));
93 wp_localize_script('photonic-flow-js', 'Photonic_Wizard_JS', $wizard_js);
94 }
95
96 function wizard_next_screen() {
97 require_once(PHOTONIC_PATH."/Admin/Wizard/Wizard.php");
98 if (isset($_POST['provider'])) {
99 $wizard = new Wizard();
100 echo $wizard->get_screen();
101 }
102 die();
103 }
104
105 function flow_more() {
106 require_once(PHOTONIC_PATH."/Admin/Wizard/Wizard.php");
107 if (isset($_POST['url']) && isset($_POST['provider']) && isset($_POST['display_type'])) {
108 $url = base64_decode(sanitize_text_field($_POST['url']));
109
110 $provider = sanitize_text_field($_POST['provider']);
111 $display_type = sanitize_text_field($_POST['display_type']);
112 $args = ['sslverify' => PHOTONIC_SSL_VERIFY];
113 if ($provider == 'smugmug') {
114 $gallery = SmugMug::get_instance();
115
116 $body = [
117 'APIKey' => $gallery->api_key,
118 '_accept' => 'application/json',
119 '_expandmethod' => 'inline',
120 '_verbosity' => '1',
121 ];
122
123 if ($display_type == 'album-photo' || $display_type == 'multi-album') {
124 $body['_expand'] = 'HighlightImage.ImageSizes';
125 }
126
127 $args['body'] = $body;
128 }
129
130 // $response = wp_remote_request($url, ['sslverify' => PHOTONIC_SSL_VERIFY]);
131 $response = wp_remote_request($url, $args);
132 $wizard = new Wizard();
133 $objects = $wizard->process_response($response, $provider, $display_type, [], [], $url, true);
134
135 if (!empty($objects['success'])) {
136 echo $objects['success'];
137 }
138 else if (!empty($objects['error'])) {
139 echo $objects['error'];
140 }
141 }
142 die();
143 }
144
145 /**
146 * Adds all scripts and their dependencies to the <head> of the Photonic administration page. This takes care to not add scripts on other admin pages.
147 *
148 * @param $hook
149 * @return void
150 */
151 function add_admin_scripts($hook) {
152 if ('media-upload-popup' == $hook) {
153 wp_enqueue_script('jquery');
154 wp_enqueue_style('photonic-upload', PHOTONIC_URL.'include/css/admin/admin-form.css', [], Photonic::get_version(PHOTONIC_PATH.'/include/css/admin/admin-form.css'));
155 }
156 else if ('post-new.php' == $hook || 'post.php' == $hook) {
157 global $photonic_disable_editor, $photonic_disable_editor_post_type;
158 $disabled_types = explode(',', $photonic_disable_editor_post_type);
159 $post_type = empty($_REQUEST['post_type']) ? 'post' : $_REQUEST['post_type'];
160 wp_enqueue_style('photonic-upload', PHOTONIC_URL.'include/css/admin/admin-form.css', [], Photonic::get_version(PHOTONIC_PATH.'/include/css/admin/admin-form.css'));
161 if (empty($photonic_disable_editor) && !in_array($post_type, $disabled_types)) {
162 $this->prepare_mce_data();
163
164 add_editor_style(PHOTONIC_URL.'include/css/admin/admin-editor.css?'.Photonic::get_version(PHOTONIC_PATH.'/include/css/admin/admin-editor.css'));
165 }
166 }
167 else if ('widgets.php' == $hook) {
168 Photonic::enqueue_widget_scripts();
169 }
170 }
171
172 function prepare_mce_data() {
173 $url = $this->get_wizard_thickbox_url();
174 $js_array = $this->get_wizard_js_parameters($url);
175
176 wp_enqueue_script('photonic-admin-js', PHOTONIC_URL.'include/scripts/admin/gallery-settings.js', ['jquery', 'media-views', 'media-upload'], Photonic::get_version(PHOTONIC_PATH.'/include/scripts/admin/gallery-settings.js'));
177 wp_localize_script('photonic-admin-js', 'Photonic_Admin_JS', $js_array);
178 }
179
180 function mce_photonic($plugin_array) {
181 $plugin_array['photonic'] = PHOTONIC_URL.'include/scripts/admin/mce.js?'.Photonic::get_version(PHOTONIC_PATH.'/include/scripts/admin/mce.js');
182 return $plugin_array;
183 }
184
185 function mce_flow_button($buttons) {
186 array_push($buttons, 'photonic_wizard');
187 return $buttons;
188 }
189
190 function enqueue_gutenberg_assets() {
191 if (function_exists('register_block_type')) {
192 wp_enqueue_script('photonic-gutenberg',
193 PHOTONIC_URL.'include/scripts/admin/block.js',
194 ['jquery', 'wp-blocks', 'wp-i18n', 'wp-element', 'shortcode', 'thickbox'],
195 Photonic::get_version(PHOTONIC_PATH.'/include/scripts/admin/block.js')
196 );
197
198 if (function_exists( 'gutenberg_get_jed_locale_data')) {
199 $locale = gutenberg_get_jed_locale_data('photonic');
200 $content = 'wp.i18n.setLocaleData('.json_encode($locale).', "photonic");';
201 wp_script_add_data( 'photonic-gutenberg', 'data', $content );
202 }
203
204 $url = $this->get_wizard_thickbox_url();
205 $js_array = $this->get_wizard_js_parameters($url);
206 wp_localize_script('photonic-gutenberg', 'Photonic_Gutenberg_JS', $js_array);
207
208 wp_enqueue_style('photonic-gutenberg',
209 PHOTONIC_URL.'include/css/admin/admin-block.css',
210 ['thickbox'],
211 Photonic::get_version(PHOTONIC_PATH.'/include/css/admin/admin-block.css')
212 );
213 }
214 }
215
216 /**
217 * Adds a "Photonic" tab to the "Add Media" panel.
218 *
219 * @param $tabs
220 * @return array
221 */
222 function media_upload_tabs($tabs) {
223 if (!function_exists('is_gutenberg_page') || (function_exists('is_gutenberg_page') && !is_gutenberg_page())) {
224 $tabs['photonic'] = 'Photonic';
225 }
226 return $tabs;
227 }
228
229 /**
230 * Invokes the form to display the photonic insertion screen in the "Add Media" panel. The call to wp_iframe ensures that the right CSS and JS are called.
231 *
232 * @return void
233 */
234 function media_upload_photonic() {
235 wp_iframe([&$this, 'media_upload_photonic_form']);
236 }
237
238 /**
239 * First prints the standard buttons for media upload, then shows the UI for Photonic.
240 *
241 * @return void
242 */
243 function media_upload_photonic_form() {
244 media_upload_header();
245 require_once(PHOTONIC_PATH."/Admin/Forms/Add_Gallery.php");
246 }
247
248 function edit_gallery() {
249 global $photonic_disable_editor, $photonic_disable_editor_post_type;
250 $disabled_types = explode(',', $photonic_disable_editor_post_type);
251 $post_type = empty($_REQUEST['post_type']) ? 'post' : $_REQUEST['post_type'];
252 // check if WYSIWYG is enabled
253 if ('true' == get_user_option('rich_editing') && empty($photonic_disable_editor) && !in_array($post_type, $disabled_types)) {
254 require_once(PHOTONIC_PATH."/Admin/Forms/Edit_Gallery_Templates.php");
255 }
256 }
257
258 /**
259 * @return string
260 */
261 private function get_wizard_thickbox_url() {
262 $url = add_query_arg([
263 'action' => 'photonic_wizard',
264 'class' => 'photonic-flow',
265 'post_id' => empty($_REQUEST['post']) ? '' : $_REQUEST['post'],
266 'width' => '1000',
267 'height' => '600',
268 'TB_iframe' => 'true',
269 ], admin_url('admin.php'));
270 return $url;
271 }
272
273 /**
274 * @param string $url
275 * @return array
276 */
277 private function get_wizard_js_parameters(string $url) {
278 global $photonic_alternative_shortcode, $photonic_disable_flow_editor, $photonic_disable_flow_editor_global;
279 $js_array = [
280 'flow_url' => $url,
281 'ajaxurl' => admin_url('admin-ajax.php'),
282 'shortcode' => empty($photonic_alternative_shortcode) ? 'gallery' : $photonic_alternative_shortcode,
283 'disable_flow' => !empty($photonic_disable_flow_editor) || !empty($photonic_disable_flow_editor_global),
284 'default_gallery_type' => 'default',
285 'plugin_dir' => plugin_dir_url(__FILE__),
286 ];
287 return $js_array;
288 }
289
290 function admin_notices() {
291 if (!empty($_REQUEST['page']) && in_array($_REQUEST['page'], ['photonic-options-manager', 'photonic-getting-started', 'photonic-auth', 'photonic-helpers'])){
292 if (function_exists('register_block_type')) {
293 $dismissed = get_user_meta(get_current_user_id(), 'photonic_gutenberg');
294 if (!$dismissed) {
295 echo "<div class='notice notice-warning'>\n<p>\n";
296 echo sprintf(esc_html__('It looks like you are using the Gutenberg editor. Please ensure that you have followed the instructions under %s.', 'photonic'), '<em><a href="'.admin_url('admin.php?page=photonic-gutenberg').'">Photonic &rarr; Switch to Gutenberg</a></em>');
297 echo "</p>\n";
298 echo "<button type='button' data-photonic-dismissible='gutenberg' class='photonic-notice-dismiss'><span class='screen-reader-text'>Dismiss this notice.</span></button>";
299 echo "</div>\n";
300 }
301 }
302 }
303 }
304 }
305
306 new Admin();