class-admin-notice-custom-css.php
1 week ago
class-admin-notices.php
1 week ago
class-admin.php
1 week ago
class-attachment-fields.php
1 week ago
class-columns.php
1 week ago
class-demo-content.php
1 week ago
class-extensions.php
1 week ago
class-gallery-attachment-modal.php
1 week ago
class-gallery-datasources.php
1 week ago
class-gallery-editor.php
1 week ago
class-gallery-metabox-fields.php
1 week ago
class-gallery-metabox-items.php
1 week ago
class-gallery-metabox-settings-helper.php
1 week ago
class-gallery-metabox-settings.php
1 week ago
class-gallery-metabox-template.php
1 week ago
class-gallery-metaboxes.php
1 week ago
class-menu.php
1 week ago
class-pro-promotion.php
1 week ago
class-settings.php
1 week ago
class-silent-installer-skin.php
1 week ago
class-trial-mode.php
1 week ago
demo-content-galleries.php
1 week ago
demo-content-images.php
1 week ago
index.php
1 week ago
pro-features.php
1 week ago
view-features.php
1 week ago
view-help-demos.php
1 week ago
view-help-getting-started.php
1 week ago
view-help-pro.php
1 week ago
view-help.php
1 week ago
view-system-info.php
1 week ago
class-gallery-editor.php
353 lines
| 1 | <?php |
| 2 | |
| 3 | if ( ! defined( 'ABSPATH' ) ) { |
| 4 | exit; |
| 5 | } |
| 6 | |
| 7 | /* |
| 8 | * FooGallery Admin Gallery MetaBoxes class |
| 9 | */ |
| 10 | |
| 11 | if ( ! class_exists( 'FooGallery_Admin_Gallery_Editor' ) ) { |
| 12 | |
| 13 | class FooGallery_Admin_Gallery_Editor { |
| 14 | |
| 15 | /** |
| 16 | * Primary class constructor. |
| 17 | */ |
| 18 | public function __construct() { |
| 19 | //adds a media button to the editor |
| 20 | add_action( 'media_buttons', array( $this, 'add_media_button') ); |
| 21 | |
| 22 | //add a tinymce plugin |
| 23 | add_action( 'admin_head', array( $this, 'add_tinymce_plugin' ) ); |
| 24 | |
| 25 | // Ajax calls for showing all galleries in the modal |
| 26 | add_action( 'wp_ajax_foogallery_load_galleries', array( $this, 'ajax_galleries_html' ) ); |
| 27 | |
| 28 | add_action( 'wp_ajax_foogallery_tinymce_load_info', array( $this, 'ajax_get_gallery_info' ) ); |
| 29 | } |
| 30 | |
| 31 | private function should_hide_editor_button() { |
| 32 | return 'on' == foogallery_get_setting( 'hide_editor_button', false ); |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * Adds a gallery insert button into the editor |
| 37 | * |
| 38 | * @param string $editor_id the instance id of the current editor |
| 39 | * |
| 40 | * @return string $buttons the amended media buttons |
| 41 | */ |
| 42 | public function add_media_button( $editor_id ) { |
| 43 | |
| 44 | if ( $this->should_hide_editor_button() ) { |
| 45 | return; |
| 46 | } |
| 47 | |
| 48 | //render the gallery modal |
| 49 | add_action( 'admin_footer', array( $this, 'render_gallery_modal' ) ); |
| 50 | add_action( 'wp_footer', array( $this, 'render_gallery_modal' ) ); |
| 51 | |
| 52 | $url = FOOGALLERY_URL . 'css/admin-foogallery-gallery-piles.css'; |
| 53 | wp_enqueue_style( 'admin-foogallery-gallery-piles', $url, array(), FOOGALLERY_VERSION ); |
| 54 | |
| 55 | $foogallery = FooGallery_Plugin::get_instance(); |
| 56 | $foogallery->register_and_enqueue_js( 'admin-foogallery-editor.js' ); |
| 57 | |
| 58 | ?> |
| 59 | <button type="button" class="button foogallery-modal-trigger" |
| 60 | <?php /* translators: %s: Value inserted at runtime. */ ?> |
| 61 | title="<?php esc_attr_e( sprintf( __( 'Add Gallery From %s', 'foogallery' ), foogallery_plugin_name() ) ); ?>" |
| 62 | style="padding-left: .4em;" |
| 63 | data-editor="<?php esc_attr_e( $editor_id ); ?>"> |
| 64 | <?php /* translators: %s: Value inserted at runtime. */ ?> |
| 65 | <span class="wp-media-buttons-icon dashicons dashicons-format-gallery"></span> <?php echo esc_html( sprintf( __( 'Add %s', 'foogallery' ), foogallery_plugin_name() ) ); ?> |
| 66 | </button> |
| 67 | <?php |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Adds our custom plugin to the tinyMCE editor |
| 72 | */ |
| 73 | public function add_tinymce_plugin() { |
| 74 | |
| 75 | // get out if we do not want to add the button |
| 76 | if ( $this->should_hide_editor_button() ) { |
| 77 | return; |
| 78 | } |
| 79 | |
| 80 | // check user permissions |
| 81 | if ( ! current_user_can( 'edit_posts' ) && ! current_user_can( 'edit_pages' ) ) { |
| 82 | return; |
| 83 | } |
| 84 | // check if WYSIWYG is enabled |
| 85 | if ( 'true' == get_user_option( 'rich_editing' ) ) { |
| 86 | add_filter( 'mce_external_plugins', array( $this, 'add_tinymce_js' ) ); |
| 87 | add_filter( 'mce_css', array( $this, 'add_tinymce_css' ) ); |
| 88 | add_action( 'admin_footer', array( $this, 'render_tinymce_nonce') ); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Include a plugin script into the editor |
| 94 | * @param $plugin_array |
| 95 | * |
| 96 | * @return mixed |
| 97 | */ |
| 98 | public function add_tinymce_js( $plugin_array ) { |
| 99 | $plugin_array['foogallery'] = FOOGALLERY_URL . 'js/admin-tinymce.js'; |
| 100 | return $plugin_array; |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Include a plugin script into the editor |
| 105 | * @param $mce_css |
| 106 | * |
| 107 | * @return string |
| 108 | */ |
| 109 | public function add_tinymce_css( $mce_css ) { |
| 110 | if ( ! empty( $mce_css ) ) { |
| 111 | $mce_css .= ','; |
| 112 | } |
| 113 | |
| 114 | $mce_css .= FOOGALLERY_URL . 'css/admin-tinymce.css'; // . urlencode( '?v=' + FOOGALLERY_VERSION ); |
| 115 | |
| 116 | return $mce_css; |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Renders a nonce field that is used in our AJAX calls for the visual editor |
| 121 | */ |
| 122 | public function render_tinymce_nonce() { |
| 123 | ?> |
| 124 | <input id="foogallery-timnymce-action-nonce" type="hidden" value="<?php echo esc_attr( wp_create_nonce( 'foogallery-timymce-nonce' ) ); ?>" /> |
| 125 | <?php |
| 126 | } |
| 127 | |
| 128 | |
| 129 | /** |
| 130 | * Renders the gallery modal for use in the editor |
| 131 | */ |
| 132 | public function render_gallery_modal() { |
| 133 | |
| 134 | ?> |
| 135 | <style> |
| 136 | .foogallery-modal-reload-container { |
| 137 | display: inline-block; |
| 138 | margin-left: 10px; |
| 139 | } |
| 140 | .foogallery-modal-reload-container a.button { |
| 141 | margin-top:10px !important; |
| 142 | } |
| 143 | .foogallery-modal-reload-container a span { |
| 144 | margin-top: 3px; |
| 145 | } |
| 146 | .foogallery-modal-reload-container .spinner { |
| 147 | position: absolute; |
| 148 | top: 15px; |
| 149 | display: inline-block; |
| 150 | margin-left: 5px; |
| 151 | } |
| 152 | |
| 153 | .foogallery-add-gallery { |
| 154 | background: #444; |
| 155 | } |
| 156 | |
| 157 | .foogallery-add-gallery span::after { |
| 158 | background: #ddd; |
| 159 | -webkit-border-radius: 50%; |
| 160 | border-radius: 50%; |
| 161 | display: inline-block; |
| 162 | content: '\f132'; |
| 163 | -webkit-font-smoothing: antialiased; |
| 164 | font: normal 75px/115px 'dashicons'; |
| 165 | width: 100px; |
| 166 | height: 100px; |
| 167 | vertical-align: middle; |
| 168 | text-align: center; |
| 169 | color: #999; |
| 170 | position: absolute; |
| 171 | top: 40%; |
| 172 | left: 50%; |
| 173 | margin-left: -50px; |
| 174 | margin-top: -50px; |
| 175 | padding: 0; |
| 176 | text-shadow: none; |
| 177 | z-index: 4; |
| 178 | text-indent: -4px; |
| 179 | } |
| 180 | |
| 181 | .foogallery-add-gallery:hover span::after { |
| 182 | background: #1E8CBE; |
| 183 | color: #444; |
| 184 | } |
| 185 | |
| 186 | </style> |
| 187 | <?php wp_nonce_field( 'foogallery_load_galleries', 'foogallery_load_galleries', false ); ?> |
| 188 | <div class="foogallery-modal-wrapper" style="display: none;"> |
| 189 | <div class="media-modal wp-core-ui"> |
| 190 | <button type="button" class="media-modal-close"><span class="media-modal-icon"><span class="screen-reader-text">Close media panel</span></span></button> |
| 191 | <div class="media-modal-content"> |
| 192 | <div class="media-frame wp-core-ui hide-menu hide-router foogallery-meta-wrap"> |
| 193 | <div class="media-frame-title foogallery-modal-title"> |
| 194 | <h1> |
| 195 | <?php esc_html_e( 'Choose a gallery to insert', 'foogallery' ); ?> |
| 196 | <div class="foogallery-modal-reload-container"> |
| 197 | <div class="spinner"></div> |
| 198 | <a class="foogallery-modal-reload button" href="#"><span class="dashicons dashicons-update"></span> <?php esc_html_e( 'Reload', 'foogallery' ); ?></a> |
| 199 | </div> |
| 200 | </h1> |
| 201 | </div> |
| 202 | <div class="media-frame-content foogallery-modal-container"> |
| 203 | <div class="attachments-browser"> |
| 204 | <ul class="foogallery-attachment-container attachments" style="padding-left: 8px; top: 1em;"> |
| 205 | <div class="foogallery-modal-loading"><?php esc_html_e( 'Loading galleries...', 'foogallery' ); ?></div> |
| 206 | </ul> |
| 207 | <!-- end .foogallery-meta --> |
| 208 | <div class="media-sidebar"> |
| 209 | <div class="foogallery-modal-sidebar"> |
| 210 | <h3><?php esc_html_e( 'Select A Gallery', 'foogallery' ); ?></h3> |
| 211 | <p> |
| 212 | <?php esc_html_e( 'Select a gallery by clicking it, and then click the "Insert Gallery" button to insert it into your content.', 'foogallery' ); ?> |
| 213 | </p> |
| 214 | <h3><?php esc_html_e( 'Add A Gallery', 'foogallery' ); ?></h3> |
| 215 | <p> |
| 216 | <?php esc_html_e( 'You can add a new gallery by clicking the "Add New Gallery" tile on the left. It will open in a new window.', 'foogallery' ); ?> |
| 217 | </p> |
| 218 | <p> |
| 219 | <?php esc_html_e( 'Once you have finished adding a gallery, come back to this dialog and click the "Reload" button to see your newly created gallery.', 'foogallery' ); ?> |
| 220 | </p> |
| 221 | </div> |
| 222 | <!-- end .foogallery-meta-sidebar --> |
| 223 | </div> |
| 224 | <!-- end .media-sidebar --> |
| 225 | </div> |
| 226 | <!-- end .attachments-browser --> |
| 227 | </div> |
| 228 | <!-- end .media-frame-content --> |
| 229 | <div class="media-frame-toolbar foogallery-modal-toolbar"> |
| 230 | <div class="media-toolbar"> |
| 231 | <div class="media-toolbar-secondary"> |
| 232 | <a href="#" class="foogallery-modal-cancel button media-button button-large button-secondary media-button-insert" title="<?php esc_attr_e( 'Cancel', 'foogallery' ); ?>"><?php esc_html_e( 'Cancel', 'foogallery' ); ?></a> |
| 233 | </div> |
| 234 | <div class="media-toolbar-primary"> |
| 235 | <a href="#" class="foogallery-modal-insert button media-button button-large button-primary media-button-insert" disabled="disabled" |
| 236 | title="<?php esc_attr_e( 'Insert Gallery', 'foogallery' ); ?>"><?php esc_html_e( 'Insert Gallery', 'foogallery' ); ?></a> |
| 237 | </div> |
| 238 | <!-- end .media-toolbar-primary --> |
| 239 | </div> |
| 240 | <!-- end .media-toolbar --> |
| 241 | </div> |
| 242 | <!-- end .media-frame-toolbar --> |
| 243 | </div> |
| 244 | <!-- end .media-frame --> |
| 245 | </div> |
| 246 | <!-- end .media-modal-content --> |
| 247 | </div> |
| 248 | <!-- end .media-modal --> |
| 249 | <div class="media-modal-backdrop"></div> |
| 250 | </div> |
| 251 | <?php |
| 252 | } |
| 253 | |
| 254 | function ajax_galleries_html() { |
| 255 | if ( ! check_ajax_referer( 'foogallery_load_galleries', 'foogallery_load_galleries', false ) ) { |
| 256 | wp_send_json_error( __( 'Invalid security token.', 'foogallery' ), 403 ); |
| 257 | } |
| 258 | |
| 259 | if ( ! current_user_can( 'edit_posts' ) && ! current_user_can( 'edit_pages' ) ) { |
| 260 | wp_send_json_error( __( 'Insufficient permissions.', 'foogallery' ), 403 ); |
| 261 | } |
| 262 | |
| 263 | echo $this->get_galleries_html_for_modal(); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- HTML generated by internal method |
| 264 | wp_die( '', '', array( 'response' => null ) ); |
| 265 | } |
| 266 | |
| 267 | function get_galleries_html_for_modal() { |
| 268 | $galleries = foogallery_get_all_galleries(); |
| 269 | |
| 270 | ob_start(); |
| 271 | |
| 272 | foreach ( $galleries as $gallery ) { |
| 273 | $img_src = foogallery_find_featured_attachment_thumbnail_src( $gallery, array( |
| 274 | 'width' => get_option( 'thumbnail_size_w' ), |
| 275 | 'height' => get_option( 'thumbnail_size_h' ), |
| 276 | 'force_use_original_thumb' => true |
| 277 | ) ); |
| 278 | $images = $gallery->image_count(); |
| 279 | $title = empty( $gallery->name ) ? |
| 280 | /* translators: %s: Value inserted at runtime. */ |
| 281 | sprintf( __( '%s #%s', 'foogallery' ), foogallery_plugin_name(), $gallery->ID ) : |
| 282 | $gallery->name; |
| 283 | /* translators: %s: gallery title. */ |
| 284 | $image_alt = sprintf( __( 'Preview of %s', 'foogallery' ), $title ); |
| 285 | ?> |
| 286 | <li class="foogallery-pile"> |
| 287 | <div class="foogallery-gallery-select" data-foogallery-id="<?php echo esc_attr( $gallery->ID ); ?>"> |
| 288 | <div style="display: table;"> |
| 289 | <div style="display: table-cell; vertical-align: middle; text-align: center;"> |
| 290 | <img src="<?php echo esc_url( $img_src ); ?>" alt="<?php echo esc_attr( $image_alt ); ?>"/> |
| 291 | <h3> |
| 292 | <?php echo esc_html( $title ); ?> |
| 293 | <span><?php echo esc_html( $images ); ?></span> |
| 294 | </h3> |
| 295 | </div> |
| 296 | </div> |
| 297 | </div> |
| 298 | </li> |
| 299 | <?php } ?> |
| 300 | <li class="foogallery-pile"> |
| 301 | <div class="foogallery-gallery-select foogallery-add-gallery"> |
| 302 | <a href="<?php echo esc_url( foogallery_admin_add_gallery_url() ); ?>" target="_blank" class="thumbnail" style="display: table;"> |
| 303 | <span></span> |
| 304 | <div class="foogallery-gallery-select-inner" > |
| 305 | <h3><?php esc_html_e( 'Add New Gallery', 'foogallery' ); ?></h3> |
| 306 | </div> |
| 307 | </a> |
| 308 | </div> |
| 309 | </li> |
| 310 | <?php |
| 311 | |
| 312 | return ob_get_clean(); |
| 313 | } |
| 314 | |
| 315 | function ajax_get_gallery_info() { |
| 316 | if ( ! check_ajax_referer( 'foogallery-timymce-nonce', 'nonce', false ) ) { |
| 317 | wp_send_json_error( __( 'Invalid security token.', 'foogallery' ) ); |
| 318 | } |
| 319 | |
| 320 | if ( ! current_user_can( 'edit_posts' ) && ! current_user_can( 'edit_pages' ) ) { |
| 321 | wp_send_json_error( __( 'Insufficient permissions.', 'foogallery' ) ); |
| 322 | } |
| 323 | |
| 324 | $id = absint( safe_get_from_request( 'foogallery_id' ) ); |
| 325 | if ( ! $id ) { |
| 326 | wp_send_json_error( __( 'Invalid gallery ID.', 'foogallery' ) ); |
| 327 | } |
| 328 | |
| 329 | $gallery = FooGallery::get_by_id( $id ); |
| 330 | if ( ! $gallery ) { |
| 331 | wp_send_json_error( __( 'Gallery not found.', 'foogallery' ) ); |
| 332 | } |
| 333 | |
| 334 | if ( ! current_user_can( 'edit_post', $gallery->ID ) ) { |
| 335 | wp_send_json_error( __( 'Insufficient permissions.', 'foogallery' ) ); |
| 336 | } |
| 337 | |
| 338 | $image_src = foogallery_find_featured_attachment_thumbnail_src( $gallery, array( |
| 339 | 'width' => get_option( 'thumbnail_size_w' ), |
| 340 | 'height' => get_option( 'thumbnail_size_h' ), |
| 341 | 'force_use_original_thumb' => true, |
| 342 | ) ); |
| 343 | |
| 344 | wp_send_json_success( array( |
| 345 | 'id' => $id, |
| 346 | 'name' => $gallery->name, |
| 347 | 'count' => $gallery->image_count(), |
| 348 | 'src' => $image_src, |
| 349 | ) ); |
| 350 | } |
| 351 | } |
| 352 | } |
| 353 |