PageRenderTime 56ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/media-library-assistant/includes/class-mla-objects.php

https://bitbucket.org/kenaku/karate
PHP | 321 lines | 178 code | 31 blank | 112 comment | 20 complexity | 09841ea0ea76d4b27b49e78392b12b51 MD5 | raw file
  1. <?php
  2. /**
  3. * Media Library Assistant Custom Taxonomy and Widget objects
  4. *
  5. * @package Media Library Assistant
  6. * @since 0.1
  7. */
  8. /**
  9. * Class MLA (Media Library Assistant) Objects defines and manages custom taxonomies for Attachment Categories and Tags
  10. *
  11. * @package Media Library Assistant
  12. * @since 0.20
  13. */
  14. class MLAObjects {
  15. /**
  16. * Initialization function, similar to __construct()
  17. *
  18. * @since 0.20
  19. *
  20. * @return void
  21. */
  22. public static function initialize() {
  23. self::_build_taxonomies();
  24. }
  25. /**
  26. * Registers Attachment Categories and Attachment Tags custom taxonomies, adds taxonomy-related filters
  27. *
  28. * @since 0.1
  29. *
  30. * @return void
  31. */
  32. private static function _build_taxonomies( ) {
  33. if ( MLAOptions::mla_taxonomy_support('attachment_category') ) {
  34. $labels = array(
  35. 'name' => _x( 'Att. Categories', 'taxonomy_name_plural', 'media-library-assistant' ),
  36. 'singular_name' => _x( 'Att. Category', 'taxonomy_name_singular', 'media-library-assistant' ),
  37. 'search_items' => __( 'Search Att. Categories', 'media-library-assistant' ),
  38. 'all_items' => __( 'All Att. Categories', 'media-library-assistant' ),
  39. 'parent_item' => __( 'Parent Att. Category', 'media-library-assistant' ),
  40. 'parent_item_colon' => __( 'Parent Att. Category', 'media-library-assistant' ) . ':',
  41. 'edit_item' => __( 'Edit Att. Category', 'media-library-assistant' ),
  42. 'update_item' => __( 'Update Att. Category', 'media-library-assistant' ),
  43. 'add_new_item' => __( 'Add New Att. Category', 'media-library-assistant' ),
  44. 'new_item_name' => __( 'New Att. Category Name', 'media-library-assistant' ),
  45. 'menu_name' => __( 'Att. Category', 'media-library-assistant' )
  46. );
  47. register_taxonomy(
  48. 'attachment_category',
  49. array( 'attachment' ),
  50. array(
  51. 'hierarchical' => true,
  52. 'labels' => $labels,
  53. 'show_ui' => true,
  54. 'query_var' => true,
  55. 'rewrite' => true
  56. )
  57. );
  58. }
  59. if ( MLAOptions::mla_taxonomy_support('attachment_tag') ) {
  60. $labels = array(
  61. 'name' => _x( 'Att. Tags', 'taxonomy_name_plural', 'media-library-assistant' ),
  62. 'singular_name' => _x( 'Att. Tag', 'taxonomy_name_singular', 'media-library-assistant' ),
  63. 'search_items' => __( 'Search Att. Tags', 'media-library-assistant' ),
  64. 'all_items' => __( 'All Att. Tags', 'media-library-assistant' ),
  65. 'parent_item' => __( 'Parent Att. Tag', 'media-library-assistant' ),
  66. 'parent_item_colon' => __( 'Parent Att. Tag', 'media-library-assistant' ) . ':',
  67. 'edit_item' => __( 'Edit Att. Tag', 'media-library-assistant' ),
  68. 'update_item' => __( 'Update Att. Tag', 'media-library-assistant' ),
  69. 'add_new_item' => __( 'Add New Att. Tag', 'media-library-assistant' ),
  70. 'new_item_name' => __( 'New Att. Tag Name', 'media-library-assistant' ),
  71. 'menu_name' => __( 'Att. Tag', 'media-library-assistant' )
  72. );
  73. register_taxonomy(
  74. 'attachment_tag',
  75. array( 'attachment' ),
  76. array(
  77. 'hierarchical' => false,
  78. 'labels' => $labels,
  79. 'show_ui' => true,
  80. 'update_count_callback' => '_update_post_term_count',
  81. 'query_var' => true,
  82. 'rewrite' => true
  83. )
  84. );
  85. }
  86. $taxonomies = get_taxonomies( array ( 'show_ui' => true ), 'names' );
  87. foreach ( $taxonomies as $tax_name ) {
  88. if ( MLAOptions::mla_taxonomy_support( $tax_name ) ) {
  89. register_taxonomy_for_object_type( $tax_name, 'attachment');
  90. if ( 'checked' == MLAOptions::mla_get_option( 'attachments_column' ) ) {
  91. add_filter( "manage_edit-{$tax_name}_columns", 'MLAObjects::mla_taxonomy_get_columns_filter', 10, 1 ); // $columns
  92. add_filter( "manage_{$tax_name}_custom_column", 'MLAObjects::mla_taxonomy_column_filter', 10, 3 ); // $place_holder, $column_name, $tag->term_id
  93. } // option is checked
  94. } // taxonomy support
  95. } // foreach
  96. } // _build_taxonomies
  97. /**
  98. * WordPress Filter for edit taxonomy "Attachments" column,
  99. * which replaces the "Posts" column with an equivalent "Attachments" column.
  100. *
  101. * @since 0.30
  102. *
  103. * @param array column definitions for the edit taxonomy list table
  104. *
  105. * @return array updated column definitions for the edit taxonomy list table
  106. */
  107. public static function mla_taxonomy_get_columns_filter( $columns ) {
  108. /*
  109. * Adding or inline-editing a tag is done with AJAX, and there's no current screen object
  110. */
  111. if ( isset( $_POST['action'] ) && in_array( $_POST['action'], array( 'add-tag', 'inline-save-tax' ) ) ) {
  112. $post_type = !empty($_POST['post_type']) ? $_POST['post_type'] : 'post';
  113. } else {
  114. $screen = get_current_screen();
  115. $post_type = !empty( $screen->post_type ) ? $screen->post_type : 'post';
  116. }
  117. if ( 'attachment' == $post_type ) {
  118. if ( isset ( $columns[ 'posts' ] ) ) {
  119. unset( $columns[ 'posts' ] );
  120. }
  121. $columns[ 'attachments' ] = __( 'Attachments', 'media-library-assistant' );
  122. }
  123. return $columns;
  124. }
  125. /**
  126. * WordPress Filter for edit taxonomy "Attachments" column,
  127. * which returns a count of the attachments assigned a given term
  128. *
  129. * @since 0.30
  130. *
  131. * @param string current column value; always ''
  132. * @param array name of the column
  133. * @param array ID of the term for which the count is desired
  134. *
  135. * @return array HTML markup for the column content; number of attachments in the category
  136. * and alink to retrieve a list of them
  137. */
  138. public static function mla_taxonomy_column_filter( $place_holder, $column_name, $term_id ) {
  139. /*
  140. * Adding or inline-editing a tag is done with AJAX, and there's no current screen object
  141. */
  142. if ( isset( $_POST['action'] ) && in_array( $_POST['action'], array( 'add-tag', 'inline-save-tax' ) ) ) {
  143. $taxonomy = !empty($_POST['taxonomy']) ? $_POST['taxonomy'] : 'post_tag';
  144. } else {
  145. $screen = get_current_screen();
  146. $taxonomy = !empty( $screen->taxonomy ) ? $screen->taxonomy : 'post_tag';
  147. }
  148. $term = get_term( $term_id, $taxonomy );
  149. if ( is_wp_error( $term ) ) {
  150. /* translators: 1: taxonomy 2: error message */
  151. error_log( sprintf( _x( 'ERROR: mla_taxonomy_column_filter( "%1$s" ) - get_term failed: "%2$s"', 'error_log', 'media-library-assistant' ), $taxonomy, $term->get_error_message() ), 0 );
  152. return 0;
  153. }
  154. $request = array (
  155. 'post_type' => 'attachment',
  156. 'post_status' => 'inherit',
  157. 'orderby' => 'none',
  158. 'nopaging' => true,
  159. 'posts_per_page' => 0,
  160. 'posts_per_archive_page' => 0,
  161. 'cache_results' => false,
  162. 'update_post_meta_cache' => false,
  163. 'update_post_term_cache' => false,
  164. 'tax_query' => array(
  165. array(
  166. 'taxonomy' => $taxonomy,
  167. 'field' => 'slug',
  168. 'terms' => $term->slug,
  169. 'include_children' => false
  170. ) )
  171. );
  172. if ( MLATest::$wordpress_3point5_plus ) {
  173. $request['fields'] = 'ids';
  174. }
  175. $results = new WP_Query( $request );
  176. error_log( 'ids results = ' . var_export( $results, true ), 0 );
  177. if ( ! empty( $results->error ) ){
  178. /* translators: 1: taxonomy 2: error message */
  179. error_log( sprintf( _x( 'ERROR: mla_taxonomy_column_filter( "%1$s" ) - WP_Query failed: "%2$s"', 'error_log', 'media-library-assistant' ), $taxonomy, $results->error ), 0 );
  180. return 0;
  181. }
  182. $tax_object = get_taxonomy($taxonomy);
  183. return sprintf( '<a href="%1$s">%2$s</a>', esc_url( add_query_arg(
  184. array( 'page' => MLA::ADMIN_PAGE_SLUG, 'mla-tax' => $taxonomy, 'mla-term' => $term->slug, 'heading_suffix' => urlencode( $tax_object->label . ':' . $term->name ) ), 'upload.php' ) ), number_format_i18n( $results->post_count ) );
  185. }
  186. } //Class MLAObjects
  187. /**
  188. * Class MLA (Media Library Assistant) Text Widget defines a shortcode-enabled version of the WordPress Text widget
  189. *
  190. * @package Media Library Assistant
  191. * @since 1.60
  192. */
  193. class MLATextWidget extends WP_Widget {
  194. /**
  195. * Calls the parent constructor to set some defaults.
  196. *
  197. * @since 1.60
  198. *
  199. * @return void
  200. */
  201. function __construct() {
  202. $widget_args = array(
  203. 'classname' => 'mla_text_widget',
  204. 'description' => __( 'Shortcode(s), HTML and/or Plain Text', 'media-library-assistant' )
  205. );
  206. $control_args = array(
  207. 'width' => 400,
  208. 'height' => 350
  209. );
  210. parent::__construct( 'mla-text-widget', __( 'MLA Text', 'media-library-assistant' ), $widget_args, $control_args );
  211. }
  212. /**
  213. * Display the widget content - called from the WordPress "front end"
  214. *
  215. * @since 1.60
  216. *
  217. * @param array Widget arguments
  218. * @param array Widget definition, from the database
  219. *
  220. * @return void Echoes widget output
  221. */
  222. function widget( $args, $instance ) {
  223. $title = apply_filters( 'widget_title', empty( $instance['title'] ) ? '' : $instance['title'], $instance, $this->id_base );
  224. $text = do_shortcode( apply_filters( 'widget_text', empty( $instance['text'] ) ? '' : $instance['text'], $instance ) );
  225. echo $args['before_widget'];
  226. if ( !empty( $title ) ) { echo $args['before_title'] . $title . $args['after_title']; } ?>
  227. <div class="textwidget"><?php echo !empty( $instance['filter'] ) ? wpautop( $text ) : $text; ?></div>
  228. <?php
  229. echo $args['after_widget'];
  230. }
  231. /**
  232. * Echo the "edit widget" form on the Appearance/Widgets admin screen
  233. *
  234. * @since 1.60
  235. *
  236. * @param array Previous definition values, from the database
  237. *
  238. * @return void Echoes "edit widget" form
  239. */
  240. function form( $instance ) {
  241. $instance = wp_parse_args( (array) $instance, array( 'title' => '', 'text' => '' ) );
  242. $title = strip_tags( $instance['title'] );
  243. $text = esc_textarea( $instance['text'] );
  244. ?>
  245. <p><label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php echo __( 'Title', 'media-library-assistant' ) . ':'; ?></label>
  246. <input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" /></p>
  247. <textarea class="widefat" rows="16" cols="20" id="<?php echo $this->get_field_id( 'text' ); ?>" name="<?php echo $this->get_field_name( 'text' ); ?>"><?php echo $text; ?></textarea>
  248. <p><input id="<?php echo $this->get_field_id( 'filter' ); ?>" name="<?php echo $this->get_field_name( 'filter' ); ?>" type="checkbox" <?php checked( isset( $instance['filter'] ) ? $instance['filter'] : 0 ); ?> />&nbsp;<label for="<?php echo $this->get_field_id( 'filter' ); ?>"><?php _e( 'Automatically add paragraphs', 'media-library-assistant' ); ?></label></p>
  249. <?php
  250. }
  251. /**
  252. * Sanitize widget definition as it is saved to the database
  253. *
  254. * @since 1.60
  255. *
  256. * @param array Current definition values, to be saved in the database
  257. * @param array Previous definition values, from the database
  258. *
  259. * @return array Updated definition values to be saved in the database
  260. */
  261. function update( $new_instance, $old_instance ) {
  262. $instance = $old_instance;
  263. $instance['title'] = strip_tags( $new_instance['title'] );
  264. if ( current_user_can( 'unfiltered_html' ) ) {
  265. $instance['text'] = $new_instance['text'];
  266. } else {
  267. $instance['text'] = stripslashes( wp_filter_post_kses( addslashes( $new_instance['text'] ) ) ); // wp_filter_post_kses() expects slashed
  268. }
  269. $instance['filter'] = isset( $new_instance['filter'] );
  270. return $instance;
  271. }
  272. /**
  273. * Register the widget with WordPress
  274. *
  275. * Defined as public because it's an action.
  276. *
  277. * @since 1.60
  278. *
  279. * @return void
  280. */
  281. public static function mla_text_widget_widgets_init_action(){
  282. register_widget( 'MLATextWidget' );
  283. }
  284. } // Class MLATextWidget
  285. /*
  286. * Actions are added here, when the source file is loaded, because the MLATextWidget
  287. * object(s) are created too late to be useful.
  288. */
  289. add_action( 'widgets_init', 'MLATextWidget::mla_text_widget_widgets_init_action' );
  290. ?>