Changeset 2358898
- Timestamp:
- 08/12/2020 01:13:15 PM (6 years ago)
- Location:
- taxonomies-sortable/tags/1.0.1/php
- Files:
-
- 2 edited
-
class-taxonomies-sortable-admin.php (modified) (4 diffs)
-
class-taxonomies-sortable.php (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
-
taxonomies-sortable/tags/1.0.1/php/class-taxonomies-sortable-admin.php
r1896855 r2358898 1 1 <?php 2 2 /** 3 * Plugin baseclass.3 * Plugin administration class. 4 4 * 5 5 * @package taxonomies-sortable 6 6 * @author Enrico Sorcinelli 7 7 */ 8 9 namespace Taxonomies_Sortable; 8 10 9 11 // Check running WordPress instance. … … 12 14 exit(); 13 15 } 14 15 16 /** 16 * Class Taxonomies_Sortable. 17 * Admin interface class. 18 * 19 * @package taxonomies-sortable 20 * @author Enrico Sorcinelli 17 21 */ 18 class Taxonomies_Sortable{19 20 /** 21 * Plugin constrctor arguments.22 class Admin { 23 24 /** 25 * Admin pages name. 22 26 * 23 27 * @var array 24 28 */ 25 private $settings = array(); 29 private $admin_pages = array(); 30 31 /** 32 * Prefix. 33 * 34 * @var string 35 */ 36 private $prefix; 26 37 27 38 /** … … 33 44 34 45 /** 35 * Prefix used for options and postmeta fields, DOM IDs and DB tables. 36 * 37 * @var string 38 */ 39 private static $prefix = 'taxonomies_sortable_plugin_'; 40 41 /** 42 * Singleton instance. 43 * 44 * @var object Instance of this class. 45 */ 46 public static $instance; 47 48 /** 49 * Plugin class constructor. 46 * Construct the plugin. 50 47 * 51 48 * @param array $args { 52 49 * Array of arguments for constructor. 53 50 * 54 * @type boolean $debug Default `false`. 51 * @type string $prefix 52 * @type boolean $debug Default `false`. 55 53 * } 56 54 * … … 59 57 public function __construct( $args = array() ) { 60 58 61 $this->settings = wp_parse_args( $args, array( 62 'debug' => false, 63 ), $args ); 64 65 $this->plugin_options = $this->getPluginOptions(); 66 67 // Load plugin text domain. 68 load_plugin_textdomain( 'taxonomies-sortable', false, dirname( plugin_basename( __FILE__ ) ) . '/../languages/' ); 69 70 // Check and load needed components. 71 $this->requireComponents(); 72 73 // Create plugin admin object. 74 if ( is_admin() ) { 75 $this->_admin = new \Taxonomies_Sortable\Admin( array( 76 'prefix' => self::$prefix, 77 'plugin_options' => $this->plugin_options, 78 'debug' => $this->settings['debug'], 79 )); 80 } 81 82 // Add sort filter for sortable taxonomies. 83 add_filter( 'wp_get_object_terms_args', array( $this, 'wpGgetOobjectTermsArgsFilter' ), 10, 3 ); 84 85 // Make taxonomies sortable. 86 add_filter( 'register_taxonomy_args', array( $this, 'makeTaxonomiesSortable' ), 10, 2 ); 87 } 88 89 /** 90 * Get the singleton instance of this class. 91 * 92 * @param array $args Constructor arguments. 93 * 94 * @return object 95 */ 96 public static function get_instance( $args = array() ) { 97 if ( ! ( self::$instance instanceof self ) ) { 98 self::$instance = new self( $args ); 99 } 100 return self::$instance; 101 } 102 103 /** 104 * This function will include core files before the theme's functions.php 105 * file has been executed. 106 * 107 * @return void 108 */ 109 public function requireComponents() { 110 111 // Plugin classes. 112 if ( ! class_exists( 'Plugin_Utils' ) ) { 113 require_once TAXONOMIES_SORTABLE_PLUGIN_BASEDIR . '/php/class-plugin-utils.php'; 114 } 115 if ( is_admin() ) { 116 require_once TAXONOMIES_SORTABLE_PLUGIN_BASEDIR . '/php/class-taxonomies-sortable-admin.php'; 117 } 118 } 119 120 /** 121 * Filter adding sortable arguments when registering taxonomy. 122 * 123 * @param array $args Taxonomy registration arguments. 124 * @param string $taxonomy Taxonomy name. 59 // Set object property. 60 $this->debug = isset( $args['debug'] ) ? $args['debug'] : false; 61 foreach ( array( 'prefix', 'plugin_options' ) as $property ) { 62 $this->$property = $args[ $property ]; 63 } 64 65 // This plugin only runs in admin, but we need it initialized on init. 66 add_action( 'init', array( $this, 'init' ) ); 67 } 68 69 /** 70 * Initialize the plugin: setup menu, settings, add filters, actions, 71 * scripts, styles and so on. 72 * 73 * @return void 74 */ 75 public function init() { 76 77 if ( ! is_admin() ) { 78 return; 79 } 80 81 // Menu settings. 82 add_action( 'admin_menu', array( $this, 'setupMenu' ), '10' ); 83 84 // Forms settings. 85 add_action( 'admin_init', array( $this, 'setupSettings' ), '10' ); 86 } 87 88 /** 89 * Setup admin menu. 90 * 91 * @return void 92 */ 93 public function setupMenu() { 94 95 $admin_menu_capability = is_super_admin() ? 'manage_options' : 'taxonomies_sortable_plugin_manage_options'; 96 97 /** 98 * Filter to allow display plugin settings page. 99 * 100 * @param boolean $display_setting_page Default `true`. 101 * 102 * @return boolean 103 */ 104 if ( apply_filters( 'taxonomies_sortable_admin_settings', true ) ) { 105 $this->admin_pages['general_settings'] = add_options_page( __( 'Taxonomies Sortable settings', 'taxonomies-sortable' ), __( 'Taxonomies Sortable', 'taxonomies-sortable' ), 'manage_options', $this->prefix . 'general_settings', array( $this, 'pageGeneralSettings' ) ); 106 } 107 } 108 109 /** 110 * Setup admin (register settings, enqueue ie JavaScript, CSS, add filters & 111 * actions, ...). 112 * 113 * @return void 114 */ 115 public function setupSettings() { 116 117 global $wp_version; 118 119 // Enqueue JS/CSS only for non AJAX requests. 120 if ( ! \Plugin_Utils::isAjaxRequest() ) { 121 122 // Screens where to enqueue assets. 123 $admin_pages = array_merge( array( 'post.php', 'post-new.php' ), array_values( $this->admin_pages ) ); 124 125 // Add CSS to post pages. 126 foreach ( $admin_pages as $page ) { 127 add_action( 'admin_print_styles-' . $page, array( $this, 'loadCSS' ), 10, 0 ); 128 add_action( 'admin_print_scripts-' . $page, array( $this, 'loadJavaScript' ), 10, 0 ); 129 } 130 } 131 132 // Register settings. 133 register_setting( $this->prefix . 'general-settings', $this->prefix . 'general_settings', array( $this, 'checkGeneralSettings' ) ); 134 } 135 136 /** 137 * Check general settings. 138 * 139 * @param mixed $settings Settings values. 125 140 * 126 141 * @return array 127 142 */ 128 public function makeTaxonomiesSortable( $args, $taxonomy ) { 129 130 if ( in_array( $taxonomy, $this->plugin_options['taxonomies'], true ) ) { 131 $args['sortable'] = true; 132 } 133 if ( isset( $args['sortable'] ) && true === $args['sortable'] ) { 134 $args['sort'] = true; 135 } 136 else { 137 $args['sortable'] = false; 138 } 139 140 // In order to force wp_get_object_terms() loop in wp-includes/taxonomy.php:1975. 141 $args['args']['_tax'] = $taxonomy; 142 143 return $args; 144 } 145 146 /** 147 * Filter applied to set term meta order (see https://core.trac.wordpress.org/ticket/35925). 148 * 149 * @param array $args Query arguments. 150 * @param integer $object_ids Object ID. 151 * @param array $taxonomies Taxonomies. 152 * 153 * @return array 154 */ 155 public function wpGgetOobjectTermsArgsFilter( $args, $object_ids, $taxonomies ) { 156 157 $t = get_taxonomy( $taxonomies[0] ); 158 if ( isset( $t->sortable ) && true === $t->sortable ) { 159 $args['orderby'] = 'term_order'; 160 } 161 else { 162 unset( $args['orderby'] ); 163 } 164 165 return $args; 166 } 167 168 /** 169 * Plugin uninstall hook. 170 * Delete wp_options entries only when plugin deactivated and deleted. 171 * 172 * @return void 173 */ 174 public static function pluginUninstall() { 175 $options = get_option( self::$prefix . 'general_settings', true ); 176 if ( isset( $options['remove_plugin_settings'] ) && $options['remove_plugin_settings'] ) { 177 delete_option( self::$prefix . 'general_settings' ); 178 } 179 } 180 181 /** 182 * Get plugin options settings. 183 * 184 * @return array 185 */ 186 private function getPluginOptions() { 187 $settings = wp_parse_args( 188 get_option( self::$prefix . 'general_settings', array() ), 189 array( 190 'taxonomies' => array(), 191 'remove_plugin_settings' => false, 192 ) 143 public function checkGeneralSettings( $settings ) { 144 145 return $settings; 146 } 147 148 /** 149 * Load CSS files. 150 * 151 * @return void 152 */ 153 public function loadCSS() { 154 155 global $post_type; 156 157 wp_enqueue_style( 158 $this->prefix . 'css', 159 TAXONOMIES_SORTABLE_PLUGIN_BASEURL . 'assets/css/admin.css', 160 array() 193 161 ); 194 195 /** 196 * Filter taxonomies to make sortable. 197 * 198 * @param array $taxonomies Taxonomies sortable. 199 * 200 * @return array 201 */ 202 $settings['taxonomies'] = apply_filters( 'taxonomies_sortable', $settings['taxonomies'] ); 203 204 return $settings; 162 } 163 164 /** 165 * Load JavaScript files. 166 * 167 * @return void 168 */ 169 public function loadJavaScript() { 170 171 global $post_type; 172 173 wp_enqueue_script( 174 $this->prefix . 'js', 175 TAXONOMIES_SORTABLE_PLUGIN_BASEURL . 'assets/js/admin.js', 176 array(), 177 TAXONOMIES_SORTABLE_PLUGIN_VERSION, 178 false 179 ); 180 181 // Get sortable taxonomies for current post type. 182 $tax_objs = get_object_taxonomies( $post_type, 'objects' ); 183 184 $sortable_taxonomies = array(); 185 foreach ( $tax_objs as $taxonomy ) { 186 if ( ! $taxonomy->hierarchical && isset( $taxonomy->sortable ) && $taxonomy->sortable ) { 187 $sortable_taxonomies[] = '#' . $taxonomy->name . '.tagsdiv'; 188 } 189 } 190 191 // Localization. 192 wp_localize_script( $this->prefix . 'js', $this->prefix . 'i18n', array( 193 '_plugin_url' => TAXONOMIES_SORTABLE_PLUGIN_BASEURL, 194 'msgs' => array(), 195 'taxonomies' => $sortable_taxonomies, 196 ) ); 197 } 198 199 /** 200 * General settings admin page callback. 201 * 202 * @return void 203 */ 204 public function pageGeneralSettings() { 205 206 \Plugin_Utils::includeTemplate( TAXONOMIES_SORTABLE_PLUGIN_BASEDIR . '/php/adminpages/general-settings.php', array( 207 'prefix' => $this->prefix, 208 'settings' => $this->plugin_options, 209 ) ); 205 210 } 206 211 } -
taxonomies-sortable/tags/1.0.1/php/class-taxonomies-sortable.php
r1896793 r2358898 55 55 * } 56 56 * 57 * @return object57 * @return object 58 58 */ 59 59 public function __construct( $args = array() ) { … … 103 103 /** 104 104 * This function will include core files before the theme's functions.php 105 * file has been ex cecuted.105 * file has been executed. 106 106 * 107 107 * @return void … … 174 174 public static function pluginUninstall() { 175 175 $options = get_option( self::$prefix . 'general_settings', true ); 176 if ( isset( $options[' delete'] ) && $options['delete'] ) {176 if ( isset( $options['remove_plugin_settings'] ) && $options['remove_plugin_settings'] ) { 177 177 delete_option( self::$prefix . 'general_settings' ); 178 178 }
Note: See TracChangeset
for help on using the changeset viewer.