Changeset 2352101
- Timestamp:
- 08/04/2020 03:52:48 AM (6 years ago)
- Location:
- beyond-wpdb
- Files:
-
- 18 added
- 10 edited
- 1 copied
-
tags/2.0.0 (copied) (copied from beyond-wpdb/trunk)
-
tags/2.0.0/beyond-wpdb.php (modified) (2 diffs)
-
tags/2.0.0/class/admin-ajax.php (added)
-
tags/2.0.0/class/information.php (added)
-
tags/2.0.0/class/meta-query.php (modified) (1 diff)
-
tags/2.0.0/class/options.php (modified) (14 diffs)
-
tags/2.0.0/class/register.php (added)
-
tags/2.0.0/class/sql.php (modified) (25 diffs)
-
tags/2.0.0/js (added)
-
tags/2.0.0/js/beyond-wpdb.js (added)
-
tags/2.0.0/lang (added)
-
tags/2.0.0/lang/beyond-wpdb-ja.po (added)
-
tags/2.0.0/lang/beyond-wpdb.pot (added)
-
tags/2.0.0/lang/ja.mo (added)
-
tags/2.0.0/readme.txt (modified) (8 diffs)
-
trunk/beyond-wpdb.php (modified) (2 diffs)
-
trunk/class/admin-ajax.php (added)
-
trunk/class/information.php (added)
-
trunk/class/meta-query.php (modified) (1 diff)
-
trunk/class/options.php (modified) (14 diffs)
-
trunk/class/register.php (added)
-
trunk/class/sql.php (modified) (25 diffs)
-
trunk/js (added)
-
trunk/js/beyond-wpdb.js (added)
-
trunk/lang (added)
-
trunk/lang/beyond-wpdb-ja.po (added)
-
trunk/lang/beyond-wpdb.pot (added)
-
trunk/lang/ja.mo (added)
-
trunk/readme.txt (modified) (8 diffs)
Legend:
- Unmodified
- Added
- Removed
-
beyond-wpdb/tags/2.0.0/beyond-wpdb.php
r2337801 r2352101 3 3 Plugin Name: Beyond Wpdb 4 4 Plugin URI: 5 Description: Create a new table and register the meta data in a single row of JSON columns.Registration is done by mysql's trigger function.The data in WordPress is not affected.Convert sql statement, when searching for meta data.6 Version: 1.1.05 Description: Speed up your WordPress database by making use of JSON type columns in MySQL. 6 Version: 2.0.0 7 7 Author: PRESSMAN 8 8 Author URI: https://www.pressman.ne.jp/ … … 45 45 // Deifne Beyond_table name. 46 46 foreach( array_keys( BEYOND_WPDB_PRIMARYS ) as $primary ) { 47 define( beyond_wpdb_get_define_table_name( $primary ), $wpdb->prefix . $primary . 'meta_ json' );47 define( beyond_wpdb_get_define_table_name( $primary ), $wpdb->prefix . $primary . 'meta_beyond' ); 48 48 } 49 49 50 50 // Require files. 51 require_once( plugin_dir_path( __FILE__ ) . 'class/register-hook.php' ); 51 require_once( plugin_dir_path( __FILE__ ) . 'class/register.php' ); 52 require_once( plugin_dir_path( __FILE__ ) . 'class/information.php' ); 52 53 require_once( plugin_dir_path( __FILE__ ) . 'class/sql.php' ); 53 54 require_once( plugin_dir_path( __FILE__ ) . 'class/meta-query.php' ); 54 55 require_once( plugin_dir_path( __FILE__ ) . 'class/wp-orderby.php' ); 55 require_once( plugin_dir_path( __FILE__ ) . 'class/column.php' );56 56 require_once( plugin_dir_path( __FILE__ ) . 'class/options.php' ); 57 58 // Plugin activation hook. 59 register_activation_hook( __FILE__, ['Beyond_Wpdb_Register_Hook', 'activation'] ); 60 register_deactivation_hook( __FILE__, ['Beyond_Wpdb_Register_Hook', 'deactivation'] ); 61 register_uninstall_hook( __FILE__, ['Beyond_Wpdb_Register_Hook', 'uninstall'] ); 57 require_once( plugin_dir_path( __FILE__ ) . 'class/admin-ajax.php' ); -
beyond-wpdb/tags/2.0.0/class/meta-query.php
r2337801 r2352101 467 467 */ 468 468 public function virtual_column_exists( $key, $type ) { 469 $beyond_wpdb_column = new Beyond_Wpdb_Column(); 470 $beyond_wpdb_column->set_columns(); 471 $virtual_columns = $beyond_wpdb_column->get_columns(); 472 473 return in_array( $key, $virtual_columns[$type] ); 469 $beyond_wpdb_info = new Beyond_Wpdb_Information(); 470 $beyond_wpdb_info->set_columns(); 471 $virtual_columns = $beyond_wpdb_info->get_columns(); 472 $table_name = esc_sql( constant( beyond_wpdb_get_define_table_name( $type ) ) ); 473 474 return in_array( $key, $virtual_columns[$table_name] ); 474 475 } 475 476 -
beyond-wpdb/tags/2.0.0/class/options.php
r2337802 r2352101 9 9 */ 10 10 class Beyond_Wpdb_Settings_page { 11 private $ columns = array();11 private $privilege_error = False; 12 12 13 13 /** … … 16 16 public function __construct() 17 17 { 18 load_theme_textdomain( 'beyond-wpdb', plugin_dir_path( __FILE__ ) . '../lang' ); 19 add_action( 'admin_notices', array( $this, 'notice__warnig_or_error' ) ); 18 20 add_action( 'admin_menu', array( $this, 'add_beyond_wpdb_settings_page' ) ); 19 21 add_action( 'admin_init', array( $this, 'page_init' ) ); 20 $beyond_wpdb_column = new Beyond_Wpdb_Column(); 21 $beyond_wpdb_column->set_columns(); 22 $this->columns = $beyond_wpdb_column->get_columns(); 23 24 // add option beyond_wpdb_virtual_column_name 25 add_action( 'add_option_beyond_wpdb_virtual_column_name', array( $this, 'do_after_add_virtual_column' ) ,10, 2 ); 26 // add option beyond_wpdb_data_init_name 27 add_action( 'add_option_beyond_wpdb_data_init_name', array( $this, 'do_after_add_data_init' ) ,10, 2 ); 28 // update option beyond_wpdb_virtual_column_name 29 add_action( 'update_option_beyond_wpdb_virtual_column_name', array( $this, 'do_after_update_virtual_column' ) ,10, 3 ); 30 // update option beyond_wpdb_data_init_name 31 add_action( 'update_option_beyond_wpdb_data_init_name', array ( $this, 'do_after_update_data_init' ) ,10, 3 ); 32 22 23 } 24 25 /** 26 * Display warnig or error 27 */ 28 function notice__warnig_or_error() { 29 global $wpdb, $pagenow; 30 31 $current_db = $wpdb->get_var( "SELECT DATABASE()" ); 32 $current_user = explode('@', $wpdb->get_var( "SELECT USER()" )); 33 $grants = $wpdb->get_results( "SHOW GRANTS FOR '{$current_user[0]}'@'{$current_user[1]}'", ARRAY_A ); 34 $grant_for_mysql_db = False; 35 $current_privileges = array(); 36 37 foreach ( $grants as $value ) { 38 $value = array_values( $value )[0]; 39 if ( strpos( $value, "ON `mysql`." ) || strpos( $value, "root" ) ) { 40 $grant_for_mysql_db = True; 41 } 42 } 43 44 if ( $grant_for_mysql_db ) { 45 $result_privileges = $wpdb->get_results( "SELECT * FROM mysql.db WHERE Db = '{$current_db}' AND User = '{$current_user[0]}'", ARRAY_A ); 46 foreach ( $result_privileges as $val ) { 47 $keys = array_keys( $val ); 48 $values = array_values( $val ); 49 50 // If $v is Y, $keys[$idx] is assumed to be authorized. 51 foreach ( $values as $idx => $v ) { 52 if ( $v === 'Y' ) { 53 array_push( $current_privileges, $keys[$idx] ); 54 } 55 } 56 } 57 } 58 59 $privileges_error = ''; 60 $expected_privileges = array( 61 'Trigger_priv', 62 'Select_priv', 63 'Insert_priv', 64 'Update_priv', 65 'Delete_priv', 66 'Create_priv', 67 'Drop_priv' 68 ); 69 if ( count( $current_privileges ) > 0 ) { 70 foreach ( $expected_privileges as $privilege ) { 71 if ( ! in_array( $privilege, $current_privileges ) ) { 72 switch ( $privilege ) { 73 case 'Trigger_priv': 74 $privileges_error .= "<p>" . __("You do not have permission to create triggers.", "beyond-wpdb") . "</p>"; 75 break; 76 case 'Select_priv': 77 $privileges_error .= "<p>" . __("You don't have SELECT permission.", "beyond-wpdb") . "</p>"; 78 break; 79 case 'Insert_priv': 80 $privileges_error .= "<p>" . __("You don't have INSERT permission.", "beyond-wpdb") . "</p>"; 81 break; 82 case 'Update_priv': 83 $privileges_error .= "<p>" . __("You don't have UPDATE permission.", "beyond-wpdb") . "</p>"; 84 break; 85 case 'Delete_priv': 86 $privileges_error .= "<p>" . __("You don't have DELETE permission.", "beyond-wpdb") . "</p>"; 87 break; 88 case 'Create_priv': 89 $privileges_error .= "<p>" . __("You don't have CREATE permission.", "beyond-wpdb") . "</p>"; 90 break; 91 default : 92 $privileges_error .= "<p>" . __("You don't have DROP permission.", "beyond-wpdb") . "</p>"; 93 break; 94 } 95 $this->privilege_error = True; 96 } 97 } 98 } 99 100 if ( isset( $_GET['page'] ) && ! $_GET['page'] && $pagenow === 'options-general.php' ) { 101 if ( ! $grant_for_mysql_db ) { 102 $grants_message = __("You do not have permission to the mysql database.<br>We access the db table in the mysql database to find out what permissions we have to use each feature.<br>There is a possibility that each function will not be available.", "beyond-wpdb" ); 103 print " 104 <div class='notice notice-warning is-dismissible'> 105 <p> 106 {$grants_message} 107 </p> 108 </div> 109 "; 110 } 111 112 if ( $privileges_error ) { 113 $priviles_message = __( "You do not have the following permissions.Please use each function after granting permission.", "beyond-wpdb" ); 114 print " 115 <div class='notice notice-error is-dismissible'> 116 <p>{$priviles_message}</p> 117 {$privileges_error} 118 </div>"; 119 } 120 } 33 121 } 34 122 … … 52 140 public function create_beyond_wpdb_settings_page() 53 141 { 142 $disabled = $this->checkDisabled() ? '' : 'disabled'; 143 $features = __( "Speed up database loading by creating your own tables that aggregate meta information.<br>This is especially useful when you have a large number of records and for complex meta-query data calls.", "beyond-wpdb" ); 54 144 ?> 145 <style> 146 .d-block { 147 display: block; 148 } 149 .d-inline-block { 150 display: inline-block; 151 } 152 .d-none { 153 display: none; 154 } 155 .table_not_exists { 156 opacity:0.3; 157 pointer-envet:none; 158 } 159 </style> 55 160 <div class='beyond-wpdb-settings-wrap'> 56 161 <!-- title --> 57 <h1 style="margin-bottom: 30px;">Beyond WPDB Settings</h1> 58 <!-- form --> 59 <form action='options.php' method='post'> 60 <!-- Group Concat --> 61 <div> 62 <?php settings_fields( 'beyond_wpdb_group' ); ?> 63 <?php do_settings_sections( 'croup_concat_section' ); ?> 64 </div> 65 <div> 66 <?php settings_fields( 'beyond_wpdb_group' ); ?> 67 <?php do_settings_sections( 'data_init_section' ); ?> 68 </div> 69 <!-- Virtual Column Settings --> 70 <div> 71 <?php settings_fields( 'beyond_wpdb_group' ); ?> 72 <?php do_settings_sections( 'virtual_columns_section' ); ?> 73 </div> 162 <div style="margin-bottom: 30px;"> 163 <h1 style="margin-bottom: 30px;">Beyond WPDB Settings</h1> 164 <p> 165 <?php echo $features; ?> 166 </p> 167 <h2><?php echo __( "Settings", "beyond-wpdb" ) ?></h2> 168 </div> 169 <!-- data init --> 170 <div class="data_init_section"> 171 <?php settings_fields( 'beyond_wpdb_group' ); ?> 172 <?php do_settings_sections( 'data_init_section' ); ?> 74 173 75 174 <!-- Submit Button --> 76 <?php submit_button(); ?> 77 </form> 175 <p class="submit"> 176 <button id="beyond-wpdb-init-btn" class="button button-primary" <?php echo $disabled; ?>>Update</button> 177 </p> 178 </> 179 <!-- Virtual Column Settings --> 180 <div> 181 <?php settings_fields( 'beyond_wpdb_group' ); ?> 182 <?php do_settings_sections( 'virtual_columns_section' ); ?> 183 184 <!-- Submit Button --> 185 <p class="submit"> 186 <button id="beyond-wpdb-virtual-columns-btn" class="button button-primary" <?php echo $disabled; ?>>Update</button> 187 </p> 188 </div> 189 <!-- notice --> 190 <div> 191 <h2><?php echo __( "NOTICE", "beyond-wpdb" ) ?></h2> 192 <p><?php echo __( "This plugin sets 4294967295 to group_concat_max_len when it createa an original table with JSON type column.<br>You can change the number by using filter 'beyond_group_concat_max_len'.", "beyond-wpdb" ) ?></p> 193 </div> 78 194 </div> 79 195 <?php … … 85 201 public function page_init() 86 202 { 87 // group concat 88 add_settings_section( 89 'setting_section_croup_concat', 90 '<h2>Group Concat</h2>', 91 array( $this, 'print_group_concat_section_info' ), 92 'croup_concat_section' 93 ); 94 95 add_settings_field( 96 '', 97 "group_concat_max_len", 98 array( $this, 'print_croup_concat_field' ), 99 'croup_concat_section', 100 'setting_section_croup_concat' 101 ); 102 103 register_setting( 104 'beyond_wpdb_group', 105 '', 106 'sanitize_text_field' 107 ); 108 203 global $wpdb; 109 204 // data init 110 205 add_settings_section( 111 206 'setting_section_data_init', 112 '<h2> Data Init</h2>',207 '<h2>' . __( "Creating and deleting custom metatables", "beyond-wpdb" ) . '</h2>', 113 208 array( $this, 'print_data_init_section_info' ), 114 209 'data_init_section' … … 116 211 117 212 add_settings_field( 118 'data_init ',119 "Data Init",213 'data_init_postmeta_json', 214 __( "Table for postmeta", "beyond-wpdb" ) . "<br><span style='font-size: 0.7rem; color: #808080;'>Table name: postmeta_beyond</span>", 120 215 array( $this, 'print_data_init_field' ), 121 216 'data_init_section', 122 'setting_section_data_init' 217 'setting_section_data_init', 218 array( 219 'id' => "data_init_{$wpdb->prefix}postmeta_beyond", 220 'class' => "{$wpdb->prefix}postmeta_beyond" 221 ) 222 ); 223 224 add_settings_field( 225 'data_init_usermeta_json', 226 __( "Table for usermeta", "beyond-wpdb" ) . "<br><span style='font-size: 0.7rem; color: #808080;'>Table name: usermeta_beyond</span>", 227 array( $this, 'print_data_init_field' ), 228 'data_init_section', 229 'setting_section_data_init', 230 array( 231 'id' => "data_init_{$wpdb->prefix}usermeta_beyond", 232 'class' => "{$wpdb->prefix}usermeta_beyond" 233 ) 234 ); 235 236 add_settings_field( 237 'data_init_commentmeta_json', 238 __( "Table for commentmeta", "beyond-wpdb" ) . "<br><span style='font-size: 0.7rem; color: #808080;'>Table name: commentmeta_beyond</span>", 239 array( $this, 'print_data_init_field' ), 240 'data_init_section', 241 'setting_section_data_init', 242 array( 243 'id' => "data_init_{$wpdb->prefix}commentmeta_beyond", 244 'class' => "{$wpdb->prefix}commentmeta_beyond" 245 ) 123 246 ); 124 247 … … 126 249 'beyond_wpdb_group', 127 250 'beyond_wpdb_data_init_name', 128 array( $this, 'sanitize_input_columns')251 array() 129 252 ); 130 253 … … 132 255 add_settings_section( 133 256 'virtual_column_section_id', 134 '<h2> Virtual Column Settings</h2>',257 '<h2>' . __( "Virtual Column Settings", "beyond-wpdb" ) . '</h2>', 135 258 array( $this, 'print_virtual_column_section_info' ), 136 259 'virtual_columns_section' … … 139 262 add_settings_field( 140 263 'postmeta_json', // id 141 "postmeta_json", // title264 __( "Table for postmeta", "beyond-wpdb" ) . "<br><span style='font-size: 0.7rem; color: #808080;'>Table name: postmeta_beyond</span>", // title 142 265 array( $this, 'print_postmeta_json_field' ), // callback 143 266 'virtual_columns_section', // section page 144 'virtual_column_section_id' // section id 267 'virtual_column_section_id', // section id, 268 array( 269 'id' => "virtual_column_{$wpdb->prefix}postmeta_beyond", 270 'class' => "virtual_column_{$wpdb->prefix}postmeta_beyond" 271 ) 145 272 ); 146 273 147 274 add_settings_field( 148 275 'usermeta_json', 149 "usermeta_json",276 __( "Table for usermeta", "beyond-wpdb" ) . "<br><span style='font-size: 0.7rem; color: #808080;'>Table name: usermeta_beyond</span>", 150 277 array( $this, 'print_usermeta_json_field' ), 151 278 'virtual_columns_section', // section page 152 'virtual_column_section_id' // section id 279 'virtual_column_section_id', // section id 280 array( 281 'id' => "virtual_column_{$wpdb->prefix}usermeta_beyond", 282 'class' => "virtual_column_{$wpdb->prefix}usermeta_beyond" 283 ) 153 284 ); 154 285 155 286 add_settings_field( 156 287 'commentmeta_json', // id 157 "commentmeta_json", // title288 __( "Table for commentmeta", "beyond-wpdb" ) . "<br><span style='font-size: 0.7rem; color: #808080;'>Table name: commentmeta_beyond</span>", // title 158 289 array( $this, 'print_commentmeta_json_field' ), // callback 159 290 'virtual_columns_section', // section id 160 'virtual_column_section_id' 291 'virtual_column_section_id', 292 array( 293 'id' => "virtual_column_{$wpdb->prefix}commentmeta_beyond", 294 'class' => "virtual_column_{$wpdb->prefix}commentmeta_beyond" 295 ) 161 296 ); 162 297 … … 164 299 'beyond_wpdb_group', 165 300 'beyond_wpdb_virtual_column_name', 166 array( $this, 'sanitize_input_columns' ) 167 ); 168 } 169 170 /** 171 * @param $option 172 * @param $value 173 */ 174 public function do_after_add_virtual_column( $option, $value ) { 175 $this->do_virtual_column_processing( $option ); 176 } 177 178 /** 179 * @param $option 180 * @param $value 181 */ 182 public function do_after_add_data_init( $option, $value ) { 183 $this->do_data_init_processing( $option ); 184 } 185 186 /** 187 * @param $old 188 * @param $value 189 * @param $option 190 */ 191 public function do_after_update_virtual_column( $old, $value, $option ) { 192 $this->do_virtual_column_processing( $option ); 193 } 194 195 /** 196 * @param $old 197 * @param $value 198 * @param $option 199 */ 200 public function do_after_update_data_init( $old, $value, $option ) { 201 $this->do_data_init_processing( $option ); 202 } 203 204 205 /** 206 * @param $option 207 * create virtual column and delete virtual column and delete option 208 */ 209 public function do_virtual_column_processing( $option ) { 210 $this->create_virtual_column_and_index( get_option( $option ) ); 211 $this->delete_virtual_column( get_option( $option ) ); 212 delete_option( $option ); 213 } 214 215 /** 216 * @param $option 217 * data init 218 */ 219 public function do_data_init_processing( $option ) { 220 $this->data_init( get_option($option) ); 221 delete_option( $option ); 222 } 223 224 /** 225 * @param $input 226 * 227 * @return array 228 */ 229 public function sanitize_input_columns( $input ) 230 { 231 $new_input = array(); 232 if( isset( $input['postmeta_json'] ) ) { 233 $input_columns = explode( PHP_EOL, $input['postmeta_json'] ); 234 $input_columns = $this->sanitize( $input_columns ); 235 $new_input['postmeta_json'] = implode( PHP_EOL, $input_columns ); 236 } 237 238 if( isset( $input['usermeta_json'] ) ) { 239 $input_columns = explode( PHP_EOL, $input['usermeta_json'] ); 240 $input_columns = $this->sanitize( $input_columns ); 241 $new_input['usermeta_json'] = implode( PHP_EOL, $input_columns ); 242 } 243 244 if( isset( $input['commentmeta_json'] ) ) { 245 $input_columns = explode( PHP_EOL, $input['commentmeta_json'] ); 246 $input_columns = $this->sanitize( $input_columns ); 247 $new_input['commentmeta_json'] = implode( PHP_EOL, $input_columns ); 248 } 249 250 if( isset( $input['data_init'] ) ) { 251 $input_columns = explode( PHP_EOL, $input['data_init'] ); 252 $input_columns = $this->sanitize( $input_columns ); 253 $new_input['data_init'] = implode( PHP_EOL, $input_columns ); 254 } 255 256 return $new_input; 257 } 258 259 /** 260 * @param $option 261 * data init 262 */ 263 public function data_init( $option ) 264 { 265 if ( isset( $option['data_init'] ) && $option['data_init'] === '1' ) { 266 $beyond_wpdb_sql = new Beyond_Wpdb_Sql(); 267 $beyond_wpdb_sql->data_init(); 268 } 269 } 270 271 /** 272 * @param $input_columns 273 * 274 * @return string 275 */ 276 public function sanitize( $input_columns ) 277 { 278 foreach ( $input_columns as $key => $value ) { 279 $input_columns[$key] = sanitize_text_field( $value ); 280 } 281 return $input_columns; 282 } 283 284 /** 285 * @param $options 286 * create virtual column 287 */ 288 public function create_virtual_column_and_index( $options ) { 289 global $wpdb; 290 291 if ( is_array( $options ) ) { 292 foreach ( $options as $key => $option ) { 293 294 if ( 'postmeta_json' === $key ) { 295 $type = 'post'; 296 } elseif ( 'usermeta_json' === $key ) { 297 $type = 'user'; 298 } else { 299 $type = 'comment'; 300 } 301 302 $table_name = $this->get_json_table_name( $type ); 303 $exist_columns = $this->columns[$type]; 304 $option = explode( PHP_EOL, $option ); 305 306 foreach ( $option as $value ) { 307 308 $value = esc_sql( $value ); 309 310 // If $value already exists, continue 311 if ( in_array( $value, $exist_columns ) ) { 312 continue; 313 } 314 315 $json_key = '$.' . $value; 316 317 // create virtual column 318 $sql = "ALTER TABLE {$table_name} ADD {$value} VARCHAR(255) GENERATED ALWAYS AS ( JSON_UNQUOTE( JSON_EXTRACT( json, '$json_key' ) ) )"; 319 $wpdb->query( $sql ); 320 321 // create index 322 $sql = "ALTER TABLE {$table_name} ADD INDEX ({$value})"; 323 $wpdb->query( $sql ); 324 } 325 } 326 } 327 } 328 329 /** 330 * @param $options 331 * delete virtual columns 332 * @return mixed 333 */ 334 public function delete_virtual_column( $options ) { 335 global $wpdb; 336 337 if ( is_array( $options ) ) { 338 foreach ( $options as $key => $option ) { 339 if ( 'postmeta_json' === $key ) { 340 $type = 'post'; 341 } elseif ( 'usermeta_json' === $key ) { 342 $type = 'user'; 343 } else { 344 $type = 'comment'; 345 } 346 347 $table_name = $this->get_json_table_name( $type ); 348 $exist_columns = $this->columns[$type]; 349 $option = explode( PHP_EOL, $option ); 350 351 foreach ( $exist_columns as $column ) { 352 if ( ! in_array( $column, $option ) ) { 353 $sql = "ALTER TABLE {$table_name} DROP COLUMN {$column}"; 354 $wpdb->query( $sql ); 355 } 356 } 357 } 358 } 359 } 360 361 362 public function get_json_table_name( $type ) { 363 return esc_sql( constant( beyond_wpdb_get_define_table_name( $type ) ) ); 301 array() 302 ); 364 303 } 365 304 … … 369 308 public function print_virtual_column_section_info() 370 309 { 371 print '<p>372 Enter a list metakeys for which you want to create a virtual column.Each metakey should be separated by return enter key.<br>373 The virtual column is indexed and the search uses the virtual column, which makes it faster.374 </p> ';310 $info = __( "When you enter a key of meta information separated by a new line,<br>the specified meta key from the JSON type column is set as a virtual column and the index is pasted.<br>You can further speed up data calls by specifying the most commonly used meta keys for meta queries.", "beyond-wpdb" ); 311 print "<p> 312 {$info} 313 </p>"; 375 314 } 376 315 … … 378 317 * print postmeta json field 379 318 */ 380 public function print_postmeta_json_field() 381 { 382 $value = implode( PHP_EOL, $this->columns['post'] ); 383 printf( 384 '<textarea rows="3" cols="40" id="postmeta_json" name="beyond_wpdb_virtual_column_name[postmeta_json]">%s</textarea>', 385 $value 386 ); 319 public function print_postmeta_json_field( $args ) 320 { 321 $id = $args['id']; 322 $disabled = $this->checkDisabled() ? '' : 'disabled'; 323 print " 324 <textarea class='create_virtualColumns_text-area d-none' rows='3' cols='40' id='{$id}_textarea' name='{$id}' {$disabled}></textarea> 325 <span class='create_{$id} d-none' style='margin-left: 20px;'>Processing...</span> 326 <span class='data-init-input-loading' style='margin-left: 20px;'>Loading...</span> 327 "; 328 387 329 } 388 330 … … 390 332 * print usermeta json field 391 333 */ 392 public function print_usermeta_json_field() 393 { 394 $value = implode( PHP_EOL, $this->columns['user'] ); 395 printf( 396 '<textarea rows="3" cols="40" id="usermeta_json" name="beyond_wpdb_virtual_column_name[usermeta_json]">%s</textarea>', 397 $value 398 ); 334 public function print_usermeta_json_field( $args ) 335 { 336 $id = $args['id']; 337 $disabled = $this->checkDisabled() ? '' : 'disabled'; 338 print " 339 <textarea class='create_virtualColumns_text-area d-none' rows='3' cols='40' id='{$id}_textarea' name='{$id}' {$disabled}></textarea> 340 <span class='create_{$id} d-none' style='margin-left: 20px;'>Processing...</span> 341 <span class='data-init-input-loading' style='margin-left: 20px;'>Loading...</span> 342 "; 399 343 } 400 344 … … 402 346 * print commentmeta json field 403 347 */ 404 public function print_commentmeta_json_field() 405 { 406 $value = implode( PHP_EOL, $this->columns['comment'] ); 407 printf( 408 '<textarea rows="3" cols="40" id="commentmeta_json" name="beyond_wpdb_virtual_column_name[commentmeta_json]">%s</textarea>', 409 $value 410 ); 411 } 412 413 /** 414 * print group concat section info 415 */ 416 public function print_group_concat_section_info() 417 { 418 print '<p>Use GROUP_CONCAT in data init.If the json string consisting of all metas is longer than this value, an error occurs.</p>'; 419 } 420 421 /** 422 * print croup_concat field 423 */ 424 public function print_croup_concat_field() 425 { 426 global $wpdb; 427 $group_concat_max_len = $wpdb->get_results( "show variables like 'group_concat_max_len'" ); 428 printf( 429 '<p>%s</p>', 430 $group_concat_max_len[0]->Value 431 ); 348 public function print_commentmeta_json_field( $args ) 349 { 350 $id = $args['id']; 351 $disabled = $this->checkDisabled() ? '' : 'disabled'; 352 print " 353 <textarea class='create_virtualColumns_text-area d-none' rows='3' cols='40' id='{$id}_textarea' name='{$id}' {$disabled}></textarea> 354 <span class='create_{$id} d-none' style='margin-left: 20px;'>Processing...</span> 355 <span class='data-init-input-loading' style='margin-left: 20px;'>Loading...</span> 356 "; 432 357 } 433 358 … … 437 362 public function print_data_init_section_info() 438 363 { 439 print '<p>Collect all meta information and re-register it in the json table.</p>'; 440 } 441 442 /** 443 * print data_init field 444 */ 445 public function print_data_init_field() 446 { 447 print '<input type="checkbox" id="data_init" name="beyond_wpdb_data_init_name[data_init]" value="1">'; 364 $info = __( "Create and delete your own tables that aggregate the metadata. (Enabled to create, disabled to delete)<br>As long as it is enabled, all meta information will continue to be automatically registered, updated, and deleted in the JSON type columns of the relevant table.<br>When enabled, a new table will be created or initialized, which will take some time. When disabled, the table will be deleted.", "beyond-wpdb" ); 365 print $info; 366 } 367 368 /** 369 * Check to see if each feature is available 370 * @return bool 371 */ 372 public function checkDisabled() { 373 return ! $this->privilege_error; 374 } 375 376 /** 377 * @param $args 378 * print data_init field 379 */ 380 public function print_data_init_field( $args ) 381 { 382 $id = $args['id']; 383 $disabled = $this->checkDisabled() ? '' : 'disabled'; 384 print " 385 <div class='data-init-input-radio d-none'><input type='radio' id='{$id}_active' name='name_{$id}' value='1' {$disabled}>" . "<label for='{$id}_active'>" . __( "activation", "beyond-wpdb" ) . "</label>" . 386 "<input type='radio' id='{$id}_deactive' name='name_{$id}' value='0' {$disabled} style='margin-left: 25px;'>" . "<label for='{$id}_deactive'>" . __( "deactivation", "beyond-wpdb" ) . "</label>" . 387 "<span class='activate_{$id} d-none' style='margin-left: 20px;'>Processing...</span> 388 <span class='success_{$id}' style='margin-left: 20px; display: none;'>Success.</span></div><span class='data-init-input-loading' style='margin-left: 20px;'>Loading...</span> 389 "; 448 390 } 449 391 } -
beyond-wpdb/tags/2.0.0/class/sql.php
r2330366 r2352101 8 8 */ 9 9 class Beyond_Wpdb_Sql { 10 10 11 function __construct() { 11 12 global $wpdb; 12 13 13 14 foreach( array_keys( BEYOND_WPDB_PRIMARYS ) as $primary ) { 15 $this->triggers[$primary] = array(); 16 14 17 $insert_trigger = 'insert_' . $primary . '_trigger'; 15 18 $this->$insert_trigger = esc_sql( $wpdb->prefix . 'insert_' . $primary . '_trigger' ); 19 array_push( $this->triggers[$primary], $this->$insert_trigger ); 16 20 17 21 $delete_trigger = 'delete_' . $primary . '_trigger'; 18 22 $this->$delete_trigger = esc_sql( $wpdb->prefix . 'delete_' . $primary . '_trigger' ); 23 array_push( $this->triggers[$primary], $this->$delete_trigger ); 19 24 20 25 $insert_meta_trigger = 'insert_' . $primary . 'meta_trigger'; 21 26 $this->$insert_meta_trigger = esc_sql( $wpdb->prefix . 'insert_' . $primary . 'meta_trigger' ); 27 array_push( $this->triggers[$primary], $this->$insert_meta_trigger ); 22 28 23 29 $update_meta_trigger = 'update_' . $primary . 'meta_trigger'; 24 30 $this->$update_meta_trigger = esc_sql( $wpdb->prefix . 'update_' . $primary . 'meta_trigger' ); 31 array_push( $this->triggers[$primary], $this->$update_meta_trigger ); 25 32 26 33 $delete_meta_trigger = 'delete_' . $primary . 'meta_trigger'; 27 34 $this->$delete_meta_trigger = esc_sql( $wpdb->prefix . 'delete_' . $primary . 'meta_trigger' ); 35 array_push( $this->triggers[$primary], $this->$delete_meta_trigger ); 28 36 } 29 37 } … … 32 40 * Create table 33 41 * 34 * @ return void35 * /36 function create_table() : void {37 foreach( BEYOND_WPDB_PRIMARYS as $primary => $values ){38 $this->create_table_sql( $primary, $values['meta_table_key'] );39 }42 * @param string $type 43 * @return void 44 */ 45 function create_table( $primary ) : void { 46 $values = BEYOND_WPDB_PRIMARYS[$primary]; 47 $this->create_table_sql( $primary, $values['meta_table_key'] ); 40 48 } 41 49 … … 66 74 * Drop table 67 75 * 68 * @ return void69 * /70 function drop_table() {71 foreach( array_keys( BEYOND_WPDB_PRIMARYS ) as $primary ) {72 $this->drop_table_sql( $primary );73 }76 * @param $primary 77 * @return void 78 * @throws Exception 79 */ 80 function drop_table( $primary ) { 81 $this->drop_table_sql( $primary ); 74 82 } 75 83 … … 79 87 * @param string $primary 80 88 * @return void 89 * @throws Exception 81 90 */ 82 91 protected function drop_table_sql( $primary ) { 83 92 global $wpdb; 84 $table_name = esc_sql( constant( beyond_wpdb_get_define_table_name( $primary ) ) ); 85 86 $sql = 'DROP TABLE ' . $table_name; 87 $wpdb->query( $sql ); 93 94 $exist_tables = $this->get_exist_json_tables(); 95 $table_name = esc_sql( constant( beyond_wpdb_get_define_table_name( $primary ) ) ); 96 97 if ( in_array( $table_name, $exist_tables ) ) { 98 $sql = 'DROP TABLE ' . $table_name; 99 $wpdb->query( $sql ); 100 101 if ( $wpdb->last_error ) { 102 throw new Exception( $wpdb->last_error ); 103 } 104 } 88 105 } 89 106 … … 93 110 * Insert/Update/Delete postmeta trigger 94 111 * 95 * @ return void96 * /97 function create_trigger() {98 foreach( BEYOND_WPDB_PRIMARYS as $primary => $values ) {99 $this->insert_primary_trigger( $primary, $values['primary_table_name'], $values['primary_table_key'], $values['meta_table_key'] );100 $this->delete_primary_trigger( $primary, $values['primary_table_name'], $values['primary_table_key'], $values['meta_table_key'] );101 $this->insert_meta_trigger( $primary, $values['meta_table_name'], $values['meta_table_key'] );102 $this->update_meta_trigger( $primary, $values['meta_table_name'], $values['meta_table_key'] );103 $this->delete_meta_trigger( $primary, $values['meta_table_name'], $values['meta_table_key'] );104 105 }112 * @param string $primary 113 * @return void 114 * @throws Exception 115 */ 116 function create_trigger( $primary ) { 117 $values = BEYOND_WPDB_PRIMARYS[$primary]; 118 $this->insert_primary_trigger( $primary, $values['primary_table_name'], $values['primary_table_key'], $values['meta_table_key'] ); 119 $this->delete_primary_trigger( $primary, $values['primary_table_name'], $values['primary_table_key'], $values['meta_table_key'] ); 120 $this->insert_meta_trigger( $primary, $values['meta_table_name'], $values['meta_table_key'] ); 121 $this->update_meta_trigger( $primary, $values['meta_table_name'], $values['meta_table_key'] ); 122 $this->delete_meta_trigger( $primary, $values['meta_table_name'], $values['meta_table_key'] ); 106 123 } 107 124 … … 114 131 * @param string $meta_table_key 115 132 * @return void 133 * @throws Exception 116 134 */ 117 135 protected function insert_primary_trigger( $primary, $primaty_table_name, $primary_table_key, $meta_table_key ) { … … 119 137 120 138 $insert_trigger = 'insert_' . $primary . '_trigger'; 139 $exist_triggers = $this->get_exist_triggers(); 121 140 $table_name = esc_sql( constant( beyond_wpdb_get_define_table_name( $primary ) ) ); 122 141 $primaty_table_name = esc_sql( $primaty_table_name ); … … 124 143 $primary_table_key = esc_sql( $primary_table_key ); 125 144 126 $sql = 'CREATE TRIGGER ' . $this->$insert_trigger . ' AFTER 145 if ( ! in_array( $this->$insert_trigger, $exist_triggers ) ) { 146 $sql = 'CREATE TRIGGER ' . $this->$insert_trigger . ' AFTER 127 147 INSERT ON 128 148 ' . $primaty_table_name . ' … … 134 154 VALUES 135 155 (NEW.' . $primary_table_key . ', "{}" )'; 136 $wpdb->query( $sql ); 156 $wpdb->query( $sql ); 157 158 if ( $wpdb->last_error ) { 159 throw new Exception( $wpdb->last_error ); 160 } 161 } 137 162 } 138 163 … … 145 170 * @param string $meta_table_key 146 171 * @return void 172 * @throws Exception 147 173 */ 148 174 protected function delete_primary_trigger( $primary, $primaty_table_name, $primary_table_key, $meta_table_key ) { … … 150 176 151 177 $delete_trigger = 'delete_' . $primary . '_trigger'; 178 $exist_triggers = $this->get_exist_triggers(); 152 179 $table_name = esc_sql( constant( beyond_wpdb_get_define_table_name( $primary ) ) ); 153 180 $primaty_table_name = esc_sql( $primaty_table_name ); … … 155 182 $primary_table_key = esc_sql( $primary_table_key ); 156 183 157 $sql = 'CREATE TRIGGER ' . $this->$delete_trigger . ' BEFORE 158 DELETE ON 159 ' . $primaty_table_name . ' 160 FOR 161 EACH 162 ROW 163 DELETE FROM ' . $table_name . ' 164 WHERE ' . $meta_table_key . ' = OLD.' . $primary_table_key; 165 $wpdb->query( $sql ); 184 if ( ! in_array( $this->$delete_trigger, $exist_triggers ) ) { 185 $sql = 'CREATE TRIGGER ' . $this->$delete_trigger . ' BEFORE 186 DELETE ON 187 ' . $primaty_table_name . ' 188 FOR 189 EACH 190 ROW 191 DELETE FROM ' . $table_name . ' 192 WHERE ' . $meta_table_key . ' = OLD.' . $primary_table_key; 193 $wpdb->query( $sql ); 194 195 if ( $wpdb->last_error ) { 196 throw new Exception( $wpdb->last_error ); 197 } 198 } 166 199 } 167 200 … … 173 206 * @param string $meta_table_key 174 207 * @return void 208 * @throws Exception 175 209 */ 176 210 protected function insert_meta_trigger( $primary, $meta_table_name, $meta_table_key ) { 177 211 global $wpdb; 178 212 213 $exist_triggers = $this->get_exist_triggers(); 179 214 $insert_meta_trigger = 'insert_' . $primary . 'meta_trigger'; 180 215 $table_name = esc_sql( constant( beyond_wpdb_get_define_table_name( $primary ) ) ); … … 182 217 $meta_table_key = esc_sql( $meta_table_key ); 183 218 184 $sql = 'CREATE TRIGGER ' . $this->$insert_meta_trigger . ' AFTER 219 if ( ! in_array( $this->$insert_meta_trigger, $exist_triggers ) ) { 220 $sql = 'CREATE TRIGGER ' . $this->$insert_meta_trigger . ' AFTER 185 221 INSERT ON 186 222 ' . $meta_table_name . ' … … 193 229 (`json`, CONCAT 194 230 ("$.",NEW.meta_key), NEW.meta_value) WHERE ' . $meta_table_key . ' = NEW.'.$meta_table_key; 195 $wpdb->query( $sql ); 231 $wpdb->query( $sql ); 232 233 if ( $wpdb->last_error ) { 234 throw new Exception( $wpdb->last_error ); 235 } 236 } 196 237 } 197 238 … … 203 244 * @param string $meta_table_key 204 245 * @return void 246 * @throws Exception 205 247 */ 206 248 protected function update_meta_trigger( $primary, $meta_table_name, $meta_table_key ) { 207 249 global $wpdb; 208 250 251 $exist_triggers = $this->get_exist_triggers(); 209 252 $update_meta_trigger = 'update_' . $primary . 'meta_trigger'; 210 253 $table_name = esc_sql( constant( beyond_wpdb_get_define_table_name( $primary ) ) ); … … 212 255 $meta_table_key = esc_sql( $meta_table_key ); 213 256 214 $sql = 'CREATE TRIGGER ' . $this->$update_meta_trigger . ' AFTER 257 if ( ! in_array( $this->$update_meta_trigger, $exist_triggers ) ) { 258 $sql = 'CREATE TRIGGER ' . $this->$update_meta_trigger . ' AFTER 215 259 UPDATE ON 216 260 ' . $meta_table_name . ' … … 223 267 (`json`, CONCAT 224 268 ("$.",NEW.meta_key), NEW.meta_value) WHERE ' . $meta_table_key . ' = NEW.' . $meta_table_key; 225 $wpdb->query( $sql ); 269 $wpdb->query( $sql ); 270 271 if ( $wpdb->last_error ) { 272 throw new Exception( $wpdb->last_error ); 273 } 274 } 226 275 } 227 276 … … 233 282 * @param string $meta_table_key 234 283 * @return void 284 * @throws Exception 235 285 */ 236 286 protected function delete_meta_trigger( $primary, $meta_table_name, $meta_table_key ) { 237 287 global $wpdb; 238 288 289 $exist_triggers = $this->get_exist_triggers(); 239 290 $delete_meta_trigger = 'delete_' . $primary . 'meta_trigger'; 240 291 $table_name = esc_sql( constant( beyond_wpdb_get_define_table_name( $primary ) ) ); … … 242 293 $meta_table_key = esc_sql( $meta_table_key ); 243 294 244 $sql = 'CREATE TRIGGER ' . $this->$delete_meta_trigger . ' AFTER 295 if ( ! in_array( $this->$delete_meta_trigger, $exist_triggers ) ) { 296 $sql = 'CREATE TRIGGER ' . $this->$delete_meta_trigger . ' AFTER 245 297 DELETE ON 246 298 ' . $meta_table_name . ' … … 252 304 `json` = JSON_REMOVE(`json`, CONCAT 253 305 ("$.",OLD.meta_key)) WHERE ' . $meta_table_key . ' = OLD.'. $meta_table_key; 306 $wpdb->query( $sql ); 307 308 if ( $wpdb->last_error ) { 309 throw new Exception( $wpdb->last_error ); 310 } 311 } 312 } 313 314 /** 315 * Drop triggers 316 * 317 * @param $primary 318 * @return void 319 * @throws Exception 320 */ 321 function drop_triggers( $primary ) { 322 global $wpdb; 323 324 $exist_triggers = $this->get_exist_triggers(); 325 326 foreach ( $this->triggers[$primary] as $value ) { 327 if ( in_array( $value, $exist_triggers ) ) { 328 $sql = 'DROP TRIGGER ' . esc_sql( $value ); 329 $wpdb->query( $sql ); 330 } 331 } 332 333 if ( $wpdb->last_error ) { 334 throw new Exception( $wpdb->last_error ); 335 } 336 } 337 338 /** 339 * Update table from post, postmeta table. 340 * 341 * @param $primary 342 * 343 * @throws Exception 344 */ 345 function data_init( $primary ) { 346 global $wpdb; 347 $values = BEYOND_WPDB_PRIMARYS[$primary]; 348 $maxlen = 4294967295; 349 350 $maxlen = apply_filters( 'beyond_group_concat_max_len', $maxlen ); 351 $sql = 'SET SESSION group_concat_max_len = ' . $maxlen; 254 352 $wpdb->query( $sql ); 255 } 256 257 /** 258 * Drop triggers 259 * 260 * @return void 261 */ 262 function drop_triggers() { 263 global $wpdb; 264 265 foreach( get_object_vars( $this ) as $value ) { 266 $sql = 'DROP TRIGGER ' . esc_sql( $value ); 267 $wpdb->query( $sql ); 268 } 269 } 270 271 /** 272 * Update table from post, postmeta table. 273 * 274 * @return void 275 */ 276 function data_init() { 277 foreach( BEYOND_WPDB_PRIMARYS as $primary => $values ) { 278 $this->data_init_sql( $primary, $values['primary_table_name'], $values['primary_table_key'], $values['meta_table_name'], $values['meta_table_key'] ); 279 $this->delete_non_existent_data_from_json( $primary, $values['primary_table_name'], $values['primary_table_key'] ); 280 } 353 354 $this->data_init_sql( $primary, $values['primary_table_name'], $values['primary_table_key'], $values['meta_table_name'], $values['meta_table_key'] ); 355 $this->delete_non_existent_data_from_json( $primary, $values['primary_table_name'], $values['primary_table_key'] ); 281 356 } 282 357 … … 284 359 * data init sql 285 360 * 286 * @param string $primary 287 * @param string $primary_table_name 288 * @param string $primary_table_key 289 * @param string $meta_table_name 290 * @param string $meta_table_key 291 * @return void 361 * @param $primary 362 * @param $primary_table_name 363 * @param $primary_table_key 364 * @param $meta_table_name 365 * @param $meta_table_key 366 * 367 * @throws Exception 292 368 */ 293 369 protected function data_init_sql( $primary, $primary_table_name, $primary_table_key, $meta_table_name, $meta_table_key ) { … … 311 387 UPDATE json = VALUES(json)'; 312 388 $wpdb->query( $sql ); 389 390 if ( $wpdb->last_error ) { 391 throw new Exception( $wpdb->last_error ); 392 } 313 393 } 314 394 … … 320 400 * @param $primary_table_key 321 401 * @return void 402 * @throws Exception 322 403 */ 323 404 protected function delete_non_existent_data_from_json( $primary, $primary_table_name, $primary_table_key ) { … … 334 415 335 416 $wpdb->query( $sql ); 417 418 if ( $wpdb->last_error ) { 419 throw new Exception( $wpdb->last_error ); 420 } 421 } 422 423 /** 424 * Get exist virtual columns 425 * @return array 426 */ 427 protected function get_exist_triggers() { 428 $beyond_wpdb_info = new Beyond_Wpdb_Information(); 429 $beyond_wpdb_info->set_triggers(); 430 return $beyond_wpdb_info->get_triggers(); 431 } 432 433 /** 434 * Get exist json tables 435 * @return array 436 */ 437 protected function get_exist_json_tables(){ 438 $beyond_wpdb_info = new Beyond_Wpdb_Information(); 439 $beyond_wpdb_info->set_tables(); 440 return $beyond_wpdb_info->get_tables(); 336 441 } 337 442 } -
beyond-wpdb/tags/2.0.0/readme.txt
r2337807 r2352101 2 2 Contributors: pressmaninc,kazunao,hiroshisekiguchi,hommakoharu,pmhirotaka 3 3 Tags: pressman,pressmaninc,json,meta_query,wpdb,database,fast,speed,speed up,sql,replace sql 4 Stable tag: 1.1.04 Stable tag: 2.0.0 5 5 License: GPLv2 or later 6 6 License URI: http://www.gnu.org/licenses/gpl-2.0.html … … 10 10 Requires MariaDB: 10.2 11 11 Tested up to: 5.4 12 13 ※ Table names have been changed since v2.0.Please delete the meta_json table and reactivate it in the options screen. 12 14 13 15 Are you having trouble with slow queries due to WordPress table structure? … … 23 25 24 26 == Actual Behavior == 27 * You can create and delete json tables and triggers and data for each json table on the configuration page. 28 * Create virtual columns and indexes for the json table you created in the configuration page. 29 * If you add a virtual column to the json table in the configuration page, the SQL is converted to face the virtual column. 25 30 * Converts the SQL that WordPress auto-generates when using the default APIs of get_posts, Wp_Query, and etc. into a simple SQL that combines the posts/users/comment table with its own meta table. 26 31 * The custom meta table automatically stores data using MySQL and MariaDB triggers. … … 32 37 33 38 == Custom meta tables generated by plugin == 34 * postmeta_json (for posts table) 35 * usermeta_json (for users table) 36 * commentmeta_json (for comment table) 39 The actual table names are prefixed with $table_prefix. 40 * postmeta_beyond (for posts table) 41 * usermeta_beyond (for users table) 42 * commentmeta_beyond (for comment table) 37 43 38 44 == Columns in each table == 39 * postmeta_json 45 The actual table names are prefixed with $table_prefix. 46 * postmeta_beyond 40 47 * post_id(int) 41 48 * json(json) 42 * usermeta_ json49 * usermeta_beyond 43 50 * post_id(int) 44 51 * json(json) 45 * commentmeta_ json52 * commentmeta_beyond 46 53 * post_id(int) 47 54 * json(json) … … 53 60 ex1 ) Create 100 posts and 50 meta data for each post. Combine them 9 times. 54 61 55 * postmeta_ json62 * postmeta_beyond 56 63 * Equal:0.04 57 64 * IN:0.09 … … 61 68 * BETWEEN:0.04 62 69 * NOT BETWEEN:0.04 70 63 71 * postmeta 64 72 * Equal:4.72 … … 70 78 * NOT BETWEEN:0.63 71 79 72 ex2 ) 10000 posts, 1 meta(category) per post.If you add a virtual column and put an index in Category, or if you add a virtual column and put an index in Category 73 * postmeta_json(without virtual column) 80 ex2 ) 10,000 posts, 1 meta per post. meta_key is category. 81 Add a virtual column(category) to postmeta_beyond and put an index, 82 and compare it to the case without adding a virtual column. 83 84 * postmeta_beyond(with virtual column) 85 * Equal:0.02 86 * IN:0.35 87 * BETWEEN:0.12 88 * NOT BETWEEN:0.09 89 90 * postmeta_beyond(without virtual column) 74 91 * Equal:0.15 75 92 * IN:1.92 76 93 * BETWEEN:0.37 77 94 * NOT BETWEEN:0.29 78 79 * postmeta_json(with virtual column)80 * Equal:0.0281 * IN:0.3582 * BETWEEN:0.1283 * NOT BETWEEN:0.0984 95 85 96 == Installation == … … 91 102 * You'll need TRIGGER, SELECT, and UPDATE permissions for the target table and its columns to use the trigger feature of MySQL and MariaDB. 92 103 * It makes changes to SQL only if `suppress_filters` is false. 104 * This plugin sets 4294967295 to group_concat_max_len when it create an original table with JSON type column. You can change the number by using filter 'beyond_group_concat_max_len'. -
beyond-wpdb/trunk/beyond-wpdb.php
r2337801 r2352101 3 3 Plugin Name: Beyond Wpdb 4 4 Plugin URI: 5 Description: Create a new table and register the meta data in a single row of JSON columns.Registration is done by mysql's trigger function.The data in WordPress is not affected.Convert sql statement, when searching for meta data.6 Version: 1.1.05 Description: Speed up your WordPress database by making use of JSON type columns in MySQL. 6 Version: 2.0.0 7 7 Author: PRESSMAN 8 8 Author URI: https://www.pressman.ne.jp/ … … 45 45 // Deifne Beyond_table name. 46 46 foreach( array_keys( BEYOND_WPDB_PRIMARYS ) as $primary ) { 47 define( beyond_wpdb_get_define_table_name( $primary ), $wpdb->prefix . $primary . 'meta_ json' );47 define( beyond_wpdb_get_define_table_name( $primary ), $wpdb->prefix . $primary . 'meta_beyond' ); 48 48 } 49 49 50 50 // Require files. 51 require_once( plugin_dir_path( __FILE__ ) . 'class/register-hook.php' ); 51 require_once( plugin_dir_path( __FILE__ ) . 'class/register.php' ); 52 require_once( plugin_dir_path( __FILE__ ) . 'class/information.php' ); 52 53 require_once( plugin_dir_path( __FILE__ ) . 'class/sql.php' ); 53 54 require_once( plugin_dir_path( __FILE__ ) . 'class/meta-query.php' ); 54 55 require_once( plugin_dir_path( __FILE__ ) . 'class/wp-orderby.php' ); 55 require_once( plugin_dir_path( __FILE__ ) . 'class/column.php' );56 56 require_once( plugin_dir_path( __FILE__ ) . 'class/options.php' ); 57 58 // Plugin activation hook. 59 register_activation_hook( __FILE__, ['Beyond_Wpdb_Register_Hook', 'activation'] ); 60 register_deactivation_hook( __FILE__, ['Beyond_Wpdb_Register_Hook', 'deactivation'] ); 61 register_uninstall_hook( __FILE__, ['Beyond_Wpdb_Register_Hook', 'uninstall'] ); 57 require_once( plugin_dir_path( __FILE__ ) . 'class/admin-ajax.php' ); -
beyond-wpdb/trunk/class/meta-query.php
r2337801 r2352101 467 467 */ 468 468 public function virtual_column_exists( $key, $type ) { 469 $beyond_wpdb_column = new Beyond_Wpdb_Column(); 470 $beyond_wpdb_column->set_columns(); 471 $virtual_columns = $beyond_wpdb_column->get_columns(); 472 473 return in_array( $key, $virtual_columns[$type] ); 469 $beyond_wpdb_info = new Beyond_Wpdb_Information(); 470 $beyond_wpdb_info->set_columns(); 471 $virtual_columns = $beyond_wpdb_info->get_columns(); 472 $table_name = esc_sql( constant( beyond_wpdb_get_define_table_name( $type ) ) ); 473 474 return in_array( $key, $virtual_columns[$table_name] ); 474 475 } 475 476 -
beyond-wpdb/trunk/class/options.php
r2337802 r2352101 9 9 */ 10 10 class Beyond_Wpdb_Settings_page { 11 private $ columns = array();11 private $privilege_error = False; 12 12 13 13 /** … … 16 16 public function __construct() 17 17 { 18 load_theme_textdomain( 'beyond-wpdb', plugin_dir_path( __FILE__ ) . '../lang' ); 19 add_action( 'admin_notices', array( $this, 'notice__warnig_or_error' ) ); 18 20 add_action( 'admin_menu', array( $this, 'add_beyond_wpdb_settings_page' ) ); 19 21 add_action( 'admin_init', array( $this, 'page_init' ) ); 20 $beyond_wpdb_column = new Beyond_Wpdb_Column(); 21 $beyond_wpdb_column->set_columns(); 22 $this->columns = $beyond_wpdb_column->get_columns(); 23 24 // add option beyond_wpdb_virtual_column_name 25 add_action( 'add_option_beyond_wpdb_virtual_column_name', array( $this, 'do_after_add_virtual_column' ) ,10, 2 ); 26 // add option beyond_wpdb_data_init_name 27 add_action( 'add_option_beyond_wpdb_data_init_name', array( $this, 'do_after_add_data_init' ) ,10, 2 ); 28 // update option beyond_wpdb_virtual_column_name 29 add_action( 'update_option_beyond_wpdb_virtual_column_name', array( $this, 'do_after_update_virtual_column' ) ,10, 3 ); 30 // update option beyond_wpdb_data_init_name 31 add_action( 'update_option_beyond_wpdb_data_init_name', array ( $this, 'do_after_update_data_init' ) ,10, 3 ); 32 22 23 } 24 25 /** 26 * Display warnig or error 27 */ 28 function notice__warnig_or_error() { 29 global $wpdb, $pagenow; 30 31 $current_db = $wpdb->get_var( "SELECT DATABASE()" ); 32 $current_user = explode('@', $wpdb->get_var( "SELECT USER()" )); 33 $grants = $wpdb->get_results( "SHOW GRANTS FOR '{$current_user[0]}'@'{$current_user[1]}'", ARRAY_A ); 34 $grant_for_mysql_db = False; 35 $current_privileges = array(); 36 37 foreach ( $grants as $value ) { 38 $value = array_values( $value )[0]; 39 if ( strpos( $value, "ON `mysql`." ) || strpos( $value, "root" ) ) { 40 $grant_for_mysql_db = True; 41 } 42 } 43 44 if ( $grant_for_mysql_db ) { 45 $result_privileges = $wpdb->get_results( "SELECT * FROM mysql.db WHERE Db = '{$current_db}' AND User = '{$current_user[0]}'", ARRAY_A ); 46 foreach ( $result_privileges as $val ) { 47 $keys = array_keys( $val ); 48 $values = array_values( $val ); 49 50 // If $v is Y, $keys[$idx] is assumed to be authorized. 51 foreach ( $values as $idx => $v ) { 52 if ( $v === 'Y' ) { 53 array_push( $current_privileges, $keys[$idx] ); 54 } 55 } 56 } 57 } 58 59 $privileges_error = ''; 60 $expected_privileges = array( 61 'Trigger_priv', 62 'Select_priv', 63 'Insert_priv', 64 'Update_priv', 65 'Delete_priv', 66 'Create_priv', 67 'Drop_priv' 68 ); 69 if ( count( $current_privileges ) > 0 ) { 70 foreach ( $expected_privileges as $privilege ) { 71 if ( ! in_array( $privilege, $current_privileges ) ) { 72 switch ( $privilege ) { 73 case 'Trigger_priv': 74 $privileges_error .= "<p>" . __("You do not have permission to create triggers.", "beyond-wpdb") . "</p>"; 75 break; 76 case 'Select_priv': 77 $privileges_error .= "<p>" . __("You don't have SELECT permission.", "beyond-wpdb") . "</p>"; 78 break; 79 case 'Insert_priv': 80 $privileges_error .= "<p>" . __("You don't have INSERT permission.", "beyond-wpdb") . "</p>"; 81 break; 82 case 'Update_priv': 83 $privileges_error .= "<p>" . __("You don't have UPDATE permission.", "beyond-wpdb") . "</p>"; 84 break; 85 case 'Delete_priv': 86 $privileges_error .= "<p>" . __("You don't have DELETE permission.", "beyond-wpdb") . "</p>"; 87 break; 88 case 'Create_priv': 89 $privileges_error .= "<p>" . __("You don't have CREATE permission.", "beyond-wpdb") . "</p>"; 90 break; 91 default : 92 $privileges_error .= "<p>" . __("You don't have DROP permission.", "beyond-wpdb") . "</p>"; 93 break; 94 } 95 $this->privilege_error = True; 96 } 97 } 98 } 99 100 if ( isset( $_GET['page'] ) && ! $_GET['page'] && $pagenow === 'options-general.php' ) { 101 if ( ! $grant_for_mysql_db ) { 102 $grants_message = __("You do not have permission to the mysql database.<br>We access the db table in the mysql database to find out what permissions we have to use each feature.<br>There is a possibility that each function will not be available.", "beyond-wpdb" ); 103 print " 104 <div class='notice notice-warning is-dismissible'> 105 <p> 106 {$grants_message} 107 </p> 108 </div> 109 "; 110 } 111 112 if ( $privileges_error ) { 113 $priviles_message = __( "You do not have the following permissions.Please use each function after granting permission.", "beyond-wpdb" ); 114 print " 115 <div class='notice notice-error is-dismissible'> 116 <p>{$priviles_message}</p> 117 {$privileges_error} 118 </div>"; 119 } 120 } 33 121 } 34 122 … … 52 140 public function create_beyond_wpdb_settings_page() 53 141 { 142 $disabled = $this->checkDisabled() ? '' : 'disabled'; 143 $features = __( "Speed up database loading by creating your own tables that aggregate meta information.<br>This is especially useful when you have a large number of records and for complex meta-query data calls.", "beyond-wpdb" ); 54 144 ?> 145 <style> 146 .d-block { 147 display: block; 148 } 149 .d-inline-block { 150 display: inline-block; 151 } 152 .d-none { 153 display: none; 154 } 155 .table_not_exists { 156 opacity:0.3; 157 pointer-envet:none; 158 } 159 </style> 55 160 <div class='beyond-wpdb-settings-wrap'> 56 161 <!-- title --> 57 <h1 style="margin-bottom: 30px;">Beyond WPDB Settings</h1> 58 <!-- form --> 59 <form action='options.php' method='post'> 60 <!-- Group Concat --> 61 <div> 62 <?php settings_fields( 'beyond_wpdb_group' ); ?> 63 <?php do_settings_sections( 'croup_concat_section' ); ?> 64 </div> 65 <div> 66 <?php settings_fields( 'beyond_wpdb_group' ); ?> 67 <?php do_settings_sections( 'data_init_section' ); ?> 68 </div> 69 <!-- Virtual Column Settings --> 70 <div> 71 <?php settings_fields( 'beyond_wpdb_group' ); ?> 72 <?php do_settings_sections( 'virtual_columns_section' ); ?> 73 </div> 162 <div style="margin-bottom: 30px;"> 163 <h1 style="margin-bottom: 30px;">Beyond WPDB Settings</h1> 164 <p> 165 <?php echo $features; ?> 166 </p> 167 <h2><?php echo __( "Settings", "beyond-wpdb" ) ?></h2> 168 </div> 169 <!-- data init --> 170 <div class="data_init_section"> 171 <?php settings_fields( 'beyond_wpdb_group' ); ?> 172 <?php do_settings_sections( 'data_init_section' ); ?> 74 173 75 174 <!-- Submit Button --> 76 <?php submit_button(); ?> 77 </form> 175 <p class="submit"> 176 <button id="beyond-wpdb-init-btn" class="button button-primary" <?php echo $disabled; ?>>Update</button> 177 </p> 178 </> 179 <!-- Virtual Column Settings --> 180 <div> 181 <?php settings_fields( 'beyond_wpdb_group' ); ?> 182 <?php do_settings_sections( 'virtual_columns_section' ); ?> 183 184 <!-- Submit Button --> 185 <p class="submit"> 186 <button id="beyond-wpdb-virtual-columns-btn" class="button button-primary" <?php echo $disabled; ?>>Update</button> 187 </p> 188 </div> 189 <!-- notice --> 190 <div> 191 <h2><?php echo __( "NOTICE", "beyond-wpdb" ) ?></h2> 192 <p><?php echo __( "This plugin sets 4294967295 to group_concat_max_len when it createa an original table with JSON type column.<br>You can change the number by using filter 'beyond_group_concat_max_len'.", "beyond-wpdb" ) ?></p> 193 </div> 78 194 </div> 79 195 <?php … … 85 201 public function page_init() 86 202 { 87 // group concat 88 add_settings_section( 89 'setting_section_croup_concat', 90 '<h2>Group Concat</h2>', 91 array( $this, 'print_group_concat_section_info' ), 92 'croup_concat_section' 93 ); 94 95 add_settings_field( 96 '', 97 "group_concat_max_len", 98 array( $this, 'print_croup_concat_field' ), 99 'croup_concat_section', 100 'setting_section_croup_concat' 101 ); 102 103 register_setting( 104 'beyond_wpdb_group', 105 '', 106 'sanitize_text_field' 107 ); 108 203 global $wpdb; 109 204 // data init 110 205 add_settings_section( 111 206 'setting_section_data_init', 112 '<h2> Data Init</h2>',207 '<h2>' . __( "Creating and deleting custom metatables", "beyond-wpdb" ) . '</h2>', 113 208 array( $this, 'print_data_init_section_info' ), 114 209 'data_init_section' … … 116 211 117 212 add_settings_field( 118 'data_init ',119 "Data Init",213 'data_init_postmeta_json', 214 __( "Table for postmeta", "beyond-wpdb" ) . "<br><span style='font-size: 0.7rem; color: #808080;'>Table name: postmeta_beyond</span>", 120 215 array( $this, 'print_data_init_field' ), 121 216 'data_init_section', 122 'setting_section_data_init' 217 'setting_section_data_init', 218 array( 219 'id' => "data_init_{$wpdb->prefix}postmeta_beyond", 220 'class' => "{$wpdb->prefix}postmeta_beyond" 221 ) 222 ); 223 224 add_settings_field( 225 'data_init_usermeta_json', 226 __( "Table for usermeta", "beyond-wpdb" ) . "<br><span style='font-size: 0.7rem; color: #808080;'>Table name: usermeta_beyond</span>", 227 array( $this, 'print_data_init_field' ), 228 'data_init_section', 229 'setting_section_data_init', 230 array( 231 'id' => "data_init_{$wpdb->prefix}usermeta_beyond", 232 'class' => "{$wpdb->prefix}usermeta_beyond" 233 ) 234 ); 235 236 add_settings_field( 237 'data_init_commentmeta_json', 238 __( "Table for commentmeta", "beyond-wpdb" ) . "<br><span style='font-size: 0.7rem; color: #808080;'>Table name: commentmeta_beyond</span>", 239 array( $this, 'print_data_init_field' ), 240 'data_init_section', 241 'setting_section_data_init', 242 array( 243 'id' => "data_init_{$wpdb->prefix}commentmeta_beyond", 244 'class' => "{$wpdb->prefix}commentmeta_beyond" 245 ) 123 246 ); 124 247 … … 126 249 'beyond_wpdb_group', 127 250 'beyond_wpdb_data_init_name', 128 array( $this, 'sanitize_input_columns')251 array() 129 252 ); 130 253 … … 132 255 add_settings_section( 133 256 'virtual_column_section_id', 134 '<h2> Virtual Column Settings</h2>',257 '<h2>' . __( "Virtual Column Settings", "beyond-wpdb" ) . '</h2>', 135 258 array( $this, 'print_virtual_column_section_info' ), 136 259 'virtual_columns_section' … … 139 262 add_settings_field( 140 263 'postmeta_json', // id 141 "postmeta_json", // title264 __( "Table for postmeta", "beyond-wpdb" ) . "<br><span style='font-size: 0.7rem; color: #808080;'>Table name: postmeta_beyond</span>", // title 142 265 array( $this, 'print_postmeta_json_field' ), // callback 143 266 'virtual_columns_section', // section page 144 'virtual_column_section_id' // section id 267 'virtual_column_section_id', // section id, 268 array( 269 'id' => "virtual_column_{$wpdb->prefix}postmeta_beyond", 270 'class' => "virtual_column_{$wpdb->prefix}postmeta_beyond" 271 ) 145 272 ); 146 273 147 274 add_settings_field( 148 275 'usermeta_json', 149 "usermeta_json",276 __( "Table for usermeta", "beyond-wpdb" ) . "<br><span style='font-size: 0.7rem; color: #808080;'>Table name: usermeta_beyond</span>", 150 277 array( $this, 'print_usermeta_json_field' ), 151 278 'virtual_columns_section', // section page 152 'virtual_column_section_id' // section id 279 'virtual_column_section_id', // section id 280 array( 281 'id' => "virtual_column_{$wpdb->prefix}usermeta_beyond", 282 'class' => "virtual_column_{$wpdb->prefix}usermeta_beyond" 283 ) 153 284 ); 154 285 155 286 add_settings_field( 156 287 'commentmeta_json', // id 157 "commentmeta_json", // title288 __( "Table for commentmeta", "beyond-wpdb" ) . "<br><span style='font-size: 0.7rem; color: #808080;'>Table name: commentmeta_beyond</span>", // title 158 289 array( $this, 'print_commentmeta_json_field' ), // callback 159 290 'virtual_columns_section', // section id 160 'virtual_column_section_id' 291 'virtual_column_section_id', 292 array( 293 'id' => "virtual_column_{$wpdb->prefix}commentmeta_beyond", 294 'class' => "virtual_column_{$wpdb->prefix}commentmeta_beyond" 295 ) 161 296 ); 162 297 … … 164 299 'beyond_wpdb_group', 165 300 'beyond_wpdb_virtual_column_name', 166 array( $this, 'sanitize_input_columns' ) 167 ); 168 } 169 170 /** 171 * @param $option 172 * @param $value 173 */ 174 public function do_after_add_virtual_column( $option, $value ) { 175 $this->do_virtual_column_processing( $option ); 176 } 177 178 /** 179 * @param $option 180 * @param $value 181 */ 182 public function do_after_add_data_init( $option, $value ) { 183 $this->do_data_init_processing( $option ); 184 } 185 186 /** 187 * @param $old 188 * @param $value 189 * @param $option 190 */ 191 public function do_after_update_virtual_column( $old, $value, $option ) { 192 $this->do_virtual_column_processing( $option ); 193 } 194 195 /** 196 * @param $old 197 * @param $value 198 * @param $option 199 */ 200 public function do_after_update_data_init( $old, $value, $option ) { 201 $this->do_data_init_processing( $option ); 202 } 203 204 205 /** 206 * @param $option 207 * create virtual column and delete virtual column and delete option 208 */ 209 public function do_virtual_column_processing( $option ) { 210 $this->create_virtual_column_and_index( get_option( $option ) ); 211 $this->delete_virtual_column( get_option( $option ) ); 212 delete_option( $option ); 213 } 214 215 /** 216 * @param $option 217 * data init 218 */ 219 public function do_data_init_processing( $option ) { 220 $this->data_init( get_option($option) ); 221 delete_option( $option ); 222 } 223 224 /** 225 * @param $input 226 * 227 * @return array 228 */ 229 public function sanitize_input_columns( $input ) 230 { 231 $new_input = array(); 232 if( isset( $input['postmeta_json'] ) ) { 233 $input_columns = explode( PHP_EOL, $input['postmeta_json'] ); 234 $input_columns = $this->sanitize( $input_columns ); 235 $new_input['postmeta_json'] = implode( PHP_EOL, $input_columns ); 236 } 237 238 if( isset( $input['usermeta_json'] ) ) { 239 $input_columns = explode( PHP_EOL, $input['usermeta_json'] ); 240 $input_columns = $this->sanitize( $input_columns ); 241 $new_input['usermeta_json'] = implode( PHP_EOL, $input_columns ); 242 } 243 244 if( isset( $input['commentmeta_json'] ) ) { 245 $input_columns = explode( PHP_EOL, $input['commentmeta_json'] ); 246 $input_columns = $this->sanitize( $input_columns ); 247 $new_input['commentmeta_json'] = implode( PHP_EOL, $input_columns ); 248 } 249 250 if( isset( $input['data_init'] ) ) { 251 $input_columns = explode( PHP_EOL, $input['data_init'] ); 252 $input_columns = $this->sanitize( $input_columns ); 253 $new_input['data_init'] = implode( PHP_EOL, $input_columns ); 254 } 255 256 return $new_input; 257 } 258 259 /** 260 * @param $option 261 * data init 262 */ 263 public function data_init( $option ) 264 { 265 if ( isset( $option['data_init'] ) && $option['data_init'] === '1' ) { 266 $beyond_wpdb_sql = new Beyond_Wpdb_Sql(); 267 $beyond_wpdb_sql->data_init(); 268 } 269 } 270 271 /** 272 * @param $input_columns 273 * 274 * @return string 275 */ 276 public function sanitize( $input_columns ) 277 { 278 foreach ( $input_columns as $key => $value ) { 279 $input_columns[$key] = sanitize_text_field( $value ); 280 } 281 return $input_columns; 282 } 283 284 /** 285 * @param $options 286 * create virtual column 287 */ 288 public function create_virtual_column_and_index( $options ) { 289 global $wpdb; 290 291 if ( is_array( $options ) ) { 292 foreach ( $options as $key => $option ) { 293 294 if ( 'postmeta_json' === $key ) { 295 $type = 'post'; 296 } elseif ( 'usermeta_json' === $key ) { 297 $type = 'user'; 298 } else { 299 $type = 'comment'; 300 } 301 302 $table_name = $this->get_json_table_name( $type ); 303 $exist_columns = $this->columns[$type]; 304 $option = explode( PHP_EOL, $option ); 305 306 foreach ( $option as $value ) { 307 308 $value = esc_sql( $value ); 309 310 // If $value already exists, continue 311 if ( in_array( $value, $exist_columns ) ) { 312 continue; 313 } 314 315 $json_key = '$.' . $value; 316 317 // create virtual column 318 $sql = "ALTER TABLE {$table_name} ADD {$value} VARCHAR(255) GENERATED ALWAYS AS ( JSON_UNQUOTE( JSON_EXTRACT( json, '$json_key' ) ) )"; 319 $wpdb->query( $sql ); 320 321 // create index 322 $sql = "ALTER TABLE {$table_name} ADD INDEX ({$value})"; 323 $wpdb->query( $sql ); 324 } 325 } 326 } 327 } 328 329 /** 330 * @param $options 331 * delete virtual columns 332 * @return mixed 333 */ 334 public function delete_virtual_column( $options ) { 335 global $wpdb; 336 337 if ( is_array( $options ) ) { 338 foreach ( $options as $key => $option ) { 339 if ( 'postmeta_json' === $key ) { 340 $type = 'post'; 341 } elseif ( 'usermeta_json' === $key ) { 342 $type = 'user'; 343 } else { 344 $type = 'comment'; 345 } 346 347 $table_name = $this->get_json_table_name( $type ); 348 $exist_columns = $this->columns[$type]; 349 $option = explode( PHP_EOL, $option ); 350 351 foreach ( $exist_columns as $column ) { 352 if ( ! in_array( $column, $option ) ) { 353 $sql = "ALTER TABLE {$table_name} DROP COLUMN {$column}"; 354 $wpdb->query( $sql ); 355 } 356 } 357 } 358 } 359 } 360 361 362 public function get_json_table_name( $type ) { 363 return esc_sql( constant( beyond_wpdb_get_define_table_name( $type ) ) ); 301 array() 302 ); 364 303 } 365 304 … … 369 308 public function print_virtual_column_section_info() 370 309 { 371 print '<p>372 Enter a list metakeys for which you want to create a virtual column.Each metakey should be separated by return enter key.<br>373 The virtual column is indexed and the search uses the virtual column, which makes it faster.374 </p> ';310 $info = __( "When you enter a key of meta information separated by a new line,<br>the specified meta key from the JSON type column is set as a virtual column and the index is pasted.<br>You can further speed up data calls by specifying the most commonly used meta keys for meta queries.", "beyond-wpdb" ); 311 print "<p> 312 {$info} 313 </p>"; 375 314 } 376 315 … … 378 317 * print postmeta json field 379 318 */ 380 public function print_postmeta_json_field() 381 { 382 $value = implode( PHP_EOL, $this->columns['post'] ); 383 printf( 384 '<textarea rows="3" cols="40" id="postmeta_json" name="beyond_wpdb_virtual_column_name[postmeta_json]">%s</textarea>', 385 $value 386 ); 319 public function print_postmeta_json_field( $args ) 320 { 321 $id = $args['id']; 322 $disabled = $this->checkDisabled() ? '' : 'disabled'; 323 print " 324 <textarea class='create_virtualColumns_text-area d-none' rows='3' cols='40' id='{$id}_textarea' name='{$id}' {$disabled}></textarea> 325 <span class='create_{$id} d-none' style='margin-left: 20px;'>Processing...</span> 326 <span class='data-init-input-loading' style='margin-left: 20px;'>Loading...</span> 327 "; 328 387 329 } 388 330 … … 390 332 * print usermeta json field 391 333 */ 392 public function print_usermeta_json_field() 393 { 394 $value = implode( PHP_EOL, $this->columns['user'] ); 395 printf( 396 '<textarea rows="3" cols="40" id="usermeta_json" name="beyond_wpdb_virtual_column_name[usermeta_json]">%s</textarea>', 397 $value 398 ); 334 public function print_usermeta_json_field( $args ) 335 { 336 $id = $args['id']; 337 $disabled = $this->checkDisabled() ? '' : 'disabled'; 338 print " 339 <textarea class='create_virtualColumns_text-area d-none' rows='3' cols='40' id='{$id}_textarea' name='{$id}' {$disabled}></textarea> 340 <span class='create_{$id} d-none' style='margin-left: 20px;'>Processing...</span> 341 <span class='data-init-input-loading' style='margin-left: 20px;'>Loading...</span> 342 "; 399 343 } 400 344 … … 402 346 * print commentmeta json field 403 347 */ 404 public function print_commentmeta_json_field() 405 { 406 $value = implode( PHP_EOL, $this->columns['comment'] ); 407 printf( 408 '<textarea rows="3" cols="40" id="commentmeta_json" name="beyond_wpdb_virtual_column_name[commentmeta_json]">%s</textarea>', 409 $value 410 ); 411 } 412 413 /** 414 * print group concat section info 415 */ 416 public function print_group_concat_section_info() 417 { 418 print '<p>Use GROUP_CONCAT in data init.If the json string consisting of all metas is longer than this value, an error occurs.</p>'; 419 } 420 421 /** 422 * print croup_concat field 423 */ 424 public function print_croup_concat_field() 425 { 426 global $wpdb; 427 $group_concat_max_len = $wpdb->get_results( "show variables like 'group_concat_max_len'" ); 428 printf( 429 '<p>%s</p>', 430 $group_concat_max_len[0]->Value 431 ); 348 public function print_commentmeta_json_field( $args ) 349 { 350 $id = $args['id']; 351 $disabled = $this->checkDisabled() ? '' : 'disabled'; 352 print " 353 <textarea class='create_virtualColumns_text-area d-none' rows='3' cols='40' id='{$id}_textarea' name='{$id}' {$disabled}></textarea> 354 <span class='create_{$id} d-none' style='margin-left: 20px;'>Processing...</span> 355 <span class='data-init-input-loading' style='margin-left: 20px;'>Loading...</span> 356 "; 432 357 } 433 358 … … 437 362 public function print_data_init_section_info() 438 363 { 439 print '<p>Collect all meta information and re-register it in the json table.</p>'; 440 } 441 442 /** 443 * print data_init field 444 */ 445 public function print_data_init_field() 446 { 447 print '<input type="checkbox" id="data_init" name="beyond_wpdb_data_init_name[data_init]" value="1">'; 364 $info = __( "Create and delete your own tables that aggregate the metadata. (Enabled to create, disabled to delete)<br>As long as it is enabled, all meta information will continue to be automatically registered, updated, and deleted in the JSON type columns of the relevant table.<br>When enabled, a new table will be created or initialized, which will take some time. When disabled, the table will be deleted.", "beyond-wpdb" ); 365 print $info; 366 } 367 368 /** 369 * Check to see if each feature is available 370 * @return bool 371 */ 372 public function checkDisabled() { 373 return ! $this->privilege_error; 374 } 375 376 /** 377 * @param $args 378 * print data_init field 379 */ 380 public function print_data_init_field( $args ) 381 { 382 $id = $args['id']; 383 $disabled = $this->checkDisabled() ? '' : 'disabled'; 384 print " 385 <div class='data-init-input-radio d-none'><input type='radio' id='{$id}_active' name='name_{$id}' value='1' {$disabled}>" . "<label for='{$id}_active'>" . __( "activation", "beyond-wpdb" ) . "</label>" . 386 "<input type='radio' id='{$id}_deactive' name='name_{$id}' value='0' {$disabled} style='margin-left: 25px;'>" . "<label for='{$id}_deactive'>" . __( "deactivation", "beyond-wpdb" ) . "</label>" . 387 "<span class='activate_{$id} d-none' style='margin-left: 20px;'>Processing...</span> 388 <span class='success_{$id}' style='margin-left: 20px; display: none;'>Success.</span></div><span class='data-init-input-loading' style='margin-left: 20px;'>Loading...</span> 389 "; 448 390 } 449 391 } -
beyond-wpdb/trunk/class/sql.php
r2330366 r2352101 8 8 */ 9 9 class Beyond_Wpdb_Sql { 10 10 11 function __construct() { 11 12 global $wpdb; 12 13 13 14 foreach( array_keys( BEYOND_WPDB_PRIMARYS ) as $primary ) { 15 $this->triggers[$primary] = array(); 16 14 17 $insert_trigger = 'insert_' . $primary . '_trigger'; 15 18 $this->$insert_trigger = esc_sql( $wpdb->prefix . 'insert_' . $primary . '_trigger' ); 19 array_push( $this->triggers[$primary], $this->$insert_trigger ); 16 20 17 21 $delete_trigger = 'delete_' . $primary . '_trigger'; 18 22 $this->$delete_trigger = esc_sql( $wpdb->prefix . 'delete_' . $primary . '_trigger' ); 23 array_push( $this->triggers[$primary], $this->$delete_trigger ); 19 24 20 25 $insert_meta_trigger = 'insert_' . $primary . 'meta_trigger'; 21 26 $this->$insert_meta_trigger = esc_sql( $wpdb->prefix . 'insert_' . $primary . 'meta_trigger' ); 27 array_push( $this->triggers[$primary], $this->$insert_meta_trigger ); 22 28 23 29 $update_meta_trigger = 'update_' . $primary . 'meta_trigger'; 24 30 $this->$update_meta_trigger = esc_sql( $wpdb->prefix . 'update_' . $primary . 'meta_trigger' ); 31 array_push( $this->triggers[$primary], $this->$update_meta_trigger ); 25 32 26 33 $delete_meta_trigger = 'delete_' . $primary . 'meta_trigger'; 27 34 $this->$delete_meta_trigger = esc_sql( $wpdb->prefix . 'delete_' . $primary . 'meta_trigger' ); 35 array_push( $this->triggers[$primary], $this->$delete_meta_trigger ); 28 36 } 29 37 } … … 32 40 * Create table 33 41 * 34 * @ return void35 * /36 function create_table() : void {37 foreach( BEYOND_WPDB_PRIMARYS as $primary => $values ){38 $this->create_table_sql( $primary, $values['meta_table_key'] );39 }42 * @param string $type 43 * @return void 44 */ 45 function create_table( $primary ) : void { 46 $values = BEYOND_WPDB_PRIMARYS[$primary]; 47 $this->create_table_sql( $primary, $values['meta_table_key'] ); 40 48 } 41 49 … … 66 74 * Drop table 67 75 * 68 * @ return void69 * /70 function drop_table() {71 foreach( array_keys( BEYOND_WPDB_PRIMARYS ) as $primary ) {72 $this->drop_table_sql( $primary );73 }76 * @param $primary 77 * @return void 78 * @throws Exception 79 */ 80 function drop_table( $primary ) { 81 $this->drop_table_sql( $primary ); 74 82 } 75 83 … … 79 87 * @param string $primary 80 88 * @return void 89 * @throws Exception 81 90 */ 82 91 protected function drop_table_sql( $primary ) { 83 92 global $wpdb; 84 $table_name = esc_sql( constant( beyond_wpdb_get_define_table_name( $primary ) ) ); 85 86 $sql = 'DROP TABLE ' . $table_name; 87 $wpdb->query( $sql ); 93 94 $exist_tables = $this->get_exist_json_tables(); 95 $table_name = esc_sql( constant( beyond_wpdb_get_define_table_name( $primary ) ) ); 96 97 if ( in_array( $table_name, $exist_tables ) ) { 98 $sql = 'DROP TABLE ' . $table_name; 99 $wpdb->query( $sql ); 100 101 if ( $wpdb->last_error ) { 102 throw new Exception( $wpdb->last_error ); 103 } 104 } 88 105 } 89 106 … … 93 110 * Insert/Update/Delete postmeta trigger 94 111 * 95 * @ return void96 * /97 function create_trigger() {98 foreach( BEYOND_WPDB_PRIMARYS as $primary => $values ) {99 $this->insert_primary_trigger( $primary, $values['primary_table_name'], $values['primary_table_key'], $values['meta_table_key'] );100 $this->delete_primary_trigger( $primary, $values['primary_table_name'], $values['primary_table_key'], $values['meta_table_key'] );101 $this->insert_meta_trigger( $primary, $values['meta_table_name'], $values['meta_table_key'] );102 $this->update_meta_trigger( $primary, $values['meta_table_name'], $values['meta_table_key'] );103 $this->delete_meta_trigger( $primary, $values['meta_table_name'], $values['meta_table_key'] );104 105 }112 * @param string $primary 113 * @return void 114 * @throws Exception 115 */ 116 function create_trigger( $primary ) { 117 $values = BEYOND_WPDB_PRIMARYS[$primary]; 118 $this->insert_primary_trigger( $primary, $values['primary_table_name'], $values['primary_table_key'], $values['meta_table_key'] ); 119 $this->delete_primary_trigger( $primary, $values['primary_table_name'], $values['primary_table_key'], $values['meta_table_key'] ); 120 $this->insert_meta_trigger( $primary, $values['meta_table_name'], $values['meta_table_key'] ); 121 $this->update_meta_trigger( $primary, $values['meta_table_name'], $values['meta_table_key'] ); 122 $this->delete_meta_trigger( $primary, $values['meta_table_name'], $values['meta_table_key'] ); 106 123 } 107 124 … … 114 131 * @param string $meta_table_key 115 132 * @return void 133 * @throws Exception 116 134 */ 117 135 protected function insert_primary_trigger( $primary, $primaty_table_name, $primary_table_key, $meta_table_key ) { … … 119 137 120 138 $insert_trigger = 'insert_' . $primary . '_trigger'; 139 $exist_triggers = $this->get_exist_triggers(); 121 140 $table_name = esc_sql( constant( beyond_wpdb_get_define_table_name( $primary ) ) ); 122 141 $primaty_table_name = esc_sql( $primaty_table_name ); … … 124 143 $primary_table_key = esc_sql( $primary_table_key ); 125 144 126 $sql = 'CREATE TRIGGER ' . $this->$insert_trigger . ' AFTER 145 if ( ! in_array( $this->$insert_trigger, $exist_triggers ) ) { 146 $sql = 'CREATE TRIGGER ' . $this->$insert_trigger . ' AFTER 127 147 INSERT ON 128 148 ' . $primaty_table_name . ' … … 134 154 VALUES 135 155 (NEW.' . $primary_table_key . ', "{}" )'; 136 $wpdb->query( $sql ); 156 $wpdb->query( $sql ); 157 158 if ( $wpdb->last_error ) { 159 throw new Exception( $wpdb->last_error ); 160 } 161 } 137 162 } 138 163 … … 145 170 * @param string $meta_table_key 146 171 * @return void 172 * @throws Exception 147 173 */ 148 174 protected function delete_primary_trigger( $primary, $primaty_table_name, $primary_table_key, $meta_table_key ) { … … 150 176 151 177 $delete_trigger = 'delete_' . $primary . '_trigger'; 178 $exist_triggers = $this->get_exist_triggers(); 152 179 $table_name = esc_sql( constant( beyond_wpdb_get_define_table_name( $primary ) ) ); 153 180 $primaty_table_name = esc_sql( $primaty_table_name ); … … 155 182 $primary_table_key = esc_sql( $primary_table_key ); 156 183 157 $sql = 'CREATE TRIGGER ' . $this->$delete_trigger . ' BEFORE 158 DELETE ON 159 ' . $primaty_table_name . ' 160 FOR 161 EACH 162 ROW 163 DELETE FROM ' . $table_name . ' 164 WHERE ' . $meta_table_key . ' = OLD.' . $primary_table_key; 165 $wpdb->query( $sql ); 184 if ( ! in_array( $this->$delete_trigger, $exist_triggers ) ) { 185 $sql = 'CREATE TRIGGER ' . $this->$delete_trigger . ' BEFORE 186 DELETE ON 187 ' . $primaty_table_name . ' 188 FOR 189 EACH 190 ROW 191 DELETE FROM ' . $table_name . ' 192 WHERE ' . $meta_table_key . ' = OLD.' . $primary_table_key; 193 $wpdb->query( $sql ); 194 195 if ( $wpdb->last_error ) { 196 throw new Exception( $wpdb->last_error ); 197 } 198 } 166 199 } 167 200 … … 173 206 * @param string $meta_table_key 174 207 * @return void 208 * @throws Exception 175 209 */ 176 210 protected function insert_meta_trigger( $primary, $meta_table_name, $meta_table_key ) { 177 211 global $wpdb; 178 212 213 $exist_triggers = $this->get_exist_triggers(); 179 214 $insert_meta_trigger = 'insert_' . $primary . 'meta_trigger'; 180 215 $table_name = esc_sql( constant( beyond_wpdb_get_define_table_name( $primary ) ) ); … … 182 217 $meta_table_key = esc_sql( $meta_table_key ); 183 218 184 $sql = 'CREATE TRIGGER ' . $this->$insert_meta_trigger . ' AFTER 219 if ( ! in_array( $this->$insert_meta_trigger, $exist_triggers ) ) { 220 $sql = 'CREATE TRIGGER ' . $this->$insert_meta_trigger . ' AFTER 185 221 INSERT ON 186 222 ' . $meta_table_name . ' … … 193 229 (`json`, CONCAT 194 230 ("$.",NEW.meta_key), NEW.meta_value) WHERE ' . $meta_table_key . ' = NEW.'.$meta_table_key; 195 $wpdb->query( $sql ); 231 $wpdb->query( $sql ); 232 233 if ( $wpdb->last_error ) { 234 throw new Exception( $wpdb->last_error ); 235 } 236 } 196 237 } 197 238 … … 203 244 * @param string $meta_table_key 204 245 * @return void 246 * @throws Exception 205 247 */ 206 248 protected function update_meta_trigger( $primary, $meta_table_name, $meta_table_key ) { 207 249 global $wpdb; 208 250 251 $exist_triggers = $this->get_exist_triggers(); 209 252 $update_meta_trigger = 'update_' . $primary . 'meta_trigger'; 210 253 $table_name = esc_sql( constant( beyond_wpdb_get_define_table_name( $primary ) ) ); … … 212 255 $meta_table_key = esc_sql( $meta_table_key ); 213 256 214 $sql = 'CREATE TRIGGER ' . $this->$update_meta_trigger . ' AFTER 257 if ( ! in_array( $this->$update_meta_trigger, $exist_triggers ) ) { 258 $sql = 'CREATE TRIGGER ' . $this->$update_meta_trigger . ' AFTER 215 259 UPDATE ON 216 260 ' . $meta_table_name . ' … … 223 267 (`json`, CONCAT 224 268 ("$.",NEW.meta_key), NEW.meta_value) WHERE ' . $meta_table_key . ' = NEW.' . $meta_table_key; 225 $wpdb->query( $sql ); 269 $wpdb->query( $sql ); 270 271 if ( $wpdb->last_error ) { 272 throw new Exception( $wpdb->last_error ); 273 } 274 } 226 275 } 227 276 … … 233 282 * @param string $meta_table_key 234 283 * @return void 284 * @throws Exception 235 285 */ 236 286 protected function delete_meta_trigger( $primary, $meta_table_name, $meta_table_key ) { 237 287 global $wpdb; 238 288 289 $exist_triggers = $this->get_exist_triggers(); 239 290 $delete_meta_trigger = 'delete_' . $primary . 'meta_trigger'; 240 291 $table_name = esc_sql( constant( beyond_wpdb_get_define_table_name( $primary ) ) ); … … 242 293 $meta_table_key = esc_sql( $meta_table_key ); 243 294 244 $sql = 'CREATE TRIGGER ' . $this->$delete_meta_trigger . ' AFTER 295 if ( ! in_array( $this->$delete_meta_trigger, $exist_triggers ) ) { 296 $sql = 'CREATE TRIGGER ' . $this->$delete_meta_trigger . ' AFTER 245 297 DELETE ON 246 298 ' . $meta_table_name . ' … … 252 304 `json` = JSON_REMOVE(`json`, CONCAT 253 305 ("$.",OLD.meta_key)) WHERE ' . $meta_table_key . ' = OLD.'. $meta_table_key; 306 $wpdb->query( $sql ); 307 308 if ( $wpdb->last_error ) { 309 throw new Exception( $wpdb->last_error ); 310 } 311 } 312 } 313 314 /** 315 * Drop triggers 316 * 317 * @param $primary 318 * @return void 319 * @throws Exception 320 */ 321 function drop_triggers( $primary ) { 322 global $wpdb; 323 324 $exist_triggers = $this->get_exist_triggers(); 325 326 foreach ( $this->triggers[$primary] as $value ) { 327 if ( in_array( $value, $exist_triggers ) ) { 328 $sql = 'DROP TRIGGER ' . esc_sql( $value ); 329 $wpdb->query( $sql ); 330 } 331 } 332 333 if ( $wpdb->last_error ) { 334 throw new Exception( $wpdb->last_error ); 335 } 336 } 337 338 /** 339 * Update table from post, postmeta table. 340 * 341 * @param $primary 342 * 343 * @throws Exception 344 */ 345 function data_init( $primary ) { 346 global $wpdb; 347 $values = BEYOND_WPDB_PRIMARYS[$primary]; 348 $maxlen = 4294967295; 349 350 $maxlen = apply_filters( 'beyond_group_concat_max_len', $maxlen ); 351 $sql = 'SET SESSION group_concat_max_len = ' . $maxlen; 254 352 $wpdb->query( $sql ); 255 } 256 257 /** 258 * Drop triggers 259 * 260 * @return void 261 */ 262 function drop_triggers() { 263 global $wpdb; 264 265 foreach( get_object_vars( $this ) as $value ) { 266 $sql = 'DROP TRIGGER ' . esc_sql( $value ); 267 $wpdb->query( $sql ); 268 } 269 } 270 271 /** 272 * Update table from post, postmeta table. 273 * 274 * @return void 275 */ 276 function data_init() { 277 foreach( BEYOND_WPDB_PRIMARYS as $primary => $values ) { 278 $this->data_init_sql( $primary, $values['primary_table_name'], $values['primary_table_key'], $values['meta_table_name'], $values['meta_table_key'] ); 279 $this->delete_non_existent_data_from_json( $primary, $values['primary_table_name'], $values['primary_table_key'] ); 280 } 353 354 $this->data_init_sql( $primary, $values['primary_table_name'], $values['primary_table_key'], $values['meta_table_name'], $values['meta_table_key'] ); 355 $this->delete_non_existent_data_from_json( $primary, $values['primary_table_name'], $values['primary_table_key'] ); 281 356 } 282 357 … … 284 359 * data init sql 285 360 * 286 * @param string $primary 287 * @param string $primary_table_name 288 * @param string $primary_table_key 289 * @param string $meta_table_name 290 * @param string $meta_table_key 291 * @return void 361 * @param $primary 362 * @param $primary_table_name 363 * @param $primary_table_key 364 * @param $meta_table_name 365 * @param $meta_table_key 366 * 367 * @throws Exception 292 368 */ 293 369 protected function data_init_sql( $primary, $primary_table_name, $primary_table_key, $meta_table_name, $meta_table_key ) { … … 311 387 UPDATE json = VALUES(json)'; 312 388 $wpdb->query( $sql ); 389 390 if ( $wpdb->last_error ) { 391 throw new Exception( $wpdb->last_error ); 392 } 313 393 } 314 394 … … 320 400 * @param $primary_table_key 321 401 * @return void 402 * @throws Exception 322 403 */ 323 404 protected function delete_non_existent_data_from_json( $primary, $primary_table_name, $primary_table_key ) { … … 334 415 335 416 $wpdb->query( $sql ); 417 418 if ( $wpdb->last_error ) { 419 throw new Exception( $wpdb->last_error ); 420 } 421 } 422 423 /** 424 * Get exist virtual columns 425 * @return array 426 */ 427 protected function get_exist_triggers() { 428 $beyond_wpdb_info = new Beyond_Wpdb_Information(); 429 $beyond_wpdb_info->set_triggers(); 430 return $beyond_wpdb_info->get_triggers(); 431 } 432 433 /** 434 * Get exist json tables 435 * @return array 436 */ 437 protected function get_exist_json_tables(){ 438 $beyond_wpdb_info = new Beyond_Wpdb_Information(); 439 $beyond_wpdb_info->set_tables(); 440 return $beyond_wpdb_info->get_tables(); 336 441 } 337 442 } -
beyond-wpdb/trunk/readme.txt
r2337807 r2352101 2 2 Contributors: pressmaninc,kazunao,hiroshisekiguchi,hommakoharu,pmhirotaka 3 3 Tags: pressman,pressmaninc,json,meta_query,wpdb,database,fast,speed,speed up,sql,replace sql 4 Stable tag: 1.1.04 Stable tag: 2.0.0 5 5 License: GPLv2 or later 6 6 License URI: http://www.gnu.org/licenses/gpl-2.0.html … … 10 10 Requires MariaDB: 10.2 11 11 Tested up to: 5.4 12 13 ※ Table names have been changed since v2.0.Please delete the meta_json table and reactivate it in the options screen. 12 14 13 15 Are you having trouble with slow queries due to WordPress table structure? … … 23 25 24 26 == Actual Behavior == 27 * You can create and delete json tables and triggers and data for each json table on the configuration page. 28 * Create virtual columns and indexes for the json table you created in the configuration page. 29 * If you add a virtual column to the json table in the configuration page, the SQL is converted to face the virtual column. 25 30 * Converts the SQL that WordPress auto-generates when using the default APIs of get_posts, Wp_Query, and etc. into a simple SQL that combines the posts/users/comment table with its own meta table. 26 31 * The custom meta table automatically stores data using MySQL and MariaDB triggers. … … 32 37 33 38 == Custom meta tables generated by plugin == 34 * postmeta_json (for posts table) 35 * usermeta_json (for users table) 36 * commentmeta_json (for comment table) 39 The actual table names are prefixed with $table_prefix. 40 * postmeta_beyond (for posts table) 41 * usermeta_beyond (for users table) 42 * commentmeta_beyond (for comment table) 37 43 38 44 == Columns in each table == 39 * postmeta_json 45 The actual table names are prefixed with $table_prefix. 46 * postmeta_beyond 40 47 * post_id(int) 41 48 * json(json) 42 * usermeta_ json49 * usermeta_beyond 43 50 * post_id(int) 44 51 * json(json) 45 * commentmeta_ json52 * commentmeta_beyond 46 53 * post_id(int) 47 54 * json(json) … … 53 60 ex1 ) Create 100 posts and 50 meta data for each post. Combine them 9 times. 54 61 55 * postmeta_ json62 * postmeta_beyond 56 63 * Equal:0.04 57 64 * IN:0.09 … … 61 68 * BETWEEN:0.04 62 69 * NOT BETWEEN:0.04 70 63 71 * postmeta 64 72 * Equal:4.72 … … 70 78 * NOT BETWEEN:0.63 71 79 72 ex2 ) 10000 posts, 1 meta(category) per post.If you add a virtual column and put an index in Category, or if you add a virtual column and put an index in Category 73 * postmeta_json(without virtual column) 80 ex2 ) 10,000 posts, 1 meta per post. meta_key is category. 81 Add a virtual column(category) to postmeta_beyond and put an index, 82 and compare it to the case without adding a virtual column. 83 84 * postmeta_beyond(with virtual column) 85 * Equal:0.02 86 * IN:0.35 87 * BETWEEN:0.12 88 * NOT BETWEEN:0.09 89 90 * postmeta_beyond(without virtual column) 74 91 * Equal:0.15 75 92 * IN:1.92 76 93 * BETWEEN:0.37 77 94 * NOT BETWEEN:0.29 78 79 * postmeta_json(with virtual column)80 * Equal:0.0281 * IN:0.3582 * BETWEEN:0.1283 * NOT BETWEEN:0.0984 95 85 96 == Installation == … … 91 102 * You'll need TRIGGER, SELECT, and UPDATE permissions for the target table and its columns to use the trigger feature of MySQL and MariaDB. 92 103 * It makes changes to SQL only if `suppress_filters` is false. 104 * This plugin sets 4294967295 to group_concat_max_len when it create an original table with JSON type column. You can change the number by using filter 'beyond_group_concat_max_len'.
Note: See TracChangeset
for help on using the changeset viewer.