class-admin-notice-custom-css.php
7 months ago
class-admin-notices.php
7 months ago
class-admin.php
7 months ago
class-attachment-fields.php
7 months ago
class-columns.php
7 months ago
class-demo-content.php
7 months ago
class-extensions.php
7 months ago
class-gallery-attachment-modal.php
7 months ago
class-gallery-datasources.php
7 months ago
class-gallery-editor.php
7 months ago
class-gallery-metabox-fields.php
7 months ago
class-gallery-metabox-items.php
7 months ago
class-gallery-metabox-settings-helper.php
7 months ago
class-gallery-metabox-settings.php
7 months ago
class-gallery-metabox-template.php
7 months ago
class-gallery-metaboxes.php
7 months ago
class-menu.php
7 months ago
class-pro-promotion.php
7 months ago
class-settings.php
7 months ago
class-silent-installer-skin.php
7 months ago
class-trial-mode.php
7 months ago
demo-content-galleries.php
7 months ago
demo-content-images.php
7 months ago
index.php
11 years ago
pro-features.php
7 months ago
view-features.php
7 months ago
view-help-demos.php
7 months ago
view-help-getting-started.php
7 months ago
view-help-pro.php
7 months ago
view-help.php
7 months ago
view-system-info.php
7 months ago
class-gallery-datasources.php
244 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Class to handle all interactions for Gallery datasources |
| 4 | */ |
| 5 | |
| 6 | if ( ! class_exists( 'FooGallery_Admin_Gallery_Datasources' ) ) { |
| 7 | |
| 8 | class FooGallery_Admin_Gallery_Datasources { |
| 9 | |
| 10 | /** |
| 11 | * Primary class constructor. |
| 12 | */ |
| 13 | public function __construct() { |
| 14 | //render the datasource modal |
| 15 | add_action( 'foogallery_admin_enqueue_scripts', array( $this, 'enqueue_scripts_and_styles' ) ); |
| 16 | add_action( 'admin_footer', array( $this, 'render_datasource_modal' ) ); |
| 17 | add_action( 'foogallery_gallery_metabox_items', array( $this, 'add_datasources_hidden_inputs' ) ); |
| 18 | add_action( 'foogallery_gallery_metabox_items_add', array( $this, 'add_datasources_button' ) ); |
| 19 | add_action( 'wp_ajax_foogallery_load_datasource_content', array( $this, 'ajax_load_datasource_content' ) ); |
| 20 | add_action( 'foogallery_before_save_gallery', array( $this, 'save_gallery_datasource' ), 8, 2 ); |
| 21 | add_filter( 'foogallery_preview_arguments', array( $this, 'include_datasource_in_preview' ), 10, 3 ); |
| 22 | add_filter( 'foogallery_render_template_argument_overrides', array( $this, 'override_datasource_arguments' ), 10, 2 ); |
| 23 | add_action( 'foogallery_admin_datasource_modal_content', array( $this, 'render_datasource_modal_default_content' ) ); |
| 24 | } |
| 25 | |
| 26 | public function enqueue_scripts_and_styles( $hook ) { |
| 27 | wp_enqueue_style( 'foogallery.admin.datasources', FOOGALLERY_URL . 'css/foogallery.admin.datasources.css', array(), FOOGALLERY_VERSION ); |
| 28 | wp_enqueue_script( 'foogallery.admin.datasources', FOOGALLERY_URL . 'js/foogallery.admin.datasources.js', array( 'jquery' ), FOOGALLERY_VERSION ); |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * Include the datasource arguments for previews |
| 33 | * |
| 34 | * @param $args |
| 35 | * @param $form_post |
| 36 | * @param $template |
| 37 | * @return array |
| 38 | */ |
| 39 | public function include_datasource_in_preview( $args, $form_post, $template ) { |
| 40 | if ( isset( $form_post['foogallery_datasource'] ) ) { |
| 41 | $args['datasource'] = $form_post['foogallery_datasource']; |
| 42 | } |
| 43 | if ( isset( $form_post['foogallery_datasource_value'] ) ) { |
| 44 | $args['datasource_value'] = $form_post['foogallery_datasource_value']; |
| 45 | } |
| 46 | |
| 47 | return $args; |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Allow the gallery to render using an override for the datasource |
| 52 | * @param $foogallery |
| 53 | * @param $args |
| 54 | * @return FooGallery |
| 55 | */ |
| 56 | public function override_datasource_arguments( $foogallery, $args ) { |
| 57 | if ( isset( $args['datasource'] ) ) { |
| 58 | $foogallery->datasource_name = $args['datasource']; |
| 59 | } |
| 60 | if ( isset( $args['datasource_value'] ) ) { |
| 61 | $foogallery->datasource_value = $this->get_json_datasource_value( $args['datasource_value'] ); |
| 62 | } |
| 63 | |
| 64 | return $foogallery; |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Save the datasource name and value for the gallery |
| 69 | * @param $post_id |
| 70 | * @param $_post |
| 71 | */ |
| 72 | public function save_gallery_datasource( $post_id, $_post ) { |
| 73 | //action pre-save |
| 74 | do_action( 'foogallery_before_save_gallery_datasource', $post_id ); |
| 75 | |
| 76 | //set some defaults |
| 77 | $datasource = ''; |
| 78 | $datasource_value = array(); |
| 79 | |
| 80 | if ( isset( $_POST[FOOGALLERY_META_DATASOURCE] ) ) { |
| 81 | $datasource = sanitize_file_name( $_POST[FOOGALLERY_META_DATASOURCE] ); |
| 82 | |
| 83 | update_post_meta( $post_id, FOOGALLERY_META_DATASOURCE, $datasource ); |
| 84 | |
| 85 | if ( isset( $_POST[FOOGALLERY_META_DATASOURCE_VALUE] ) ) { |
| 86 | $datasource_value = $this->get_json_datasource_value( $_POST[FOOGALLERY_META_DATASOURCE_VALUE] ); |
| 87 | |
| 88 | if ( !empty( $datasource_value ) ) { |
| 89 | update_post_meta( $post_id, FOOGALLERY_META_DATASOURCE_VALUE, $datasource_value ); |
| 90 | } else { |
| 91 | delete_post_meta( $post_id, FOOGALLERY_META_DATASOURCE_VALUE ); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | } else { |
| 96 | delete_post_meta( $post_id, FOOGALLERY_META_DATASOURCE ); |
| 97 | } |
| 98 | |
| 99 | //action for post-save |
| 100 | do_action( 'foogallery_after_save_gallery_datasource', $post_id, $datasource, $datasource_value ); |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Safely returns an array from the json string |
| 105 | * @param $datasource_value_string |
| 106 | * @return mixed |
| 107 | */ |
| 108 | public function get_json_datasource_value( $datasource_value_string ) { |
| 109 | $datasource_value = array(); |
| 110 | |
| 111 | //check if the value is JSON and convert to object if needed |
| 112 | if ( is_string($datasource_value_string) && is_array( json_decode( stripslashes( $datasource_value_string ), true ) ) ) { |
| 113 | $datasource_value = json_decode( stripslashes( $datasource_value_string ), true ); |
| 114 | } |
| 115 | return $datasource_value; |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Outputs the modal content for the specific datasource |
| 120 | */ |
| 121 | public function ajax_load_datasource_content() { |
| 122 | $nonce = safe_get_from_request( 'nonce' ); |
| 123 | $datasource = sanitize_file_name( safe_get_from_request( 'datasource' ) ); |
| 124 | $datasource_value = $this->get_json_datasource_value( safe_get_from_request( 'datasource_value' ) ); |
| 125 | $foogallery_id = intval( safe_get_from_request( 'foogallery_id' ) ); |
| 126 | |
| 127 | if ( wp_verify_nonce( $nonce, 'foogallery-datasource-content' ) ) { |
| 128 | do_action( 'foogallery-datasource-modal-content_'. $datasource, $foogallery_id, $datasource_value ); |
| 129 | } |
| 130 | |
| 131 | die(); |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Adds the datasource hidden inputs to the page |
| 136 | * @param FooGallery $gallery |
| 137 | */ |
| 138 | public function add_datasources_hidden_inputs( $gallery ) { |
| 139 | $datasources = foogallery_gallery_datasources(); |
| 140 | if ( count( $datasources ) > 1 ) { |
| 141 | $datasource_value = get_post_meta( $gallery->ID, FOOGALLERY_META_DATASOURCE_VALUE, true ); |
| 142 | if ( is_array( $datasource_value ) ) { |
| 143 | $datasource_value = json_encode( $datasource_value ); |
| 144 | } ?> |
| 145 | <input type="hidden" data-foogallery-preview="include" name="<?php echo esc_attr( FOOGALLERY_META_DATASOURCE ); ?>" value="<?php echo esc_attr( $gallery->datasource_name ); ?>" id="<?php echo esc_attr( FOOGALLERY_META_DATASOURCE ); ?>" /> |
| 146 | <input type="hidden" data-foogallery-preview="include" value="<?php echo esc_attr( $datasource_value ); ?>" name="<?php echo esc_attr( FOOGALLERY_META_DATASOURCE_VALUE ); ?>" id="<?php echo esc_attr( FOOGALLERY_META_DATASOURCE_VALUE ); ?>" /> |
| 147 | <?php } |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Add the datasources button to the items metabox |
| 152 | */ |
| 153 | public function add_datasources_button() { |
| 154 | $datasources = foogallery_gallery_datasources(); |
| 155 | //we only want to show the datasources button if there are more than 1 datasources |
| 156 | if ( count( $datasources ) > 1 ) { ?> |
| 157 | <button type="button" class="button button-secondary button-hero gallery_datasources_button"> |
| 158 | <span class="dashicons dashicons-format-gallery"></span> |
| 159 | <span class="foogallery-add-button-label"><?php esc_html_e( 'Add From Another Source', 'foogallery' ); ?></span> |
| 160 | </button> |
| 161 | <?php } |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * Renders the datasource modal for use on the gallery edit page |
| 166 | */ |
| 167 | public function render_datasource_modal() { |
| 168 | |
| 169 | global $post; |
| 170 | |
| 171 | //check if the gallery edit page is being shown |
| 172 | $screen = get_current_screen(); |
| 173 | if ( 'foogallery' !== $screen->id ) { |
| 174 | return; |
| 175 | } |
| 176 | |
| 177 | $datasources = foogallery_gallery_datasources(); |
| 178 | |
| 179 | ?> |
| 180 | <?php wp_nonce_field('foogallery_load_galleries', 'foogallery_load_galleries', false); ?> |
| 181 | <div class="foogallery-datasources-modal-wrapper" data-foogalleryid="<?php echo esc_attr( $post->ID ); ?>" data-nonce="<?php echo esc_attr( wp_create_nonce( 'foogallery-datasource-content' ) ); ?>" style="display: none;"> |
| 182 | <div class="media-modal wp-core-ui"> |
| 183 | <button type="button" class="media-modal-close"> |
| 184 | <span class="media-modal-icon"><span class="screen-reader-text">Close media panel</span></span> |
| 185 | </button> |
| 186 | <div class="media-modal-content"> |
| 187 | <div class="media-frame wp-core-ui"> |
| 188 | <div class="foogallery-datasource-modal-title"> |
| 189 | <h1> |
| 190 | <?php esc_html_e('Add To Gallery From Another Source', 'foogallery'); ?> |
| 191 | <a class="foogallery-datasource-modal-reload button" href="#" style="display: none; margin-top: -4px;"><span style="padding-top: 3px;" class="dashicons dashicons-update"></span> <?php esc_html_e('Reload', 'foogallery'); ?></a> |
| 192 | </h1> |
| 193 | </div> |
| 194 | <div class="foogallery-datasource-modal-sidebar"> |
| 195 | <div class="foogallery-datasource-modal-sidebar-menu"> |
| 196 | <?php foreach ( $datasources as $key=>$datasource ) { |
| 197 | if ( $datasource['public'] ) { ?> |
| 198 | <a href="#" class="media-menu-item foogallery-datasource-modal-selector" data-datasource="<?php echo esc_attr( $key ); ?>"><?php echo esc_html( $datasource['menu'] ); ?></a> |
| 199 | <?php } } ?> |
| 200 | </div> |
| 201 | </div> |
| 202 | <div class="foogallery-datasource-modal-container"> |
| 203 | <div class="foogallery-datasource-modal-container-inner"> |
| 204 | <?php do_action( 'foogallery_admin_datasource_modal_content' ); ?> |
| 205 | </div> |
| 206 | <?php foreach ( $datasources as $key=>$datasource ) { |
| 207 | if ( $datasource['public'] ) { ?> |
| 208 | <div class="foogallery-datasource-modal-container-inner <?php echo esc_attr( $key ); ?> not-loaded"> |
| 209 | <div class="spinner"></div> |
| 210 | </div> |
| 211 | <?php } } ?> |
| 212 | </div> |
| 213 | <div class="foogallery-datasource-modal-toolbar"> |
| 214 | <div class="foogallery-datasource-modal-toolbar-inner"> |
| 215 | <div class="media-toolbar-secondary"> |
| 216 | <a href="#" |
| 217 | class="foogallery-datasource-modal-cancel button media-button button-large button-secondary media-button-insert" |
| 218 | title="<?php esc_attr_e('Cancel', 'foogallery'); ?>"><?php esc_html_e('Cancel', 'foogallery'); ?></a> |
| 219 | </div> |
| 220 | <div class="media-toolbar-primary"> |
| 221 | <a href="#" |
| 222 | class="foogallery-datasource-modal-insert button media-button button-large button-primary media-button-insert" |
| 223 | disabled="disabled" |
| 224 | title="<?php esc_attr_e('OK', 'foogallery'); ?>"><?php esc_html_e('OK', 'foogallery'); ?></a> |
| 225 | </div> |
| 226 | </div> |
| 227 | </div> |
| 228 | </div> |
| 229 | </div> |
| 230 | </div> |
| 231 | <div class="media-modal-backdrop"></div> |
| 232 | </div> |
| 233 | <?php |
| 234 | } |
| 235 | |
| 236 | function render_datasource_modal_default_content() { |
| 237 | ?> |
| 238 | <?php esc_html_e('Select a source on the left to get started.', 'foogallery'); ?> |
| 239 | <div style="height: 200px;background-repeat: no-repeat;background-size: 50%;width: 200px;background-image: url("data:image/svg+xml;base64,PHN2ZyBoZWln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