Esempio n. 1
0
 /**
  * Return a list of modified themes since the last sync.
  *
  * Note:
  *  There's no point to store a themes counter since even if the number of
  *  themes didn't change, we still need to check if the versions are all the
  *  same and the activity state is similar.
  *
  * @author Vova Feldman (@svovaf)
  * @since  1.1.8
  *
  * @return array|false
  */
 private function get_themes_data_for_api()
 {
     // Alias.
     $option_name = 'all_themes';
     $all_cached_themes = self::$_accounts->get_option($option_name);
     if (!is_object($all_cached_themes)) {
         $all_cached_themes = (object) array('timestamp' => '', 'md5' => '', 'themes' => array());
     }
     $time = time();
     if (!empty($all_cached_themes->timestamp) && $time - $all_cached_themes->timestamp < WP_FS__TIME_5_MIN_IN_SEC) {
         // Don't send theme updates if last update was in the past 5 min.
         return false;
     }
     // Write timestamp to lock the logic.
     $all_cached_themes->timestamp = $time;
     self::$_accounts->set_option($option_name, $all_cached_themes, true);
     // Reload options from DB.
     self::$_accounts->load(true);
     $all_cached_themes = self::$_accounts->get_option($option_name);
     if ($time != $all_cached_themes->timestamp) {
         // If timestamp is different, then another thread captured the lock.
         return false;
     }
     // Get active theme.
     $active_theme = wp_get_theme();
     $active_theme_stylesheet = $active_theme->get_stylesheet();
     // Check if there's a change in themes.
     $all_themes = wp_get_themes();
     // Check if themes changed.
     ksort($all_themes);
     $themes_signature = '';
     foreach ($all_themes as $slug => $data) {
         $is_active = $slug === $active_theme_stylesheet;
         $themes_signature .= $slug . ',' . $data->version . ',' . ($is_active ? '1' : '0') . ';';
     }
     // Check if themes status changed (version or active/inactive).
     $themes_changed = $all_cached_themes->md5 !== md5($themes_signature);
     $themes_update_data = array();
     if ($themes_changed) {
         // Change in themes, report changes.
         // Update existing themes info.
         foreach ($all_cached_themes->themes as $slug => $data) {
             $is_active = $slug === $active_theme_stylesheet;
             if (!isset($all_themes[$slug])) {
                 // Plugin uninstalled.
                 $uninstalled_theme_data = $data;
                 $uninstalled_theme_data['is_active'] = false;
                 $uninstalled_theme_data['is_uninstalled'] = true;
                 $themes_update_data[] = $uninstalled_theme_data;
                 unset($all_themes[$slug]);
                 unset($all_cached_themes->themes[$slug]);
             } else {
                 if ($data['is_active'] !== $is_active || $data['version'] !== $all_themes[$slug]->version) {
                     // Plugin activated or deactivated, or version changed.
                     $all_cached_themes->themes[$slug]['is_active'] = $is_active;
                     $all_cached_themes->themes[$slug]['version'] = $all_themes[$slug]->version;
                     $themes_update_data[] = $all_cached_themes->themes[$slug];
                 }
             }
         }
         // Find new themes that weren't yet seen before.
         foreach ($all_themes as $slug => $data) {
             if (!isset($all_cached_themes->themes[$slug])) {
                 $is_active = $slug === $active_theme_stylesheet;
                 // New plugin.
                 $new_plugin = array('slug' => $slug, 'version' => $data->version, 'title' => $data->name, 'is_active' => $is_active, 'is_uninstalled' => false);
                 $themes_update_data[] = $new_plugin;
                 $all_cached_themes->themes[$slug] = $new_plugin;
             }
         }
         $all_cached_themes->md5 = md5($themes_signature);
         $all_cached_themes->timestamp = time();
         self::$_accounts->set_option($option_name, $all_cached_themes, true);
     }
     return $themes_update_data;
 }