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