Changeset 2118155
- Timestamp:
- 07/05/2019 02:13:15 PM (7 years ago)
- Location:
- database-operations/trunk
- Files:
-
- 2 edited
-
db_op.php (modified) (2 diffs)
-
readme.txt (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
database-operations/trunk/db_op.php
r2014412 r2118155 3 3 Plugin Name: Database operations 4 4 Plugin URI: https://antechncom.wordpress.com/ 5 Description: A database manager for various MY_SQL database operations6 Version: 1. 07 Author: Bowale Joseph5 Description: This plugin connects Wordpress and the MY_SQL database, thus allowing a user fetch data from the MY_SQL database, a user can display a table or perform a simple 'UNION' on two tables 6 Version: 1.5.0 7 Author: Antechn 8 8 Author URI: https://antechncom.wordpress.com/about-us/ 9 9 License: GPLv2 10 Text Domain: Database-operations 11 Domain Path: /languages 10 12 */ 11 13 /* Copyright 2019 Bowale Joseph (email : [email protected]) … … 22 24 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 23 25 */ 24 // Call function when plugin is activated25 register_activation_hook( __FILE__, 'db_op_install' );26 26 27 // function to pull available database from db 28 function db_op_retrieve_dbs(){ 29 global $wpdb; 30 $mytables=$wpdb->get_results("SHOW TABLES"); 31 return $mytables; 27 // If this file is called directly, abort. 28 if ( ! defined( 'WPINC' ) ) { 29 die; 32 30 } 33 31 34 function db_op_install() { 35 $db_op_retrieve_dbs = db_op_retrieve_dbs(); 36 37 //setup default option values 38 $db_op_options_arr = array( 39 'db_op_first_tb' => $db_op_retrieve_dbs, 40 'db_op_second_tb' => $db_op_retrieve_dbs, 41 'db_op_sel_first_tb' => "", 42 'db_op_sel_second_tb' => "", 43 'db_op_sel_query_op' => "", 44 'db_op_query_op' => array( 'UNION' ) 45 ); 46 //save our default option values 47 update_option( 'db_op_options', $db_op_options_arr ); 48 32 /** 33 * The code that runs during plugin activation. 34 * This action is documented in includes/class-database-operations-activator.php 35 */ 36 function activate_database_operations() { 37 require_once plugin_dir_path( __FILE__ ) . 'includes/class-database-operations-activator.php'; 38 Database_Operations_Activator::activate(); 49 39 } 50 40 51 add_action( 'admin_menu', 'db_op_reg_custom_menu_page' ); 52 function db_op_reg_custom_menu_page() { 53 // add_menu_page( $page_title, $menu_title, $capability, $menu_slug, $function, $icon_url, $position ); 54 add_menu_page( 'Db Op', 'Db Op', 'manage_options', 'db_op.php', 'db_op_menu_page', 'dashicons-vault', 90 ); 41 /** 42 * The code that runs during plugin deactivation. 43 * This action is documented in includes/class-database-operations-deactivator.php 44 */ 45 function deactivate_database_operations() { 46 require_once plugin_dir_path( __FILE__ ) . 'includes/class-database-operations-deactivator.php'; 47 Database_Operations_Deactivator::deactivate(); 55 48 } 56 49 57 //Called upon saving the configuration on the settings page. Manupulates submitted post contents 58 function db_op_parse_posted_content(){ 59 global $wpdb; 60 $mytables; 61 $db_op_tb1 = sanitize_text_field($_POST['tb1']); 62 $db_op_tb2 = sanitize_text_field($_POST['tb2']); 63 64 //If submitted post content is empty 65 if ( ! empty( $_POST ) && current_user_can('edit_posts') ) { 66 //check nonce for security 67 check_admin_referer( 'db_op_menu-save', 'db_op-plugin' ); 68 69 if($db_op_tb1 == "" && $db_op_tb2 == ""){ 70 ?><div><blockquote><?php echo 'Please select at least one table'; ?> </div></blockquote> 71 <?php } 72 73 else if($db_op_tb1 == "" && $db_op_tb2 != ""){ 74 ?><div><table class="widefat fixed" > <h3> Displaying query information of table <?php echo esc_attr($db_op_tb2) ?></h3><thead class="thead-light"><tr class="table-info"> 75 <?php 76 $db_op_sql = " 77 SELECT DISTINCT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME ='{$db_op_tb2}'"; 78 $db_op_cols = $wpdb->get_col($db_op_sql); 79 foreach ($db_op_cols as $t) 80 { 81 ?><th scope="col"><?php echo esc_attr($t) ?> </th><?php 82 } 83 ?></tr></thead><tbody><tr><?php 84 $db_op_sql = " 85 SELECT * FROM $db_op_tb2"; 86 $mytables = $wpdb->get_results($db_op_sql); 87 foreach ( $mytables as $mytable ) 88 { 89 ?><tr><?php 90 foreach ($mytable as $t) 91 { 92 ?><td class="column-columnname"> <?php echo esc_attr($t); ?></td><?php 93 }?></tr><?php 94 } 95 ?></h2> </tbody></table><?php 96 97 }else if ($db_op_tb2 == "" && $db_op_tb1 != ""){ 98 ?><div><table class="widefat fixed" > <h3> Displaying query information of table <?php echo esc_attr($db_op_tb1) ?></h3><thead class="thead-light"><tr class="table-info"> 99 <?php 100 $sql = " 101 SELECT DISTINCT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME ='{$db_op_tb1}'"; 102 $db_op_cols = $wpdb->get_col($sql); 103 foreach ($db_op_cols as $t) 104 { 105 ?><th scope="col"><?php echo esc_attr($t) ?> </th><?php 106 } 107 ?></tr></thead><tbody><tr><?php 108 $sql = " 109 SELECT * FROM $db_op_tb1"; 110 $mytables = $wpdb->get_results($sql); 111 foreach ( $mytables as $mytable ) 112 { 113 ?><tr><?php 114 foreach ($mytable as $t) 115 { 116 ?><td class="column-columnname"> <?php echo esc_attr($t); ?></td><?php 117 }?></tr><?php 118 } 119 ?></h2> </tbody></table><?php 120 } else { 121 $qry = sanitize_text_field($_POST['qryop']); 122 $repl = str_replace('wp_', '', $db_op_tb1); 123 $repl1 = str_replace('wp_', '', $db_op_tb2); 124 $query = " 125 SELECT * 126 FROM {$wpdb->$repl} {$qry} 127 SELECT * 128 FROM {$wpdb->$repl1}"; 50 register_activation_hook( __FILE__, 'activate_database_operations' ); 51 register_deactivation_hook( __FILE__, 'deactivate_database_operations' ); 129 52 130 $mytables = $wpdb->get_results($query); 131 ?> <div><table id="greentable" class="widefat fixed"> <thead> 132 <h1> 133 <?php echo esc_attr($qry). " of " . esc_attr($db_op_tb1). " and " . esc_attr($db_op_tb2). '<br>'; 134 ?><tbody> 135 </h1></thead> 136 <h2><?php 137 if (empty($mytables)) { 138 echo '<h2>To use this UNION clause, each SELECTED TABLE must have</br> 139 140 - The same number of columns selected</br> 141 - The same number of column expressions</br> 142 - The same data type and</br> 143 - Have them in the same order</br> 144 But they need not have to be in the same length.</h2>'; 145 } 146 else { 147 foreach ( $mytables as $mytable ) 148 { 149 echo '<tr>'; 150 foreach ($mytable as $t) 151 { 152 echo '<td class="column-columnname">'. esc_attr($t). '</td>'; 153 } 154 echo '</tr>'; 155 } 156 } 157 ?></h2> </tbody></table><?php 158 } 159 } 160 //setup default option values 161 $db_op_options_arr = array( 162 'db_op_sel_first_tb' => $db_op_tb1, 163 'db_op_sel_second_tb' => $db_op_tb2, 164 'db_op_sel_query_op' => $qry, 165 'mytables' => $mytables, 166 'cols' => $db_op_cols, 167 'db_op_query_op' => array( 'UNION') 168 ); 169 //save our default option values 170 update_option( 'db_op_options', $db_op_options_arr ); 171 ?><div> 172 <h2>Comfortable with the results, please use with shortcode '[do]' on any page or as a widget display.</h2></br> 173 <?php 174 return $mytables; 53 /** 54 * The core plugin class that is used to define internationalization, 55 * admin-specific hooks, and public-facing site hooks. 56 */ 57 require plugin_dir_path( __FILE__ ) . 'includes/class-database-operations.php'; 58 59 /** 60 * Begins execution of the plugin. 61 * 62 * Since everything within the plugin is registered via hooks, 63 * then kicking off the plugin from this point in the file does 64 * not affect the page life cycle. 65 * 66 * @since 1.5.0 67 */ 68 function run_database_operations() { 69 70 $plugin = new Database_Operations(); 71 $plugin->run(); 72 175 73 } 176 function db_op_menu_page(){ 177 //set the option array values to variables 178 $db_op_first_tb = db_op_retrieve_dbs(); 179 $db_op_second_tb = db_op_retrieve_dbs(); 180 $db_op_query_op = array( 'UNION'); 181 ?> 182 <div class="wrap"> 183 <h2><?php _e( 'Database operations options', 'db_op_plugin' ) ?></h2> 184 <h3> Select at least one table </h3> 185 <form method="post" action="<?php echo get_permalink(); ?>"> 186 187 <?php settings_fields( 'db_op-settings-group' ); 188 189 //nonce field for security 190 wp_nonce_field( 'db_op_menu-save', 'db_op-plugin' ); 191 192 ?> 193 <table class="form-table"> 194 <tr valign="top"> 195 <th scope="row"><?php _e( 'Current Database', 'db_op_plugin' ) ?></th> 196 <td><h4><?php echo esc_attr( $site_title ); ?> </h4></td> 197 </tr> 198 <tr valign="top"> 199 <th scope="row"><?php _e( 'Select first database table', 'db_op_plugin' ) ?></th> 200 <td> 201 <select name="tb1" > 202 <option value="" > None </option> 203 <?php 204 foreach ($db_op_first_tb as $mytable) 205 { 206 foreach ($mytable as $t) 207 { ?> 208 <option value="<?php echo esc_attr( $t); ?>" > <?php echo esc_attr( $t ); ?> </option> 209 <?php } 210 } 211 ?> 212 </select> 213 </td> 214 <th scope="row"><?php _e( 'Select second database table', 'db_op_plugin' ) ?></th> 215 <td> 216 <select name="tb2" > 217 <option value="" > None </option> 218 <?php 219 foreach ($db_op_second_tb as $mytable) 220 { 221 foreach ($mytable as $t) 222 { ?> 223 <option value="<?php echo esc_attr( $t); ?>" > <?php echo esc_attr( $t ); ?> </option> 224 <?php } 225 } 226 ?> 227 </select> 228 </td> 229 </tr> 230 <tr valign="top"> 231 <th scope="row"><?php _e( 'Select database query option', 'db_op_plugin' ) ?></th> 232 <td> 233 <select name="qryop" ><?php 234 foreach ($db_op_query_op as $mytable) 235 { 236 ?> 237 <option value="<?php echo $mytable; ?>" > <?php echo esc_attr( $mytable ); ?> </option> 238 <?php 239 } 240 ?> 241 </select> 242 </td> 243 </tr> 244 245 <tr valign="bottom"> 246 <td colspan="2" style="align:right;"> 247 248 249 </td> 250 </tr> 251 </table> 252 253 <p class="submit"> 254 <input type="submit" class="button-primary" value="<?php _e( 'Perform set query', 'db_op_plugin' ); ?>" /> 255 </p> 256 257 </form> 258 259 </div> 260 <?php 261 db_op_parse_posted_content(); 262 } 263 264 add_action( 'admin_init', 'db_op_register_settings' ); 265 function db_op_register_settings() { 266 //register the array of settings 267 register_setting( 'db_op-settings-group', 'db_op_options', 'db_op_sanitize_options' ); 268 } 269 function db_op_sanitize_options( $options ) { 270 $options['db_op_sel_first_tb'] = ( ! empty( $options['db_op_sel_first_tb'] ) ) ? sanitize_text_field( $options['db_op_sel_first_tb'] ) : ''; 271 $options['db_op_sel_second_tb'] = ( ! empty( $options['db_op_sel_second_tb'] ) ) ? sanitize_text_field( $options['db_op_sel_second_tb'] ) : ''; 272 $options['db_op_sel_query_op'] = ( ! empty( $options['db_op_sel_query_op'] ) ) ? sanitize_text_field( $options['db_op_sel_query_op'] ) : ''; 273 $options['db_op_first_tb'] = ( ! empty( $options['db_op_first_tb'] ) ) ? sanitize_text_field( $options['db_op_first_tb'] ) : ''; 274 $options['db_op_second_tb'] = ( ! empty( $options['db_op_second_tb'] ) ) ? sanitize_text_field( $options['db_op_second_tb'] ) : ''; 275 $options['db_op_query_op'] = ( ! empty( $options['db_op_query_op'] ) ) ? sanitize_text_field( $options['db_op_query_op'] ) : ''; 276 return $options; 277 } 278 //perforn query operation 279 function perform_query( ) { 280 global $wpdp; 281 282 //load options array 283 $db_op_options_arr = get_option( 'db_op_options' ); 284 $mytables = $db_op_options_arr['mytables']; 285 286 return $mytables; 287 } 288 289 // Action hook to create the products shortcode 290 add_shortcode( 'do', 'db_op_shortcode' ); 291 //create shortcode 292 function db_op_shortcode( $atts, $content = null ) { 293 global $wpdp; 294 //load options array 295 $db_op_options_arr = get_option( 'db_op_options' ); 296 $cols = $db_op_options_arr['cols']; 297 $mytables = perform_query(); 298 $res = '<div><table class="table table-hover" > <h3> Displaying query information of table(s) ' . $db_op_options_arr['db_op_sel_first_tb'] . ' / ' . $db_op_options_arr['db_op_sel_second_tb'].'</h3><thead class="thead-light"><tr class="table-info">'; 299 if($cols != ""){ 300 foreach ($cols as $t) 301 { 302 $res .= '<th scope="col">'.$t.'</th>'; 303 } 304 } 305 $res .= '</tr></thead>'; 306 foreach ( $mytables as $mytable ) 307 { 308 $res .= '<tbody> <tr class="alternate">'; 309 foreach ($mytable as $t) 310 { 311 $res .= '<td class="column-columnname"> '.$t. '</td> '; 312 } 313 //$res .= '</br>'; 314 $res .= '</tbody> </tr>'; 315 } 316 $res .= '</table></div>'; 317 318 return $res; 319 } 320 // Action hook to create plugin widget 321 add_action( 'widgets_init', 'db_op_register_widgets' ); 322 //register the widget 323 function db_op_register_widgets() { 324 325 register_widget( 'db_op_widget' ); 326 327 } 328 //db_op_widget class 329 class db_op_widget extends WP_Widget { 330 //process our new widget 331 function db_op_widget() { 332 333 $widget_ops = array( 334 'classname' => 'db_op-widget-class', 335 'description' => __( 'Display Query results','db_op_plugin' ) ); 336 $this->WP_Widget( 'db_op_widget', __( 'Database Operations Widget','db_op_plugin'), $widget_ops ); 337 338 } 339 //build our widget settings form 340 function form( $instance ) { 341 342 $defaults = array( 343 'title' => __( 'Query results', 'db_op_plugin' )); 344 345 $instance = wp_parse_args( (array) $instance, $defaults ); 346 $title = $instance['title']; 347 ?> 348 <p><?php _e('Title', 'db_op_plugin') ?>: 349 <input class="widefat" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" /></p> 350 351 <?php 352 } 353 //save our widget settings 354 function update( $new_instance, $old_instance ) { 355 356 $instance = $old_instance; 357 $instance['title'] = sanitize_text_field( $new_instance['title'] ); 358 return $instance; 359 360 } 361 //display our widget 362 function widget( $args, $instance ) { 363 global $post; 364 365 extract( $args ); 366 echo $before_widget; 367 $title = apply_filters( 'widget_title', $instance['title'] ); 368 if ( ! empty( $title ) ) { echo $before_title . esc_html( $title ) . $after_title; }; 369 $db_op_options_arr = get_option( 'db_op_options' ); 370 ?> 371 <p> 372 <a href="<?php the_permalink(); ?>" rel="bookmark" title="<?php the_title_attribute(); ?> Query Information"> 373 <?php the_title(); ?> 374 </a> 375 </p> 376 <?php 377 378 $mytables = perform_query(); 379 $cols = $db_op_options_arr['cols']; 380 $mytables = perform_query(); 381 $res = '<div><table class="table table-hover" > <h3> Displaying query information of table(s) ' . $db_op_options_arr['db_op_sel_first_tb'] . ' / ' . $db_op_options_arr['db_op_sel_second_tb'].'</h3><thead class="thead-light"><tr class="table-info">'; 382 if($cols != ""){ 383 foreach ($cols as $t) 384 { 385 $res .= '<th scope="col">'.$t.'</th>'; 386 } 387 } 388 $res .= '</tr></thead>'; 389 foreach ( $mytables as $mytable ) 390 { 391 $res .= '<tbody> <tr class="alternate">'; 392 foreach ($mytable as $t) 393 { 394 $res .= '<td class="column-columnname"> '.$t. '</td> '; 395 } 396 //$res .= '</br>'; 397 $res .= '</tbody> </tr>'; 398 } 399 $res .= '</table></div>'; 400 401 echo $res; 402 echo '<hr>'; 403 echo $after_widget; 404 405 } 406 407 } 74 run_database_operations(); -
database-operations/trunk/readme.txt
r2014412 r2118155 4 4 Tags: database, mysql, query, db, database operations, database management, phpmyadmin 5 5 Requires at least: 4.6 6 Tested up to: 4.77 Stable tag: 4.36 Tested up to: 5.2 7 Stable tag: 1.5.0 8 8 Requires PHP: 5.2.4 9 9 License: GPLv2 or later … … 55 55 = 1.0.0 = 56 56 * Need no action 57 58 = 1.5.0 = 59 * Created Custom Post types To Store More Query Shortcodes 60 61 62
Note: See TracChangeset
for help on using the changeset viewer.