Plugin Directory

Changeset 1171849


Ignore:
Timestamp:
06/01/2015 12:09:51 PM (11 years ago)
Author:
samface
Message:

version 0.7 with support for wp cli and repair methods

Location:
angry-creative-logger/trunk
Files:
1 added
1 deleted
6 edited

Legend:

Unmodified
Added
Removed
  • angry-creative-logger/trunk/classes/inspector.php

    r1125402 r1171849  
    22/*
    33Class name: AC Inspector
    4 Version: 0.5.1
     4Version: 0.6
    55Author: Sammy Nordström, Angry Creative AB
    66*/
     
    1515        public static $errors = array();
    1616        public static $log_count = 0;
     17        public static $success_count = 0;
     18        public static $error_count = 0;
    1719
    1820        private static $_default_log_path = "";
     
    245247        }
    246248
     249        public function increment_success_count() {
     250
     251            self::$success_count++;
     252
     253        }
     254
     255        public function increment_error_count() {
     256
     257            self::$error_count++;
     258           
     259        }
     260
     261        public function get_success_count() {
     262
     263            return self::$success_count;
     264
     265        }
     266
     267        public function get_error_count() {
     268
     269            return self::$error_count;
     270
     271        }
     272
    247273        /*
    248274            Main log function that does the actual output
    249275        */
    250         public static function log($message, $routine = '', $site_id = '') {
    251 
    252             $log_level = '';
     276        public static function log($message, $routine = '', $args = array() ) {
     277
     278            if ( !is_array( $args ) && is_numeric( $args ) ) {
     279                // For backwards compatibility...
     280                $args = array( 'site_id' => $args );
     281            }
     282
     283            $default_args = array(
     284                'site_id' => get_current_blog_id(),
     285                'site_specific_settings' => false,
     286                'log_level' => 'notice',
     287                'success' => false,
     288                'error' => false
     289            );
    253290
    254291            if (!empty($routine)) {
     
    256293                $routine_options = ACI_Routine_Handler::get_options($routine);
    257294
    258                 if (is_array($routine_options)) {
    259                     if ( $routine_options['site_specific_settings'] && is_multisite() && is_plugin_active_for_network( ACI_PLUGIN_BASENAME ) ) {
     295                $routine_args = wp_parse_args( $routine_options, $default_args );
     296                $args = wp_parse_args( $args, $routine_args );
     297
     298                if ( is_array( $args ) ) {
     299                    if ( $args['site_specific_settings'] && is_multisite() && is_plugin_active_for_network( ACI_PLUGIN_BASENAME ) ) {
    260300                        $site_id = ( is_numeric($site_id) ) ? $site_id : get_current_blog_id();
    261                         if ( is_array($routine_options[$site_id]) && isset($routine_options[$site_id]['log_level']) ) {
    262                             $log_level = $routine_options[$site_id]['log_level'];
    263                         } else if ( is_array($routine_options[1]) && isset($routine_options[1]['log_level']) ) {
    264                             $log_level = $routine_options[1]['log_level'];
     301                        if ( is_array($args[$site_id]) && isset($args[$site_id]['log_level']) ) {
     302                            $log_level = $args[$site_id]['log_level'];
     303                        } else if ( is_array($args[1]) && isset($args[1]['log_level']) ) {
     304                            $log_level = $args[1]['log_level'];
    265305                        }
    266306                    }
    267                     if ( empty($log_level) && isset($routine_options['log_level']) ) {
    268                         $log_level = $routine_options['log_level'];
     307                    if ( empty($log_level) && isset($args['log_level']) ) {
     308                        $log_level = $args['log_level'];
    269309                    }
    270310                }
     311
     312            } else {
     313
     314                $args = wp_parse_args( $args, $default_args );
    271315
    272316            }
     
    286330                    error_log( print_r( $message, true ) . "\n", 3, self::$log_path );
    287331
     332                    if ( defined('WP_CLI') && WP_CLI ) {
     333                        echo $output . "\n";
     334                        print_r( $message );
     335                        echo "\n";
     336                    }
     337
    288338                } else {
    289339
     
    291341                    error_log( $message . "\n", 3, self::$log_path );
    292342
     343                    if ( defined('WP_CLI') && WP_CLI ) {
     344                        echo $message . "\n";
     345                    }
     346
    293347                }
    294348
    295349                self::$log_count++;
    296350
     351                if ( $args['error'] ) {
     352                    self::$error_count++;
     353                }
     354
     355                if ( $args['success'] ) {
     356                    self::$success_count++;
     357                }
     358
    297359            }
    298360
  • angry-creative-logger/trunk/classes/routine_handler.php

    r958566 r1171849  
    22/*
    33Class name: ACI Routine Handler
    4 Version: 0.3.1
     4Version: 0.3.2
    55Depends: AC Inspector 0.5.x
    66Author: Sammy Nordström, Angry Creative AB
     
    1111    class ACI_Routine_Handler {
    1212
     13        private static $force_enabled = array();
    1314        private static $routine_events = array();
    1415
     
    7475        }
    7576
     77        public static function get_repair_method( $routine, $options = array() ) {
     78
     79            if ( empty( $routine ) ) {
     80                return false;
     81            }
     82
     83            if ( !is_array( $options ) || empty( $options ) ) {
     84
     85                $options = self::get_options( $routine );
     86
     87                if ( !is_array( $options ) ) {
     88                    $options = array();
     89                }
     90
     91            }
     92
     93            if ( class_exists( $routine ) ) {
     94
     95                if ( !empty( $options['repair_method'] ) && method_exists( $routine, $options['repair_method'] ) ) {
     96
     97                    return array( $routine, $options['repair_method'] );
     98
     99                } else if ( method_exists( $routine, 'repair' ) ) {
     100
     101                    return array( $routine, 'repair' );
     102
     103                }
     104
     105            }
     106
     107            if ( !empty( $options['repair_method'] ) && function_exists( $options['repair_method'] ) ) {
     108
     109                return $options['repair_method'];
     110
     111            }
     112
     113            return false;
     114
     115        }
     116
    76117        public static function set_options($routine, $args = array()) {
    77118
     
    118159
    119160                        }
     161
     162                        if ( in_array( $routine, self::$force_enabled ) && $options[$site_blog_id]['log_level'] == 'ignore' ) {
     163                            $options[$site_blog_id]['log_level'] = 'notice';
     164                        }
     165
    120166                    }
    121167
    122168                }
    123169
     170            } else {
     171
     172                if ( in_array( $routine, self::$force_enabled ) && $options['log_level'] == 'ignore' ) {
     173                    $options['log_level'] = 'notice';
     174                }
     175
    124176            }
    125177
     
    135187
    136188            $options_key = self::routine_options_key($routine);
     189
     190        }
     191
     192        public static function force_enable( $routine = "" ) {
     193           
     194            if ( !empty( $routine ) ) {
     195                self::$force_enabled = array_merge( self::$force_enabled, array( $routine ) );
     196            }
    137197
    138198        }
  • angry-creative-logger/trunk/plugin.php

    r1129379 r1171849  
    44Plugin URI: http://angrycreative.se
    55Description: Inspects and logs possible issues with your Wordpress installation.
    6 Version: 0.6.1
     6Version: 0.7
    77Author: Robin Björklund, Sammy Nordström, Angry Creative AB
    88*/
    99
    10 define( 'ACI_PLUGIN_VERSION', '0.6.1' );
     10define( 'ACI_PLUGIN_VERSION', '0.7' );
    1111
    1212define( 'ACI_PLUGIN_DIR', dirname( __FILE__ ) );
     
    4646    } else {
    4747        $ac_inspector = new AC_Inspector();
     48        if ( defined('WP_CLI') && WP_CLI ) {
     49            require_once( dirname( __FILE__ ) . '/classes/wp_cli.php' );
     50        }
    4851    }
    4952
  • angry-creative-logger/trunk/readme.txt

    r1129379 r1171849  
    11=== Angry Creative Inspector ===
    2 Contributors: ac-robin, samface, angrycreative 
     2Contributors: ac-robin, samface, angrycreative
    33Tags: inspect, inspection, monitor, monitoring, log, logging, check, checking, validate, validation, permissions, install, installation
    44Requires at least: 4.0
    5 Tested up to: 4.1.1
    6 Stable tag: 0.6.1
     5Tested up to: 4.2.2
     6Stable tag: 0.7
    77License: GPLv2 or later
    88License URI: http://www.gnu.org/licenses/gpl-2.0.html
     
    2121
    2222== Changelog ==
     23
     24= 0.7.x =
     25* Added repair method support for inspection routines
     26* Added WP CLI support for operating the inspector via command line
     27* Added inspect command for doing inspections via command line
     28* Added repair command for doing repairs via command line
     29* Added repair method to mysql database collations routine
    2330
    2431= 0.6.x =
  • angry-creative-logger/trunk/routines/db_collations.php

    r1125423 r1171849  
    2828        global $wpdb;
    2929
    30         $blog_charset = strtolower( str_replace( '-', '', get_option('blog_charset') ) );
    31         $locale = get_locale();
    32        
    33         require_once( ABSPATH . 'wp-admin/includes/translation-install.php' );
    34         $translations = wp_get_available_translations();
    35 
    36         $language = 'general';
    37 
    38         if ( 'en_US' != $language && isset( $translations[$locale] ) ) {
    39             $language = strtolower( $translations[$locale]['english_name'] );
    40         }
    41 
    42         $proper_tbl_collation =  $blog_charset . '_' . $language . '_ci';
    43 
    44         $default_tbl_collation = $wpdb->get_var( "SELECT DEFAULT_COLLATION_NAME
    45                                                   FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = '".DB_NAME."'" );
    46 
    47         if ( $proper_tbl_collation != $default_tbl_collation ) {
    48             AC_Inspector::log( "Default table collation is $default_tbl_collation (should be $proper_tbl_collation).", __CLASS__ );
     30        $proper_db_collation =  self::get_proper_db_collation();
     31
     32        $default_db_collation = $wpdb->get_var( "SELECT DEFAULT_COLLATION_NAME
     33                                                 FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = '".DB_NAME."'" );
     34
     35        if ( $proper_db_collation != $default_db_collation ) {
     36            AC_Inspector::log( "Default table collation is $default_db_collation (should be $proper_db_collation).", __CLASS__ );
    4937        }
     38
     39        list( $proper_charset ) = explode( '_', $proper_db_collation );
     40
     41        $tbl_collation_queries = self::get_table_collation_queries();
     42
     43        if ( is_array( $tbl_collation_queries ) && count( $tbl_collation_queries ) > 0 ) {
     44
     45            foreach( $tbl_collation_queries as $tbl_collation_query ) {
     46
     47                $tbl_collation_data = $wpdb->get_row( $tbl_collation_query );
     48                $tbl_name = $tbl_collation_data->TABLE_NAME;
     49                $tbl_collation = $tbl_collation_data->TABLE_COLLATION;
     50
     51                if ( $proper_db_collation != $tbl_collation ) {
     52                    AC_Inspector::log( "Table collation for $tbl_name is $tbl_collation (should be $proper_db_collation).", __CLASS__ );
     53                }
     54
     55                $tbl_columns = $wpdb->get_results( "SHOW FULL COLUMNS FROM `$tbl_name`" );
     56                if ( ! $tbl_columns ) {
     57                    AC_Inspector::log( "Unable to determine column collations for table $tbl_name.", __CLASS__ );
     58                    continue;
     59                }
     60
     61                foreach ( $tbl_columns as $column ) {
     62                    if ( $column->Collation ) {
     63                        if ( $proper_db_collation !== $column->Collation ) {
     64                            AC_Inspector::log( "Column collation for {$column->Field} in $tbl_name is {$column->Collation} (should be $proper_db_collation).", __CLASS__ );
     65                        }
     66                    }
     67                }
     68
     69            }
     70
     71        }
     72
     73        return;
     74
     75    }
     76
     77    public static function repair() {
     78
     79        global $wpdb;
     80
     81        $proper_db_collation =  self::get_proper_db_collation();
     82
     83        $default_db_collation = $wpdb->get_var( "SELECT DEFAULT_COLLATION_NAME
     84                                                 FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = '".DB_NAME."'" );
     85
     86        list( $proper_charset ) = explode( '_', $proper_db_collation );
     87
     88        if ( $proper_db_collation != $default_db_collation ) {
     89            if ( $wpdb->query( $wpdb->prepare( "ALTER DATABASE ".DB_NAME." CHARACTER SET %s COLLATE %s", $proper_charset, $proper_db_collation ) ) ) {
     90                AC_Inspector::log( "Converted default table collation from $default_db_collation to $proper_db_collation.", __CLASS__, array( 'success' => true ) );
     91            } else {
     92                AC_Inspector::log( "Failed to convert default table collation from $default_db_collation to $proper_db_collation!", __CLASS__ , array( 'error' => true ));
     93            }
     94        }
     95
     96        $tbl_collation_queries = self::get_table_collation_queries();
     97
     98        if ( is_array( $tbl_collation_queries ) && count( $tbl_collation_queries ) > 0 ) {
     99
     100            foreach( $tbl_collation_queries as $tbl_collation_query ) {
     101
     102                $tbl_collation_data = $wpdb->get_row( $tbl_collation_query );
     103                $tbl_name = $tbl_collation_data->TABLE_NAME;
     104                $tbl_collation = $tbl_collation_data->TABLE_COLLATION;
     105
     106                if ( $proper_db_collation != $tbl_collation ) {
     107                    if ( $wpdb->query( $wpdb->prepare( "ALTER TABLE ".$tbl_name." CONVERT TO CHARACTER SET %s COLLATE %s", $proper_charset, $proper_db_collation ) ) ) {
     108                        AC_Inspector::log( "Converted collation for $tbl_name from $tbl_collation to $proper_db_collation.", __CLASS__, array( 'success' => true ) );
     109                    } else {
     110                        AC_Inspector::log( "Failed to convert collation for $tbl_name from $tbl_collation to $proper_db_collation.", __CLASS__, array( 'error' => true ) );
     111                    }
     112                    continue;
     113                }
     114
     115                $tbl_columns = $wpdb->get_results( "SHOW FULL COLUMNS FROM `$tbl_name`" );
     116                if ( ! $tbl_columns ) {
     117                    AC_Inspector::log( "Unable to determine column collations for table $tbl_name.", __CLASS__, array( 'error' => true ) );
     118                    continue;
     119                }
     120
     121                foreach ( $tbl_columns as $column ) {
     122                    if ( $column->Collation ) {
     123                        if ( $proper_db_collation !== $column->Collation ) {
     124                            if ( $wpdb->query( $wpdb->prepare( "ALTER TABLE ".$tbl_name." CONVERT TO CHARACTER SET %s COLLATE %s", $proper_charset, $proper_db_collation ) ) ) {
     125                                AC_Inspector::log( "Converted collation for $tbl_name from $tbl_collation to $proper_db_collation.", __CLASS__, array( 'success' => true ) );
     126                            } else {
     127                                AC_Inspector::log( "Failed to convert collation for $tbl_name from $tbl_collation to $proper_db_collation.", __CLASS__, array( 'error' => true ) );
     128                            }
     129                            break;
     130                        }
     131                    }
     132                }
     133
     134            }
     135
     136        }
     137
     138        return;
     139
     140    }
     141
     142    private static function get_proper_db_collation() {
     143
     144        global $wp_version;
     145
     146        if ( $wp_version >= 4.2 ) {
     147
     148            $blog_charset = 'utf8mb4';
     149            $language = 'unicode';
     150
     151        } else {
     152
     153            $blog_charset = strtolower( str_replace( '-', '', get_option('blog_charset') ) );
     154
     155            $language = 'general';
     156
     157            require_once( ABSPATH . 'wp-admin/includes/translation-install.php' );
     158            $translations = wp_get_available_translations();
     159            $locale = get_locale();
     160
     161            if ( 'en_US' != $locale && isset( $translations[$locale] ) ) {
     162                $language = strtolower( $translations[$locale]['english_name'] );
     163            }
     164
     165        }
     166
     167        return $blog_charset . '_' . $language . '_ci';
     168
     169    }
     170
     171    private static function get_table_collation_queries() {
     172
     173        global $wpdb;
    50174
    51175        $tbl_collation_queries_query = "SELECT  CONCAT('SELECT TABLE_NAME, TABLE_COLLATION FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = \'', table_name, '\';') AS sql_statements
     
    55179                                        ORDER BY table_name DESC";
    56180
    57         $tbl_collation_queries = $wpdb->get_col( $tbl_collation_queries_query  );
    58 
    59         if ( is_array( $tbl_collation_queries ) && count( $tbl_collation_queries ) > 0 ) {
    60 
    61             foreach( $tbl_collation_queries as $tbl_collation_query ) {
    62 
    63                 $tbl_collation_data = $wpdb->get_row( $tbl_collation_query );
    64                 $tbl_name = $tbl_collation_data->TABLE_NAME;
    65                 $tbl_collation = $tbl_collation_data->TABLE_COLLATION;
    66 
    67                 if ( $proper_tbl_collation != $tbl_collation ) {
    68                     AC_Inspector::log( "Table collation for $tbl_name is $tbl_collation (should be $proper_tbl_collation).", __CLASS__ );
    69                 }
    70 
    71             }
    72 
    73         }
    74 
    75         return;
     181        return $wpdb->get_col( $tbl_collation_queries_query  );
    76182
    77183    }
  • angry-creative-logger/trunk/routines/site_visibility.php

    r943414 r1171849  
    3737                        if ( !$visible ) {
    3838
    39                             AC_Inspector::log( 'Site '.$site_blog_id.' is not visible to search engines.', __CLASS__, $site_blog_id );
     39                            AC_Inspector::log( 'Site '.$site_blog_id.' is not visible to search engines.', __CLASS__, array( 'site_id' => $site_blog_id ) );
    4040
    4141                        }
Note: See TracChangeset for help on using the changeset viewer.