Plugin Directory

Changeset 785503


Ignore:
Timestamp:
10/09/2013 07:25:43 PM (12 years ago)
Author:
anukit
Message:

Added nicer logging showing the differences between array values.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • observer/trunk/lib/AMWObserver.php

    r782634 r785503  
    44
    55    protected $logfile;
    6     protected $version = '1.0.2';
     6    protected $version = '1.0.3';
    77
    88    /**
     
    9494        // ignore transients
    9595        if (preg_match('/.*_transient_.*/i', $option)) return;
    96         // convert values to strings
    97         $oldvalue = (is_array($oldvalue) || is_object($oldvalue)) ? serialize($oldvalue) : $oldvalue;
    98         $newvalue = (is_array($newvalue) || is_object($newvalue)) ? serialize($newvalue) : $newvalue;
    99         $data = sprintf("(%s)->(%s)", $oldvalue, $newvalue);
    100         $site = get_current_site();
    101         $this->log('update_site_option', $site->id, $option, $data);
     96
     97        // handle this option separately?
     98        $optionsWithArrayValues = array(
     99            'site_admins',
     100            'active_sitewide_plugins',
     101        );
     102
     103        // log
     104        //if (in_array($option, $optionsWithArrayValues)) {
     105        if (is_array($oldvalue) || is_array($newvalue)) {
     106
     107            $this->handle_option_arrays('update_site_option', $option, $oldvalue, $newvalue);
     108
     109        } else {
     110
     111            // convert values to strings
     112            $oldvalue = (is_array($oldvalue) || is_object($oldvalue)) ? serialize($oldvalue) : $oldvalue;
     113            $newvalue = (is_array($newvalue) || is_object($newvalue)) ? serialize($newvalue) : $newvalue;
     114            $data = sprintf("(%s)->(%s)", $oldvalue, $newvalue);
     115            $site = get_current_site();
     116            $this->log('update_site_option', $site->id, $option, $data);
     117
     118        }
    102119    }
    103120
     
    123140
    124141        // handle this option separately?
    125         $specialHandler = 'handle_option_' . $option;
    126         if (method_exists($this, $specialHandler)) {
    127 
    128             call_user_func(array(&$this, $specialHandler), $option, $oldvalue, $newvalue);
     142        $optionsWithArrayValues = array(
     143            'recently_edited',
     144        );
     145
     146        //if (in_array($option, $optionsWithArrayValues)) {
     147        if (is_array($oldvalue) || is_array($newvalue)) {
     148
     149            $this->handle_option_arrays('updated_option', $option, $oldvalue, $newvalue);
    129150
    130151        } else {
     
    142163
    143164    /**
    144      * Logs when 'active_plugins' option is updated.
     165     * Logs the difference in options that store serialized arrays.
     166     * Makes the differences easier to spot and read.
     167     *
     168     * @param  string $action   The triggered action.
    145169     * @param  string $option   The name of the option that was changed.
    146170     * @param  mixed  $newvalue The new value for the option.
     
    148172     * @return void
    149173     */
    150     public function handle_option_active_plugins($option, $oldvalue, $newvalue) {
     174    public function handle_option_arrays($action, $option, $oldvalue, $newvalue) {
    151175
    152176        // make sure we work with arrays
     
    155179
    156180        // get changes
    157         $deactivated = array_diff($oldvalue, $newvalue);
    158         $activated   = array_diff($newvalue, $oldvalue);
     181        if ($this->is_assoc($oldvalue) || $this->is_assoc($newvalue)) {
     182            $deactivated = array_diff_assoc($oldvalue, $newvalue);
     183            $activated   = array_diff_assoc($newvalue, $oldvalue);
     184        } else {
     185            $deactivated = array_diff($oldvalue, $newvalue);
     186            $activated   = array_diff($newvalue, $oldvalue);
     187        }
    159188
    160189        // make a readable representation of the changes
    161190        $data = '';
    162191        if (count($deactivated)) {
    163             $data .= '(DEACTIVATED: "' . implode('","', $deactivated) . '")';
     192            $withKeys = array();
     193            foreach ($deactivated as $k=>$v) {
     194                $withKeys[] = $k.' => '.$v;
     195            }
     196            $deactivatedStr = '"' . implode('","', $withKeys) . '"';
     197            $data .= '(removed: '.$deactivatedStr.')';
    164198        }
    165199        if (count($activated)) {
    166             $data .= '(ACTIVATED: "' . implode('","', $activated) . '")';
     200            $withKeys = array();
     201            foreach ($activated as $k=>$v) {
     202                $withKeys[] = $k.' => '.$v;
     203            }
     204            $activatedStr = '"' . implode('","', $withKeys) . '"';
     205            $data .= '(added: '.$activatedStr.')';
    167206        }
    168207
    169208        // log it
    170209        $blogId = get_current_blog_id();
    171         $this->log('updated_option', $blogId, $option, $data);
    172 
     210        $this->log($action, $blogId, $option, $data);
     211
     212    }
     213
     214    /**
     215     * Returns true if the given array is associative.
     216     * @param  array  $array The array to check.
     217     * @return boolean
     218     */
     219    private function is_assoc($array) {
     220        return (bool)count(array_filter(array_keys($array), 'is_string'));
    173221    }
    174222
Note: See TracChangeset for help on using the changeset viewer.