From 4458d87503d2fd8fbd4672ce543a5f898a362148 Mon Sep 17 00:00:00 2001 From: Satoshi SHODA Date: Tue, 11 Jul 2017 23:09:25 +0900 Subject: [PATCH] v3.1.0 --- README.md | 2 +- admin/settings-network.php | 38 ++++ intuitive-custom-post-order.php | 336 ++++++++++++++++++++++++++++++-- js/hicpo.js | 33 ++++ readme.txt | 19 +- 5 files changed, 408 insertions(+), 20 deletions(-) create mode 100755 admin/settings-network.php diff --git a/README.md b/README.md index e52c395..ccf391e 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ ## Description -Intuitively, order items( Posts, Pages, and Custom Post Types, and Custom Taxonomies ) using a drag and drop sortable JavaScript. +Intuitively, order items( Posts, Pages, ,Custom Post Types, Custom Taxonomies, Sites ) using a drag and drop sortable JavaScript. Select sortable items from 'Intuitive CPT' menu of Setting menu in WordPress. diff --git a/admin/settings-network.php b/admin/settings-network.php new file mode 100755 index 0000000..5199d90 --- /dev/null +++ b/admin/settings-network.php @@ -0,0 +1,38 @@ +
+ + + +

+ + +
+ +

+ +
+ + +
+ + + + + + + + + + +
+ + + +
+ +

+ +

+ +
+ +
diff --git a/intuitive-custom-post-order.php b/intuitive-custom-post-order.php index 05a2261..0ef774a 100755 --- a/intuitive-custom-post-order.php +++ b/intuitive-custom-post-order.php @@ -2,8 +2,8 @@ /* * Plugin Name: Intuitive Custom Post Order * Plugin URI: http://hijiriworld.com/web/plugins/intuitive-custom-post-order/ - * Description: Intuitively, Order Items (Posts, Pages, and Custom Post Types and Custom Taxonomies) using a Drag and Drop Sortable JavaScript. - * Version: 3.0.8 + * Description: Intuitively, Order Items( Posts, Pages, ,Custom Post Types, Custom Taxonomies, Sites ) using a Drag and Drop Sortable JavaScript. + * Version: 3.1.0 * Author: hijiri * Author URI: http://hijiriworld.com/web/ * Text Domain: intuitive-custom-post-order @@ -18,6 +18,7 @@ define( 'HICPO_URL', plugins_url( '', __FILE__ ) ); define( 'HICPO_DIR', plugin_dir_path( __FILE__ ) ); +define( 'HICPO_VER', '3.1.0' ); /** * Uninstall hook @@ -27,19 +28,25 @@ function hicpo_uninstall() { global $wpdb; - if ( function_exists( 'is_multisite' ) && is_multisite() ) { + if ( function_exists( 'is_multisite' ) && is_multisite() ) + { $curr_blog = $wpdb->blogid; $blogids = $wpdb->get_col( "SELECT blog_id FROM $wpdb->blogs" ); foreach( $blogids as $blog_id ) { switch_to_blog( $blog_id ); - hicpo_uninstall_db(); + hicpo_uninstall_db_terms(); } switch_to_blog( $curr_blog ); + hicpo_uninstall_db_blogs(); } else { - hicpo_uninstall_db(); + hicpo_uninstall_db_terms(); } + delete_option( 'hicpo_activation' ); // old version before than 3.1.0 + delete_option( 'hicpo_ver' ); } -function hicpo_uninstall_db() + +// drop term_order COLUMN to $wpdb->terms TABLE +function hicpo_uninstall_db_terms() { global $wpdb; $result = $wpdb->query( "DESCRIBE $wpdb->terms `term_order`" ); @@ -47,7 +54,16 @@ function hicpo_uninstall_db() $query = "ALTER TABLE $wpdb->terms DROP `term_order`"; $result = $wpdb->query( $query ); } - delete_option( 'hicpo_activation' ); +} +// drop menu_order COLUMN to $wpdb->blogs TABLE +function hicpo_uninstall_db_blogs() +{ + global $wpdb; + $result = $wpdb->query( "DESCRIBE $wpdb->blogs `menu_order`" ); + if ( $result ) { + $query = "ALTER TABLE $wpdb->blogs DROP `menu_order`"; + $result = $wpdb->query( $query ); + } } /** @@ -56,17 +72,27 @@ function hicpo_uninstall_db() $hicpo = new Hicpo(); -class Hicpo +class Hicpo { + /** + * Construct + */ function __construct() { - if ( !get_option( 'hicpo_activation' ) ) $this->hicpo_activation(); + // activation + $hicpo_ver = get_option( 'hicpo_ver' ); + if ( version_compare( $hicpo_ver, HICPO_VER ) < 0 ) $this->hicpo_activation(); + // textdomain add_action( 'plugins_loaded', array( $this, 'my_plugin_load_plugin_textdomain' ) ); + // add menu add_action( 'admin_menu', array( $this, 'admin_menu') ); - add_action( 'admin_init', array( $this, 'refresh' ) ); + // admin init + if ( empty($_GET) ) { + add_action( 'admin_init', array( $this, 'refresh' ) ); + } add_action( 'admin_init', array( $this, 'update_options') ); add_action( 'admin_init', array( $this, 'load_script_css' ) ); @@ -86,23 +112,67 @@ function __construct() add_filter( 'get_terms_orderby', array( $this, 'hicpo_get_terms_orderby' ), 10, 3 ); add_filter( 'wp_get_object_terms', array( $this, 'hicpo_get_object_terms' ), 10, 3 ); add_filter( 'get_terms', array( $this, 'hicpo_get_object_terms' ), 10, 3 ); + + // reorder sites + if ( function_exists( 'is_multisite' ) && is_multisite() ) + { + add_action( 'network_admin_menu', array( $this, 'network_admin_menu' ) ); + add_action( 'admin_init', array( $this, 'update_network_options' ) ); + add_action( 'wp_ajax_update-menu-order-sites', array( $this, 'update_menu_order_sites' ) ); + + // networkadmin サイト削除時はサイト並び替え除外 + if( empty( $_SERVER['QUERY_STRING'] ) || + ( + !empty( $_SERVER['QUERY_STRING'] ) && + 'action=deleteblog' !== $_SERVER['QUERY_STRING'] && // delete + 'action=allblogs' !== $_SERVER['QUERY_STRING'] // delete all + ) + ) { + + // call from 'get_sites' + add_filter( 'sites_clauses', array( $this, 'hicpo_sites_clauses' ), 10, 1 ); + + add_action( 'admin_init', array( $this, 'refresh_network' ) ); + + // adminbar sites reorder + add_filter( 'get_blogs_of_user', array( $this, 'hicpo_get_blogs_of_user' ), 10, 3 ); + } + + // before wp v4.6.0 * wp_get_sites + add_action( 'init', array( $this, 'refresh_front_network' ) ); + } } + /** + * Method + */ function hicpo_activation() { global $wpdb; + + // add term_order COLUMN to $wpdb->terms TABLE $result = $wpdb->query( "DESCRIBE $wpdb->terms `term_order`" ); if ( !$result ) { $query = "ALTER TABLE $wpdb->terms ADD `term_order` INT( 4 ) NULL DEFAULT '0'"; $result = $wpdb->query( $query ); } - update_option( 'hicpo_activation', 1 ); + + if ( function_exists( 'is_multisite' ) && is_multisite() ) { + // add menu_order COLUMN to $wpdb->blogs TABLE + $result = $wpdb->query( "DESCRIBE $wpdb->blogs `menu_order`" ); + if ( !$result ) { + $query = "ALTER TABLE $wpdb->blogs ADD `menu_order` INT( 4 ) NULL DEFAULT '0'"; + $result = $wpdb->query( $query ); + } + } + update_option( 'hicpo_ver', HICPO_VER ); } function my_plugin_load_plugin_textdomain() { load_plugin_textdomain( 'intuitive-custom-post-order', false, basename( dirname( __FILE__ ) ).'/languages/' ); } + function admin_menu() { add_options_page( __( 'Intuitive CPO', 'intuitive-custom-post-order' ), __( 'Intuitive CPO', 'intuitive-custom-post-order' ), 'manage_options', 'hicpo-settings', array( $this,'admin_page' ) ); @@ -112,11 +182,34 @@ function admin_page() { require HICPO_DIR.'admin/settings.php'; } - + + function network_admin_menu() + { + add_submenu_page( 'settings.php', __( 'Intuitive CPO', 'hicpo' ), __( 'Intuitive CPO', 'hicpo' ), 'manage_options', 'hicpo-network-settings', array( $this, 'network_admin_page' ) ); + } + + function network_admin_page() + { + require HICPO_DIR.'admin/settings-network.php'; + } + function _check_load_script_css() { + global $pagenow, $typenow; + $active = false; + // multisite > sites + if ( + function_exists( 'is_multisite' ) + && is_multisite() + && $pagenow == 'sites.php' + && get_option( 'hicpo_network_sites' ) + ) + { + return true; + } + $objects = $this->get_hicpo_options_objects(); $tags = $this->get_hicpo_options_tags(); @@ -205,6 +298,47 @@ function refresh() } } + function refresh_network() + { + global $pagenow; + if( 'sites.php' === $pagenow && !isset( $_GET['orderby'] ) ) { + add_filter( 'query', array( $this, 'refresh_network_2' ) ); + } + } + + function refresh_network_2( $query ) + { + global $wpdb, $wp_version, $blog_id; + + /** + * after wp4.7.0 + * ブログのステータスが公開以外の際、$blog_id=1以外のoptionを取得しようとする処理のSQLを除外する + * eq.) SELECT option_name, option_value FROM wp_11_options WHERE autoload = 'yes' + */ + + // $wpdb->get_varやswitch_to_blog(1)からのget_optionでは処理が止まるため、$blog_idで判別 + if ( version_compare( $wp_version, '4.7.0' ) >= 0 ) { + if( 1 !== $blog_id ) { + return $query; + } + } + + $hicpo_network_sites = get_option( 'hicpo_network_sites' ); + if( !$hicpo_network_sites ) return $query; + + + if( false !== strpos( $query, "SELECT * FROM $wpdb->blogs WHERE site_id = '1'" ) || + false !== strpos( $query, "SQL_CALC_FOUND_ROWS blog_id FROM $wpdb->blogs WHERE site_id = 1" ) + ) { + if ( false !== strpos( $query, " LIMIT " ) ) { + $query = preg_replace( '/^(.*) LIMIT(.*)$/', '$1 ORDER BY menu_order ASC LIMIT $2', $query ); + } else { + $query .= " ORDER BY menu_order ASC"; + } + } + return $query; + } + function update_menu_order() { global $wpdb; @@ -271,6 +405,28 @@ function update_menu_order_tags() } } + function update_menu_order_sites() + { + global $wpdb; + + parse_str( $_POST['order'], $data ); + + if ( !is_array( $data ) ) return false; + + $id_arr = array(); + foreach( $data as $key => $values ) { + foreach( $values as $position => $id ) { + $id_arr[] = $id; + } + } + + foreach( $data as $key => $values ) { + foreach( $values as $position => $id ) { + $wpdb->update( $wpdb->blogs, array( 'menu_order' => $position+1 ), array( 'blog_id' => intval( $id ) ) ); + } + } + } + /** * はじめて有効化されたオブジェクトは、ディフォルトの order に従って menu_order セットする * @@ -354,6 +510,39 @@ function update_options() wp_redirect( 'admin.php?page=hicpo-settings&msg=update' ); } + function update_network_options() + { + global $wpdb; + + if ( !isset( $_POST['hicpo_network_submit' ] ) ) return false; + + check_admin_referer( 'nonce_hicpo' ); + + $hicpo_network_sites = isset( $_POST['sites'] ) ? $_POST['sites'] : 0; + + update_option( 'hicpo_network_sites', $hicpo_network_sites ); + + // Initial + $result = $wpdb->get_results( " + SELECT count(*) as cnt, max(menu_order) as max, min(menu_order) as min + FROM $wpdb->blogs + " ); + if ( $result[0]->cnt == 0 || $result[0]->cnt == $result[0]->max ) { + + } else { + $results = $wpdb->get_results( " + SELECT blog_id + FROM $wpdb->blogs + ORDER BY blog_id ASC + " ); + foreach( $results as $key => $result ) { + $wpdb->update( $wpdb->blogs, array( 'menu_order' => $key+1 ), array( 'blog_id' => $result->blog_id ) ); + } + } + + wp_redirect( 'settings.php?page=hicpo-network-settings&msg=update' ); + } + function hicpo_previous_post_where( $where ) { global $post; @@ -513,6 +702,129 @@ function taxcmp( $a, $b ) return ( $a->term_order < $b->term_order ) ? -1 : 1; } + function hicpo_sites_clauses( $pieces = array() ) + { + global $blog_id; + + if ( is_admin() ) return $pieces; + if ( 1 != $blog_id ) { + $current = $blog_id; + switch_to_blog(1); + $hicpo_network_sites = get_option( 'hicpo_network_sites' ); + switch_to_blog($current); + if ( !$hicpo_network_sites ) return $pieces; + } else { + if ( !get_option( 'hicpo_network_sites' ) ) return $pieces; + } + + global $wp_version; + if ( version_compare( $wp_version, '4.6.0' ) >= 0 ) { + // サイト並び替え指定がデフォルトの場合のみ並び替え + if( 'blog_id ASC' === $pieces['orderby'] ) { + $pieces['orderby'] = 'menu_order ASC'; + } + } + return $pieces; + } + + function hicpo_get_blogs_of_user( $blogs ) + { + global $blog_id; + if ( 1 != $blog_id ) { + $current = $blog_id; + switch_to_blog(1); + $hicpo_network_sites = get_option( 'hicpo_network_sites' ); + switch_to_blog($current); + if ( !$hicpo_network_sites ) return $blogs; + } else { + if ( !get_option( 'hicpo_network_sites' ) ) return $blogs; + } + global $wpdb, $wp_version; + + if ( version_compare( $wp_version, '4.6.0' ) >= 0 ) { + $sites = get_sites( array() ); + $sort_keys = array(); + foreach( $sites as $k => $v ) { + $sort_keys[] = $v->menu_order; + } + array_multisort( $sort_keys, SORT_ASC, $sites ); + + $blog_list = array(); + foreach( $blogs as $k => $v ) { + $blog_list[$v->userblog_id] = $v; + } + + $new = array(); + foreach( $sites as $k => $v ) { + if ( isset($v->blog_id) && + isset($blog_list[$v->blog_id]) && + is_object( $blog_list[$v->blog_id] ) ) { + $new[] = $blog_list[$v->blog_id]; + } + } + } else { + $sites = wp_get_sites( array( 'limit' => 9999 ) ); + $sort_keys = array(); + foreach( $sites as $k => $v ) { + $sort_keys[] = $v['menu_order']; + } + array_multisort( $sort_keys, SORT_ASC, $sites ); + + $blog_list = array(); + foreach( $blogs as $k => $v ) { + $blog_list[$v->userblog_id] = $v; + } + + $new = array(); + foreach( $sites as $k => $v ) { + if ( isset($v['blog_id']) && + isset($blog_list[$v['blog_id']]) && + is_object( $blog_list[$v['blog_id']] ) ) { + $new[] = $blog_list[$v['blog_id']]; + } + } + } + return $new; + } + + /* before wp v4.6.0 */ + function refresh_front_network() + { + global $wp_version; + if ( version_compare( $wp_version, '4.6.0' ) < 0 ) { + global $blog_id; + if ( 1 != $blog_id ) { + $current = $blog_id; + switch_to_blog(1); + $hicpo_network_sites = get_option( 'hicpo_network_sites' ); + switch_to_blog($current); + if ( !$hicpo_network_sites ) return; + } else { + if ( !get_option( 'hicpo_network_sites' ) ) return; + } + add_filter( 'query', array( $this, 'refresh_front_network_2' ) ); + } + } + function refresh_front_network_2( $query ) + { + global $wpdb; + if ( false !== strpos( $query, "SELECT blog_id FROM $wpdb->blogs ORDER BY blog_id ASC" ) ) { + $query = str_replace( 'ORDER BY blog_id ASC', '', $query ); + if ( false !== strpos( $query, " LIMIT " ) ) { + $query = preg_replace( '/^(.*) LIMIT(.*)$/', '$1 ORDER BY menu_order ASC LIMIT $2', $query ); + } else { + $query .= " ORDER BY menu_order ASC"; + } + } elseif ( false !== strpos( $query, "SELECT * FROM $wpdb->blogs WHERE 1=1 AND site_id IN (1)" ) ) { + if ( false !== strpos( $query, " LIMIT " ) ) { + $query = preg_replace( '/^(.*) LIMIT(.*)$/', '$1 ORDER BY menu_order ASC LIMIT $2', $query ); + } else { + $query .= " ORDER BY menu_order ASC"; + } + } + return $query; + } + function get_hicpo_options_objects() { $hicpo_options = get_option( 'hicpo_options' ) ? get_option( 'hicpo_options' ) : array(); diff --git a/js/hicpo.js b/js/hicpo.js index 0a731fb..ea3b84b 100755 --- a/js/hicpo.js +++ b/js/hicpo.js @@ -30,6 +30,39 @@ }); //$("#the-list").disableSelection(); + // sites + + // add number + var site_table_tr = $('table.sites #the-list tr'); + site_table_tr.each( function() { + var ret=null; + var url = $(this).find('td.blogname a').attr('href'); + parameters = url.split('?'); + if( parameters.length > 1 ) { + var params = parameters[1].split('&'); + var paramsArray = []; + for( var i=0; i