GMMediaTags.class.php
11 years ago
GMMediaTags.js
10 years ago
GMMediaTagsAdmin.class.php
10 years ago
options.php
9 years ago
types.php
8 years ago
GMMediaTagsAdmin.class.php
320 lines
| 1 | <?php |
| 2 | /** |
| 3 | * GMMediaTagsAdmin class |
| 4 | * |
| 5 | * @package GMMediaTags |
| 6 | * @author Giuseppe Mazzapica |
| 7 | * |
| 8 | */ |
| 9 | class GMMediaTagsAdmin { |
| 10 | |
| 11 | |
| 12 | /** |
| 13 | * Class version |
| 14 | * |
| 15 | * @since 0.1.0 |
| 16 | * |
| 17 | * @access protected |
| 18 | * |
| 19 | * @var string |
| 20 | */ |
| 21 | protected static $version = '0.1.1'; |
| 22 | |
| 23 | |
| 24 | |
| 25 | /** |
| 26 | * Registered taxonomies names |
| 27 | * |
| 28 | * @since 0.1.0 |
| 29 | * |
| 30 | * @access public |
| 31 | * |
| 32 | * @var array |
| 33 | */ |
| 34 | protected static $registered = array(); |
| 35 | |
| 36 | |
| 37 | /** |
| 38 | * Constructor. Doing nothing |
| 39 | * |
| 40 | * @since 0.1.0 |
| 41 | * |
| 42 | * @access public |
| 43 | * @return null |
| 44 | * |
| 45 | */ |
| 46 | function __construct() { |
| 47 | _doing_it_wrong( 'GMMediaTagsAdmin::__construct', 'GMMediaTagsAdmin Class is intented to be used statically.' ); |
| 48 | } |
| 49 | |
| 50 | |
| 51 | |
| 52 | /** |
| 53 | * Init the plugin backend. Run on 'admin_init' hook |
| 54 | * |
| 55 | * @since 0.1.0 |
| 56 | * |
| 57 | * @access public |
| 58 | * @return null |
| 59 | * |
| 60 | */ |
| 61 | static function init() { |
| 62 | |
| 63 | if ( ! defined('GMMEDIATAGSPATH') ) die(); |
| 64 | |
| 65 | self::$registered = get_object_taxonomies('attachment'); |
| 66 | |
| 67 | add_action( 'wp_ajax_add_media_tag_bulk_tr', array(__CLASS__, 'print_tr') ); |
| 68 | add_action( 'wp_ajax_save_media_tag_bulk', array(__CLASS__, 'save') ); |
| 69 | add_action( 'admin_notices', array(__CLASS__, 'notices') ); |
| 70 | add_action( 'admin_enqueue_scripts', array(__CLASS__, 'add_scripts') ); |
| 71 | |
| 72 | } |
| 73 | |
| 74 | |
| 75 | |
| 76 | /** |
| 77 | * Add the javascript and pass json data to it using 'wp_localize_script'. Run on 'admin_enqueue_scripts' hook |
| 78 | * |
| 79 | * @since 0.1.0 |
| 80 | * |
| 81 | * @param string $hook current admin page |
| 82 | * @access public |
| 83 | * @return null |
| 84 | * |
| 85 | */ |
| 86 | static function add_scripts( $hook ) { |
| 87 | if( $hook == 'upload.php' ) { |
| 88 | wp_enqueue_script( 'suggest' ); |
| 89 | wp_enqueue_script( 'GMMediaTags', GMMEDIATAGSURL . 'includes/GMMediaTags.js', array('jquery'), null, true); |
| 90 | $vars = array( |
| 91 | 'remove_from_edit' => __('Remove from bulk edit', 'gmmediatags'), |
| 92 | 'update_error' => __('Error on update attachments', 'gmmediatags'), |
| 93 | 'assign_terms' => __('Assign Folders', 'gmmediatags'), |
| 94 | 'ver_html' => wp_create_nonce('add_media_tag_bulk_tr'), |
| 95 | 'ver_save' => wp_create_nonce('save_media_tag_bulk') |
| 96 | ); |
| 97 | wp_localize_script( 'GMMediaTags', 'gm_mediatags_vars', $vars ); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | |
| 102 | |
| 103 | |
| 104 | /** |
| 105 | * Output json data from an ajax call |
| 106 | * |
| 107 | * @since 0.1.0 |
| 108 | * |
| 109 | * @param array $data data to output |
| 110 | * @param string $error if not empty funtion output error data |
| 111 | * @access protected |
| 112 | * @return null |
| 113 | * |
| 114 | */ |
| 115 | protected static function json_out( $data = array(), $error = "" ) { |
| 116 | header('Cache-Control: no-cache, must-revalidate'); |
| 117 | header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); |
| 118 | header('Content-type: application/json'); |
| 119 | if ( ! empty($error) || empty($data) ) $data = array('bulk_media_tag' => 'error', 'error' => $error); |
| 120 | die ( json_encode( (object)$data) ); |
| 121 | } |
| 122 | |
| 123 | |
| 124 | |
| 125 | |
| 126 | /** |
| 127 | * Bulk save on ajax the terms |
| 128 | * |
| 129 | * @since 0.1.0 |
| 130 | * |
| 131 | * @access public |
| 132 | * @return null |
| 133 | * |
| 134 | */ |
| 135 | static function save() { |
| 136 | error_reporting(0); |
| 137 | if ( ! defined('DOING_AJAX') ) die(); |
| 138 | if ( ! isset($_POST['formData']) ) self::json_out(false, 'formData_error'); |
| 139 | parse_str ( $_POST['formData'], $form_data ); |
| 140 | |
| 141 | $referer = isset($form_data['_wp_http_referer']) ? $form_data['_wp_http_referer'] : null; |
| 142 | $nonce = isset($_POST['media_tag_ver']) ? $_POST['media_tag_ver'] : null; |
| 143 | $screen = isset($form_data['screen']) ? $form_data['screen'] : null; |
| 144 | if ( empty($nonce) || empty($referer) || empty($screen) ) self::json_out(false, 'security error 10'); |
| 145 | if ( ! wp_verify_nonce($_POST['media_tag_ver'], 'save_media_tag_bulk') ) self::json_out(false, 'security error 20'); |
| 146 | $from = parse_url( $referer ); |
| 147 | $admin_url = admin_url('upload.php'); |
| 148 | if ($_SERVER['HTTPS']) { |
| 149 | $http = 'https://'; |
| 150 | } else { |
| 151 | $http = 'http://'; |
| 152 | } |
| 153 | if ( ! $from ) self::json_out(false, 'security error 30'); |
| 154 | if ( ! isset($from['path']) ) self::json_out(false, 'security error 40'); |
| 155 | if ( $admin_url != $http.$_SERVER['HTTP_HOST'] . $from['path'] ) self::json_out(false, 'security error 50' ); |
| 156 | if ( $screen != 'upload' ) self::json_out(false, 'security error 60'); |
| 157 | |
| 158 | $orig_qv = array(); |
| 159 | if ( isset($from['query']) ) parse_str($from['query'], $orig_qv); |
| 160 | $taxonomies = isset($form_data['tax_input']) ? $form_data['tax_input'] : array(); |
| 161 | $clean_taxonomies = isset($form_data['clean_tax']) ? $form_data['clean_tax'] : array(); |
| 162 | $toclean = array_keys($clean_taxonomies); |
| 163 | $attachments = isset($_POST['attachments']) ? $_POST['attachments'] : array(); |
| 164 | |
| 165 | if ( ( empty($taxonomies) && empty($clean_taxonomies) ) || empty($attachments) ) self::json_out( array( 'bulk_media_tag' => 'none' ) ); |
| 166 | $errors = 0; |
| 167 | $done = null; |
| 168 | $tax_objs = array(); |
| 169 | foreach( $taxonomies as $taxonomy => $terms ) { |
| 170 | if ( empty($terms) || in_array($taxonomy, $toclean) ) continue; |
| 171 | $tax_obj = get_taxonomy($taxonomy); |
| 172 | $tax_objs[$taxonomy] = $tax_obj; |
| 173 | if ( ! current_user_can( $tax_obj->cap->assign_terms ) ) continue; |
| 174 | $done = 1; |
| 175 | foreach( $attachments as $attachment ) { |
| 176 | if ( ! is_array( wp_set_post_terms( $attachment, $terms, $taxonomy, true ) ) ) $errors++; |
| 177 | } |
| 178 | } |
| 179 | if ( ! empty($clean_taxonomies) ) { foreach ($clean_taxonomies as $taxonomy => $val ) { |
| 180 | if ( $val == 1 ) { |
| 181 | $tax_obj = isset($tax_objs[$taxonomy]) ? $tax_objs[$taxonomy] : get_taxonomy($taxonomy); |
| 182 | if ( ! current_user_can( $tax_obj->cap->delete_terms ) ) continue; |
| 183 | $done = 1; |
| 184 | foreach( $attachments as $attachment ) { |
| 185 | if ( ! is_array( wp_set_post_terms( $attachment, array(), $taxonomy, false ) ) ) $errors++; |
| 186 | } |
| 187 | } |
| 188 | } } |
| 189 | if ( is_null($done) ) self::json_out( array( 'bulk_media_tag' => 'none' ) ); |
| 190 | $location = add_query_arg( array( 'bulk_media_tag' => 'updated' ), $admin_url ); |
| 191 | if ( ! empty($orig_qv) ) $location = add_query_arg( $orig_qv, $location ); |
| 192 | $data = array( 'location' => $location, 'bulk_media_tag' => 'updated'); |
| 193 | self::json_out( $data ); |
| 194 | } |
| 195 | |
| 196 | |
| 197 | |
| 198 | |
| 199 | /** |
| 200 | * Print admin notices after update |
| 201 | * |
| 202 | * @since 0.1.0 |
| 203 | * |
| 204 | * @access public |
| 205 | * @return null |
| 206 | * |
| 207 | */ |
| 208 | static function notices() { |
| 209 | global $pagenow; |
| 210 | if( $pagenow == 'upload.php' && isset($_GET['bulk_media_tag']) && ! empty($_GET['bulk_media_tag']) ) { |
| 211 | $result = $_GET['bulk_media_tag']; |
| 212 | if ( $result == 'updated' ) { |
| 213 | echo '<div class="updated"><p>' . __( 'Attachments updated successfully.', 'gmmediatags') . '</p></div>'; |
| 214 | } elseif ( $result == 'none' ) { |
| 215 | echo '<div class="updated"><p>' . __( 'Nothing to update: no media or no terms selected.', 'gmmediatags') . '</p></div>'; |
| 216 | } else { |
| 217 | echo '<div class="error"><p>' . __( 'Error on update attachments.', 'gmmediatags') . '</p></div>'; |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | } |
| 222 | |
| 223 | |
| 224 | |
| 225 | |
| 226 | /** |
| 227 | * Output the html markup for the edit ui on ajax call |
| 228 | * |
| 229 | * @since 0.1.0 |
| 230 | * |
| 231 | * @access public |
| 232 | * @return null |
| 233 | * |
| 234 | */ |
| 235 | static function print_tr() { |
| 236 | if ( ! defined('DOING_AJAX') ) die(); |
| 237 | if ( empty( self::$registered ) ) die(); |
| 238 | if ( ! isset($_POST['media_tag_ver']) || ! wp_verify_nonce($_POST['media_tag_ver'], 'add_media_tag_bulk_tr')) die(); |
| 239 | error_reporting(0); |
| 240 | $colspan = isset( $_POST['colspan'] ) && intval($_POST['colspan']) ? $_POST['colspan'] : 8; |
| 241 | $hierarchical_taxonomies = array(); |
| 242 | $flat_taxonomies = array(); |
| 243 | foreach ( self::$registered as $taxonomy_name ) { |
| 244 | $taxonomy = get_taxonomy( $taxonomy_name ); |
| 245 | $taxonomies_obj[$taxonomy_name] = $taxonomy; |
| 246 | if ( ! $taxonomy->show_ui ) continue; |
| 247 | if ( $taxonomy->hierarchical ) |
| 248 | $hierarchical_taxonomies[] = $taxonomy; |
| 249 | else |
| 250 | $flat_taxonomies[] = $taxonomy; |
| 251 | } |
| 252 | do_action('gm_mediatags_pre_ui_print'); |
| 253 | ?> |
| 254 | <tr id="bulk-edit" class="inline-edit-row inline-edit-row-media inline-edit-media bulk-edit-row bulk-edit-row-media bulk-edit-media inline-editor" style="display: table-row;"> |
| 255 | <td colspan="<?php echo $colspan ?>" class="colspanchange"> |
| 256 | <fieldset class="inline-edit-col-left"> |
| 257 | <div class="inline-edit-col"> |
| 258 | <h4><?php _e( 'Bulk Edit', 'gmmediatags' ); ?></h4> |
| 259 | <div id="bulk-title-div"> |
| 260 | <div id="bulk-titles"> |
| 261 | <?php |
| 262 | if ( isset($_POST['media']) && ! empty($_POST['media']) ) { foreach ( $_POST['media'] as $media ) { |
| 263 | $title = get_the_title($media); |
| 264 | ?> |
| 265 | <div id="ttle<?php echo $media; ?>"> |
| 266 | <a id="_<?php echo $media; ?>" class="ntdelbutton" title="<?php _e('Remove From Bulk Edit', 'gmmediatags'); ?>">X</a><?php echo $title; ?> |
| 267 | </div> |
| 268 | <?php } } ?> |
| 269 | </div> |
| 270 | </div> |
| 271 | </div> |
| 272 | </fieldset> |
| 273 | |
| 274 | <fieldset class="inline-edit-col-center" style="width:<?php echo empty($flat_taxonomies) ? '64' : '32' ?>%; float:right;"><div class="inline-edit-col"> |
| 275 | <?php foreach ( $hierarchical_taxonomies as $taxonomy ) : ?> |
| 276 | <span class="title inline-edit-categories-label"><?php echo esc_html( $taxonomy->labels->name ) ?></span> |
| 277 | <?php if ( current_user_can( $taxonomy->cap->assign_terms ) ) { ?> |
| 278 | <ul class="cat-checklist <?php echo esc_attr( $taxonomy->name )?>-checklist"> |
| 279 | <?php wp_terms_checklist( null, array( 'taxonomy' => $taxonomy->name ) ); ?> |
| 280 | </ul> |
| 281 | <?php } ?> |
| 282 | <?php endforeach;?> |
| 283 | </div></fieldset> |
| 284 | |
| 285 | <fieldset class="inline-edit-col-right" style="width:<?php echo empty($hierarchical_taxonomies) ? '64' : '32' ?>%; float:right;"><div class="inline-edit-col"> |
| 286 | <?php foreach($flat_taxonomies as $taxonomy ) : if ( current_user_can( $taxonomy->cap->assign_terms ) ) : ?> |
| 287 | <label class="inline-edit-tags"> |
| 288 | <span class="title"><?php echo esc_html( $taxonomy->labels->name ) ?></span> |
| 289 | <textarea cols="22" rows="1" name="tax_input[<?php echo esc_attr( $taxonomy->name )?>]" class="tax_input_<?php echo esc_attr( $taxonomy->name )?>"></textarea> |
| 290 | </label> |
| 291 | <?php endif; endforeach;?> |
| 292 | </div></fieldset> |
| 293 | |
| 294 | <fieldset class="inline-edit-col-full" style="width:100%; float:left;"><div class="inline-edit-col"> |
| 295 | <h4><?php _e('Remove all terms from: ', 'gmmediatags'); ?></h4> |
| 296 | <?php |
| 297 | foreach ( self::$registered as $taxonomy_name ) { |
| 298 | printf('<label style="float:left; margin-left:6px"><input type="checkbox" name="clean_tax[%s]" value="1"> %s</label>', esc_attr($taxonomy_name), $taxonomies_obj[$taxonomy_name]->labels->name ); |
| 299 | } |
| 300 | ?> |
| 301 | </div></fieldset> |
| 302 | |
| 303 | <p class="submit inline-assign_media_tag"> |
| 304 | <input name="bulk_assign_media_tag" id="bulk_assign_media_tag" class="button button-primary alignright" value="<?php _e('Update'); ?>" accesskey="s" type="submit"> |
| 305 | <a accesskey="c" class="button-secondary cancel alignright" style="margin-right:20px;"><?php _e('Cancel'); ?></a> |
| 306 | <input name="media_view" value="list" type="hidden"> |
| 307 | <input name="screen" value="upload" type="hidden"> |
| 308 | <br class="clear"> |
| 309 | </p> |
| 310 | </td> |
| 311 | </tr> |
| 312 | <?php |
| 313 | do_action('gm_mediatags_ui_printed'); |
| 314 | die(); |
| 315 | } |
| 316 | |
| 317 | |
| 318 | |
| 319 | } |
| 320 |