Plugin Directory

source: worker/trunk/src/MMB/Installer.php

Last change on this file was 3480354, checked in by ManageWP, 3 weeks ago

version 4.9.31

File size: 35.9 KB
Line 
1<?php
2
3/*************************************************************
4 * installer.class.php
5 * Upgrade WordPress
6 * Copyright (c) 2011 Prelovac Media
7 * www.prelovac.com
8 **************************************************************/
9class MMB_Installer extends MMB_Core
10{
11    public function __construct()
12    {
13        @set_time_limit(600);
14        parent::__construct();
15        @include_once ABSPATH.'wp-admin/includes/file.php';
16        @include_once ABSPATH.'wp-admin/includes/plugin.php';
17        @include_once ABSPATH.'wp-admin/includes/theme.php';
18        @include_once ABSPATH.'wp-admin/includes/misc.php';
19        @include_once ABSPATH.'wp-admin/includes/template.php';
20        @include_once ABSPATH.'wp-admin/includes/class-wp-upgrader.php';
21
22        global $wp_filesystem;
23
24        if (!$this->check_if_pantheon() && !$wp_filesystem) {
25            WP_Filesystem();
26        }
27    }
28
29    public function mmb_maintenance_mode($enable = false, $maintenance_message = '')
30    {
31        global $wp_filesystem;
32
33        $maintenance_message .= '<?php $upgrading = '.time().'; ?>';
34
35        $file = $wp_filesystem->abspath().'.maintenance';
36        if ($enable) {
37            $wp_filesystem->delete($file);
38            $wp_filesystem->put_contents($file, $maintenance_message, FS_CHMOD_FILE);
39        } else {
40            $wp_filesystem->delete($file);
41        }
42    }
43
44    public function install_remote_file($params)
45    {
46        global $wp_filesystem;
47        extract($params);
48        $network_activate = isset($params['network_activate']) ? $params['network_activate'] : false;
49
50        if (!isset($package) || empty($package)) {
51            return array(
52                'error' => '<p>No files received. Internal error.</p>',
53            );
54        }
55
56        if (!$this->is_server_writable()) {
57            return array(
58                'error' => 'Failed, please <a target="_blank" href="http://managewp.com/user-guide/faq/my-pluginsthemes-fail-to-update-or-i-receive-a-yellow-ftp-warning">add FTP details</a>',
59            );
60        }
61
62        if (defined('WP_INSTALLING') && file_exists(ABSPATH.'.maintenance')) {
63            return array(
64                'error' => '<p>Site under maintanace.</p>',
65            );
66        }
67
68        if (!class_exists('WP_Upgrader')) {
69            include_once ABSPATH.'wp-admin/includes/class-wp-upgrader.php';
70        }
71
72        /** @handled class */
73        $upgrader = new WP_Upgrader(mwp_container()->getUpdaterSkin());
74        $upgrader->init();
75        $destination       = $type == 'themes' ? WP_CONTENT_DIR.'/themes' : WP_PLUGIN_DIR;
76        $clear_destination = isset($clear_destination) ? $clear_destination : false;
77
78        foreach ($package as $package_url) {
79            $key                = basename($package_url);
80            $install_info[$key] = @$upgrader->run(
81                array(
82                    'package'           => $package_url,
83                    'destination'       => $destination,
84                    'clear_destination' => $clear_destination, //Do not overwrite files.
85                    'clear_working'     => true,
86                    'hook_extra'        => array(),
87                )
88            );
89        }
90
91        if (defined('WP_ADMIN') && WP_ADMIN) {
92            global $wp_current_filter;
93            $wp_current_filter[] = 'load-update-core.php';
94
95            if (function_exists('wp_clean_update_cache')) {
96                /** @handled function */
97                wp_clean_update_cache();
98            }
99
100            /** @handled function */
101            wp_update_plugins();
102
103            array_pop($wp_current_filter);
104
105            /** @handled function */
106            set_current_screen();
107            do_action('load-update-core.php');
108
109            /** @handled function */
110            wp_version_check();
111
112            /** @handled function */
113            wp_version_check(array(), true);
114        }
115
116        $wrongFileType = false;
117        if ($type == 'plugins') {
118            $wrongFileType = true;
119            include_once ABSPATH.'wp-admin/includes/plugin.php';
120            $all_plugins = get_plugins();
121            foreach ($all_plugins as $plugin_slug => $plugin) {
122                $plugin_dir = preg_split('/\//', $plugin_slug);
123                foreach ($install_info as $key => $install) {
124                    if (!$install || is_wp_error($install)) {
125                        continue;
126                    }
127                    if ($install['destination_name'] == $plugin_dir[0]) {
128                        $wrongFileType = false;
129                        if ($activate) {
130                            $install_info[$key]['activated'] = activate_plugin($plugin_slug, '', $network_activate);
131                        }
132
133                        $install_info[$key]['basename']  = $plugin_slug;
134                        $install_info[$key]['full_name'] = $plugin['Name'];
135                        $install_info[$key]['version']   = $plugin['Version'];
136                    }
137                }
138            }
139        }
140
141        if ($type == 'themes') {
142            $wrongFileType = true;
143            if (count($install_info) == 1) {
144                global $wp_themes;
145                include_once ABSPATH.'wp-includes/theme.php';
146
147                $wp_themes = null;
148                unset($wp_themes); //prevent theme data caching
149                if (function_exists('wp_get_theme')) {
150                    foreach ($install_info as $key => $install) {
151                        if (!$install || is_wp_error($install)) {
152                            continue;
153                        }
154
155                        $theme = wp_get_theme($install['destination_name']);
156                        if ($theme->errors() !== false) {
157                            $install_info[$key] = $theme->errors();
158                            continue;
159                        }
160
161                        $wrongFileType = false;
162                        if ($activate) {
163                            $install_info[$key]['activated'] = switch_theme($theme->Template, $theme->Stylesheet);
164                        }
165
166                        $install_info[$key]['full_name'] = $theme->name;
167                        $install_info[$key]['version']   = $theme->version;
168                    }
169                } else {
170                    $all_themes = get_themes();
171                    foreach ($all_themes as $theme_name => $theme_data) {
172                        foreach ($install_info as $key => $install) {
173                            if (!$install || is_wp_error($install)) {
174                                continue;
175                            }
176
177                            if ($theme_data['Template'] == $install['destination_name'] || $theme_data['Stylesheet'] == $install['destination_name']) {
178                                $wrongFileType = false;
179                                if ($activate) {
180                                    $install_info[$key]['activated'] = switch_theme($theme_data['Template'], $theme_data['Stylesheet']);
181                                }
182                                $install_info[$key]['full_name'] = $theme_data->name;
183                                $install_info[$key]['version']   = $theme_data->version;
184                            }
185                        }
186                    }
187                }
188            }
189        }
190
191        // Can generate "E_NOTICE: ob_clean(): failed to delete buffer. No buffer to delete."
192        @ob_clean();
193        $this->mmb_maintenance_mode(false);
194
195        if (mwp_container()->getRequestStack()->getMasterRequest()->getProtocol() >= 1) {
196            // WP_Error won't get JSON encoded, so unwrap the error here.
197            foreach ($install_info as $key => $value) {
198                if ($value instanceof WP_Error) {
199                    $install_info[$key] = array(
200                        'error' => $value->get_error_message(),
201                        'code'  => $value->get_error_code(),
202                    );
203                } elseif ($wrongFileType) {
204                    $otherType          = $type === 'themes' ? 'plugins' : $type;
205                    $install_info[$key] = array(
206                        'error' => 'You can\'t install '.$type.' on '.$otherType.' page.',
207                        'code'  => 'wrong_type_of_file',
208                    );
209                }
210            }
211        }
212
213        return $install_info;
214    }
215
216    private function ithemes_updater_compatiblity()
217    {
218        // Check for the iThemes updater class
219        if (empty($GLOBALS['ithemes_updater_path']) ||
220            !file_exists($GLOBALS['ithemes_updater_path'].'/settings.php')
221        ) {
222            return;
223        }
224
225        // Include iThemes updater
226        require_once $GLOBALS['ithemes_updater_path'].'/settings.php';
227
228        // Check if the updater is instantiated
229        if (empty($GLOBALS['ithemes-updater-settings'])) {
230            return;
231        }
232
233        // Update the download link
234        $GLOBALS['ithemes-updater-settings']->flush('forced');
235    }
236
237    public function do_upgrade($params = null)
238    {
239        if ($params == null || empty($params)) {
240            return array(
241                'error' => 'No upgrades passed.',
242            );
243        }
244
245        if (!$this->is_server_writable()) {
246            return array(
247                'error' => 'Failed, please <a target="_blank" href="http://managewp.com/user-guide/faq/my-pluginsthemes-fail-to-update-or-i-receive-a-yellow-ftp-warning">add FTP details</a>',
248            );
249        }
250
251        $params = isset($params['upgrades_all']) ? $params['upgrades_all'] : $params;
252
253        $core_upgrade         = isset($params['wp_upgrade']) ? $params['wp_upgrade'] : array();
254        $upgrade_plugins      = isset($params['upgrade_plugins']) ? $params['upgrade_plugins'] : array();
255        $upgrade_themes       = isset($params['upgrade_themes']) ? $params['upgrade_themes'] : array();
256        $upgrade_translations = isset($params['upgrade_translations']) ? $params['upgrade_translations'] : false;
257
258        $upgrades         = array();
259        $premium_upgrades = array();
260        if (!empty($core_upgrade)) {
261            $upgrades['core'] = $this->upgrade_core($core_upgrade);
262        }
263
264        if (!empty($upgrade_plugins)) {
265            $plugin_files = array();
266            $this->ithemes_updater_compatiblity();
267            foreach ($upgrade_plugins as $plugin) {
268                if (isset($plugin['envatoPlugin']) && $plugin['envatoPlugin'] === true) {
269                    $upgrades['plugins'] = $this->upgrade_envato_component($plugin);
270                    continue;
271                }
272
273                if (isset($plugin['file'])) {
274                    $plugin_files[$plugin['file']] = $plugin['old_version'];
275                } else {
276                    $premium_upgrades[md5($plugin['name'])] = $plugin;
277                }
278            }
279            if (!empty($plugin_files)) {
280                $upgrades['plugins'] = $this->upgrade_plugins($plugin_files);
281            }
282            $this->ithemes_updater_compatiblity();
283        }
284
285        if (!empty($upgrade_themes)) {
286            $theme_temps = array();
287            foreach ($upgrade_themes as $theme) {
288                if (isset($theme['envatoTheme']) && $theme['envatoTheme'] === true) {
289                    $upgrades['themes'] = $this->upgrade_envato_component($theme);
290                    continue;
291                }
292
293                if (isset($theme['theme_tmp'])) {
294                    $theme_temps[] = $theme['theme_tmp'];
295                } else {
296                    $premium_upgrades[md5($theme['name'])] = $theme;
297                }
298            }
299
300            if (!empty($theme_temps)) {
301                $upgrades['themes'] = $this->upgrade_themes($theme_temps);
302            }
303        }
304
305        if (!empty($upgrade_translations)) {
306            $upgrades['translations'] = $this->upgrade_translations();
307        }
308
309        @ob_clean();
310        $this->mmb_maintenance_mode(false);
311
312        return $upgrades;
313    }
314
315    /**
316     * @param array $component
317     *
318     * @return array
319     */
320    private function upgrade_envato_component(array $component)
321    {
322        $result = $this->install_remote_file($component);
323        $return = array();
324
325        if (empty($result)) {
326            return array(
327                'error' => 'Upgrade failed.',
328            );
329        }
330
331        foreach ($result as $component_slug => $component_info) {
332            if (!$component_info || is_wp_error($component_info)) {
333                $return[$component_slug] = $this->mmb_get_error($component_info);
334                continue;
335            }
336
337            $return[$component_info['destination_name']] = 1;
338        }
339
340        return array(
341            'upgraded' => $return,
342        );
343    }
344
345    /**
346     * Upgrades WordPress locally
347     */
348    public function upgrade_core($current)
349    {
350        ob_start();
351
352        if (file_exists(ABSPATH.'/wp-admin/includes/update.php')) {
353            include_once ABSPATH.'/wp-admin/includes/update.php';
354        }
355
356        $current_update = false;
357        ob_end_flush();
358        ob_end_clean();
359        $core = $this->mmb_get_transient('update_core');
360
361        if (isset($core->updates) && !empty($core->updates)) {
362            $updates = $core->updates[0];
363            $updated = $core->updates[0];
364            if (!isset($updated->response) || $updated->response == 'latest') {
365                return array(
366                    'upgraded' => ' updated',
367                );
368            }
369
370            if ($updated->response == "development" && $current['response'] == "upgrade") {
371                return array(
372                    'error' => '<font color="#900">Unexpected error. Please upgrade manually.</font>',
373                );
374            } else {
375                if ($updated->response == $current['response'] || ($updated->response == "upgrade" && $current['response'] == "development")) {
376                    if ($updated->locale != $current['locale']) {
377                        foreach ($updates as $update) {
378                            if ($update->locale == $current['locale']) {
379                                $current_update = $update;
380                                break;
381                            }
382                        }
383                        if ($current_update == false) {
384                            return array(
385                                'error' => ' Localization mismatch. Try again.',
386                            );
387                        }
388                    } else {
389                        $current_update = $updated;
390                    }
391                } else {
392                    return array(
393                        'error' => ' Transient mismatch. Try again.',
394                    );
395                }
396            }
397        } else {
398            return array(
399                'error' => ' Refresh transient failed. Try again.',
400            );
401        }
402        if ($current_update != false) {
403            global $wp_filesystem, $wp_version;
404
405            if (version_compare($wp_version, '3.1.9', '>')) {
406                if (!class_exists('Core_Upgrader')) {
407                    include_once ABSPATH.'wp-admin/includes/class-wp-upgrader.php';
408                }
409
410                /** @handled class */
411                $core   = new Core_Upgrader(mwp_container()->getUpdaterSkin());
412                $result = $core->upgrade($current_update);
413                $this->mmb_maintenance_mode(false);
414                if (is_wp_error($result)) {
415                    return array(
416                        'error' => $this->mmb_get_error($result),
417                    );
418                } else {
419                    return array(
420                        'upgraded' => ' updated',
421                    );
422                }
423            } else {
424                if (!class_exists('WP_Upgrader')) {
425                    include_once ABSPATH.'wp-admin/includes/update.php';
426                    if (function_exists('wp_update_core')) {
427                        $result = wp_update_core($current_update);
428                        if (is_wp_error($result)) {
429                            return array(
430                                'error' => $this->mmb_get_error($result),
431                            );
432                        } else {
433                            return array(
434                                'upgraded' => ' updated',
435                            );
436                        }
437                    }
438                }
439
440                if (class_exists('WP_Upgrader')) {
441                    /** @handled class */
442                    $upgrader_skin              = new WP_Upgrader_Skin();
443                    $upgrader_skin->done_header = true;
444
445                    /** @handled class */
446                    $upgrader = new WP_Upgrader($upgrader_skin);
447
448                    // Is an update available?
449                    if (!isset($current_update->response) || $current_update->response == 'latest') {
450                        return array(
451                            'upgraded' => ' updated',
452                        );
453                    }
454
455                    $res = $upgrader->fs_connect(
456                        array(
457                            ABSPATH,
458                            WP_CONTENT_DIR,
459                        )
460                    );
461                    if (is_wp_error($res)) {
462                        return array(
463                            'error' => $this->mmb_get_error($res),
464                        );
465                    }
466
467                    $wp_dir = trailingslashit($wp_filesystem->abspath());
468
469                    $core_package = false;
470                    if (isset($current_update->package) && !empty($current_update->package)) {
471                        $core_package = $current_update->package;
472                    } elseif (isset($current_update->packages->full) && !empty($current_update->packages->full)) {
473                        $core_package = $current_update->packages->full;
474                    }
475
476                    $download = $upgrader->download_package($core_package);
477                    if (is_wp_error($download)) {
478                        return array(
479                            'error' => $this->mmb_get_error($download),
480                        );
481                    }
482
483                    $working_dir = $upgrader->unpack_package($download);
484                    if (is_wp_error($working_dir)) {
485                        return array(
486                            'error' => $this->mmb_get_error($working_dir),
487                        );
488                    }
489
490                    if (!$wp_filesystem->copy($working_dir.'/wordpress/wp-admin/includes/update-core.php', $wp_dir.'wp-admin/includes/update-core.php', true)) {
491                        $wp_filesystem->delete($working_dir, true);
492
493                        return array(
494                            'error' => 'Unable to move update files.',
495                        );
496                    }
497
498                    $wp_filesystem->chmod($wp_dir.'wp-admin/includes/update-core.php', FS_CHMOD_FILE);
499
500                    require ABSPATH.'wp-admin/includes/update-core.php';
501
502                    $update_core = update_core($working_dir, $wp_dir);
503                    ob_end_clean();
504
505                    $this->mmb_maintenance_mode(false);
506                    if (is_wp_error($update_core)) {
507                        return array(
508                            'error' => $this->mmb_get_error($update_core),
509                        );
510                    }
511                    ob_end_flush();
512
513                    return array(
514                        'upgraded' => 'updated',
515                    );
516                } else {
517                    return array(
518                        'error' => 'failed',
519                    );
520                }
521            }
522        } else {
523            return array(
524                'error' => 'failed',
525            );
526        }
527    }
528
529    public function upgrade_plugins($plugins = false)
530    {
531        if (!$plugins || empty($plugins)) {
532            return array(
533                'error' => 'No plugin files for upgrade.',
534            );
535        }
536
537        if (!function_exists('wp_update_plugins')) {
538            include_once ABSPATH.'wp-includes/update.php';
539        }
540
541        $return = array();
542
543        if (class_exists('Plugin_Upgrader')) {
544            /** @handled class */
545            $upgrader = new Plugin_Upgrader(mwp_container()->getUpdaterSkin());
546            $result   = $upgrader->bulk_upgrade(array_keys($plugins));
547
548            if (!empty($result)) {
549                foreach ($result as $plugin_slug => $plugin_info) {
550                    if (!$plugin_info || is_wp_error($plugin_info)) {
551                        $return[$plugin_slug] = $this->mmb_get_error($plugin_info);
552                        continue;
553                    }
554
555                    $return[$plugin_slug] = 1;
556                }
557
558                return array(
559                    'upgraded'           => $return,
560                    'additional_updates' => $this->get_additional_plugin_updates($result),
561                );
562            } else {
563                return array(
564                    'error' => 'Upgrade failed.',
565                );
566            }
567        } else {
568            return array(
569                'error' => 'WordPress update required first.',
570            );
571        }
572    }
573
574    private function get_additional_plugin_updates($plugin_upgrades)
575    {
576        if (empty($plugin_upgrades)) {
577            return array();
578        }
579
580        $additional_updates = array();
581
582        // WooCommerce - check if DB update is needed
583        if (array_key_exists('woocommerce/woocommerce.php', $plugin_upgrades) && is_plugin_active('woocommerce/woocommerce.php')) {
584            if ($this->has_woocommerce_db_update()) {
585                $additional_updates['woocommerce/woocommerce.php'] = 1;
586            }
587        }
588
589        // Elementor - check if DB update is needed
590        if (array_key_exists('elementor/elementor.php', $plugin_upgrades) && is_plugin_active('elementor/elementor.php')) {
591            if ($this->has_elementor_db_update()) {
592                $additional_updates['elementor/elementor.php'] = 1;
593            }
594        }
595
596        // Elementor Pro - check if DB update is needed
597        if (array_key_exists('elementor-pro/elementor-pro.php', $plugin_upgrades) && is_plugin_active('elementor-pro/elementor-pro.php')) {
598            if ($this->has_elementor_pro_db_update()) {
599                $additional_updates['elementor-pro/elementor-pro.php'] = 1;
600            }
601        }
602
603        return $additional_updates;
604    }
605
606    /**
607     * Check if WooCommerce needs a database update.
608     *
609     * @return bool
610     */
611    private function has_woocommerce_db_update()
612    {
613        // Caller (get_additional_plugin_updates) already verified plugin is active.
614        $dbVersion = get_option('woocommerce_db_version', '');
615
616        if (empty($dbVersion)) {
617            return false;
618        }
619
620        // IMPORTANT: After an upgrade, WC_VERSION constant still holds the OLD version
621        // because it was defined when the old plugin loaded at request start.
622        // We must read the NEW version directly from the upgraded plugin file.
623        $pluginVersion = $this->get_plugin_version_from_file('woocommerce/woocommerce.php');
624
625        if (empty($pluginVersion)) {
626            $pluginVersion = defined('WC_VERSION') ? WC_VERSION : '';
627        }
628
629        if (empty($pluginVersion)) {
630            return false;
631        }
632
633        // Compare the actual file version with stored DB version
634        return version_compare($pluginVersion, $dbVersion, '>');
635    }
636
637    /**
638     * Check if Elementor needs a database update.
639     *
640     * @return bool
641     */
642    private function has_elementor_db_update()
643    {
644        // Caller (get_additional_plugin_updates) already verified plugin is active.
645        $dbVersion = get_option('elementor_db_version', '');
646
647        // New install - no DB update needed
648        if (empty($dbVersion)) {
649            return false;
650        }
651
652        // IMPORTANT: After an upgrade, ELEMENTOR_VERSION constant still holds the OLD version
653        // because it was defined when the old plugin loaded at request start.
654        // We must read the NEW version directly from the upgraded plugin file.
655        $pluginVersion = $this->get_plugin_version_from_file('elementor/elementor.php');
656
657        if (empty($pluginVersion)) {
658            // Fallback to constant if file read fails
659            $pluginVersion = defined('ELEMENTOR_VERSION') ? ELEMENTOR_VERSION : '';
660        }
661
662        if (empty($pluginVersion)) {
663            return false;
664        }
665
666        // Compare the actual file version with stored DB version
667        return version_compare($pluginVersion, $dbVersion, '>');
668    }
669
670    /**
671     * Check if Elementor Pro needs a database update.
672     *
673     * @return bool
674     */
675    private function has_elementor_pro_db_update()
676    {
677        // Caller (get_additional_plugin_updates) already verified plugin is active.
678        $dbVersion = get_option('elementor_pro_db_version', '');
679
680        // New install - no DB update needed
681        if (empty($dbVersion)) {
682            return false;
683        }
684
685        // IMPORTANT: After an upgrade, ELEMENTOR_PRO_VERSION constant still holds the OLD version
686        // because it was defined when the old plugin loaded at request start.
687        // We must read the NEW version directly from the upgraded plugin file.
688        $pluginVersion = $this->get_plugin_version_from_file('elementor-pro/elementor-pro.php');
689
690        if (empty($pluginVersion)) {
691            // Fallback to constant if file read fails
692            $pluginVersion = defined('ELEMENTOR_PRO_VERSION') ? ELEMENTOR_PRO_VERSION : '';
693        }
694
695        if (empty($pluginVersion)) {
696            return false;
697        }
698
699        // Compare the actual file version with stored DB version
700        return version_compare($pluginVersion, $dbVersion, '>');
701    }
702
703    /**
704     * Read the version number directly from a plugin's main file.
705     * This is necessary after an upgrade because PHP constants are still
706     * set to the OLD version (they were defined when the old plugin loaded).
707     *
708     * @param string $pluginFile Plugin file path relative to plugins directory (e.g., 'woocommerce/woocommerce.php')
709     * @return string|null Version string or null if not found
710     */
711    private function get_plugin_version_from_file($pluginFile)
712    {
713        $pluginPath = WP_PLUGIN_DIR . '/' . $pluginFile;
714
715        if (!file_exists($pluginPath)) {
716            return null;
717        }
718
719        // Read only the first 8KB of the file (plugin headers are at the top)
720        $fileContent = file_get_contents($pluginPath, false, null, 0, 8192);
721
722        if (empty($fileContent)) {
723            return null;
724        }
725
726        // Look for "Version:" in the plugin header
727        // Standard format: " * Version: 1.2.3" or "Version: 1.2.3"
728        if (preg_match('/^[\s\*]*Version:\s*([0-9]+\.[0-9]+(?:\.[0-9]+)?(?:-[a-zA-Z0-9.]+)?)/mi', $fileContent, $matches)) {
729            return trim($matches[1]);
730        }
731
732        return null;
733    }
734
735    public function upgrade_themes($themes = false)
736    {
737        if (!$themes || empty($themes)) {
738            return array(
739                'error' => 'No theme files for upgrade.',
740            );
741        }
742
743        if (!function_exists('wp_update_themes')) {
744            include_once ABSPATH.'wp-includes/update.php';
745        }
746
747        if (class_exists('Theme_Upgrader')) {
748            /** @handled class */
749            $upgrader = new Theme_Upgrader(mwp_container()->getUpdaterSkin());
750            $result   = $upgrader->bulk_upgrade($themes);
751
752            $return = array();
753            if (!empty($result)) {
754                foreach ($result as $theme_tmp => $theme_info) {
755                    if (is_wp_error($theme_info) || empty($theme_info)) {
756                        $return[$theme_tmp] = $this->mmb_get_error($theme_info);
757                        continue;
758                    }
759
760                    $return[$theme_tmp] = 1;
761                }
762
763                return array(
764                    'upgraded' => $return,
765                );
766            } else {
767                return array(
768                    'error' => 'Upgrade failed.',
769                );
770            }
771        } else {
772            return array(
773                'error' => 'WordPress update required first',
774            );
775        }
776    }
777
778    public function upgrade_translations()
779    {
780        include_once ABSPATH.'wp-admin/includes/class-wp-upgrader.php';
781
782        if (class_exists('Language_Pack_Upgrader')) {
783            /** @handled class */
784            $upgrader = new Language_Pack_Upgrader(mwp_container()->getUpdaterSkin());
785            $result   = $upgrader->bulk_upgrade();
786
787            if (!empty($result)) {
788                $return = 1;
789
790                if (is_array($result)) {
791                    foreach ($result as $translate_tmp => $translate_info) {
792                        if (is_wp_error($translate_info) || empty($translate_info)) {
793                            $return = $this->mmb_get_error($translate_info);
794                            break;
795                        }
796                    }
797                }
798
799                return array('upgraded' => $return);
800            } else {
801                return array(
802                    'error' => 'Upgrade failed.',
803                );
804            }
805        } else {
806            return array(
807                'error' => 'WordPress update required first',
808            );
809        }
810    }
811
812    public function get_upgradable_plugins()
813    {
814        $current = $this->mmb_get_transient('update_plugins');
815
816        $upgradable_plugins = array();
817        if (!empty($current->response)) {
818            if (!function_exists('get_plugin_data')) {
819                include_once ABSPATH.'wp-admin/includes/plugin.php';
820            }
821            foreach ($current->response as $plugin_path => $plugin_data) {
822                $data = get_plugin_data(WP_PLUGIN_DIR.'/'.$plugin_path, false, false);
823
824                if (strlen($data['Name']) > 0 && strlen($data['Version']) > 0) {
825                    $current->response[$plugin_path]->name        = $data['Name'];
826                    $current->response[$plugin_path]->old_version = $data['Version'];
827                    $current->response[$plugin_path]->file        = $plugin_path;
828                    unset($current->response[$plugin_path]->upgrade_notice);
829                    $upgradable_plugins[] = $current->response[$plugin_path];
830                }
831            }
832
833            return $upgradable_plugins;
834        } else {
835            return array();
836        }
837    }
838
839    public function get_upgradable_themes()
840    {
841        if (function_exists('wp_get_themes')) {
842            $all_themes     = wp_get_themes();
843            $upgrade_themes = array();
844
845            $current = $this->mmb_get_transient('update_themes');
846
847            if (empty($current->response)) {
848                return $upgrade_themes;
849            }
850
851            foreach ((array)$all_themes as $theme_template => $theme_data) {
852                foreach ($current->response as $current_themes => $theme) {
853                    if ($theme_data->Stylesheet !== $current_themes) {
854                        continue;
855                    }
856
857                    if (strlen($theme_data->Name) === 0 || strlen($theme_data->Version) === 0) {
858                        continue;
859                    }
860
861                    $current->response[$current_themes]['name']        = $theme_data->Name;
862                    $current->response[$current_themes]['old_version'] = $theme_data->Version;
863                    $current->response[$current_themes]['theme_tmp']   = $theme_data->Stylesheet;
864
865                    $upgrade_themes[] = $current->response[$current_themes];
866                }
867            }
868        } else {
869            $all_themes = get_themes();
870
871            $upgrade_themes = array();
872
873            $current = $this->mmb_get_transient('update_themes');
874            if (!empty($current->response)) {
875                foreach ((array)$all_themes as $theme_template => $theme_data) {
876                    if (isset($theme_data['Parent Theme']) && !empty($theme_data['Parent Theme'])) {
877                        continue;
878                    }
879
880                    if (isset($theme_data['Name']) && in_array($theme_data['Name'], $filter)) {
881                        continue;
882                    }
883
884                    foreach ($current->response as $current_themes => $theme) {
885                        if ($theme_data['Template'] == $current_themes) {
886                            if (strlen($theme_data['Name']) > 0 && strlen($theme_data['Version']) > 0) {
887                                $current->response[$current_themes]['name']        = $theme_data['Name'];
888                                $current->response[$current_themes]['old_version'] = $theme_data['Version'];
889                                $current->response[$current_themes]['theme_tmp']   = $theme_data['Template'];
890                                $upgrade_themes[]                                  = $current->response[$current_themes];
891                            }
892                        }
893                    }
894                }
895            }
896        }
897
898        return $upgrade_themes;
899    }
900
901    public function get_upgradable_translations()
902    {
903        $updates = array(
904            'core'    => array(),
905            'plugins' => array(),
906            'themes'  => array(),
907        );
908
909        $transients = array('update_core' => 'core', 'update_plugins' => 'plugins', 'update_themes' => 'themes');
910
911        foreach ($transients as $transient => $type) {
912            $transient = get_site_transient($transient);
913
914            if (empty($transient->translations)) {
915                continue;
916            }
917
918            foreach ($transient->translations as $translation) {
919                $updates[$type][] = (object)$translation;
920            }
921        }
922
923        return $updates;
924    }
925
926    public function edit($args)
927    {
928        extract($args);
929        $return = array();
930        if ($type == 'plugins') {
931            $return['plugins'] = $this->edit_plugins($args);
932        } elseif ($type == 'themes') {
933            $return['themes'] = $this->edit_themes($args);
934        }
935
936        return $return;
937    }
938
939    public function edit_plugins($args)
940    {
941        extract($args);
942        $return = array();
943        foreach ($items as $item) {
944            switch ($items_edit_action) {
945                case 'activate':
946                    $result = activate_plugin($item['path'], '', $item['networkWide']);
947                    break;
948                case 'deactivate':
949                    $result = deactivate_plugins(
950                        array(
951                            $item['path'],
952                        ),
953                        false,
954                        $item['networkWide']
955                    );
956                    break;
957                case 'delete':
958                    $result = delete_plugins(
959                        array(
960                            $item['path'],
961                        )
962                    );
963                    break;
964                default:
965                    break;
966            }
967
968            if (is_wp_error($result)) {
969                $result = array(
970                    'error' => $result->get_error_message(),
971                );
972            } elseif ($result === false) {
973                $result = array(
974                    'error' => "Failed to perform action.",
975                );
976            } else {
977                $result = "OK";
978            }
979            $return[$item['name']] = $result;
980        }
981
982        return $return;
983    }
984
985    public function edit_themes($args)
986    {
987        extract($args);
988        $return = array();
989        foreach ($items as $item) {
990            switch ($items_edit_action) {
991                case 'activate':
992                    switch_theme($item['path'], $item['stylesheet']);
993                    break;
994                case 'delete':
995                    $result = delete_theme($item['stylesheet']);
996                    break;
997                default:
998                    break;
999            }
1000
1001            if (is_wp_error($result)) {
1002                $result = array(
1003                    'error' => $result->get_error_message(),
1004                );
1005            } elseif ($result === false) {
1006                $result = array(
1007                    'error' => "Failed to perform action.",
1008                );
1009            } else {
1010                $result = "OK";
1011            }
1012            $return[$item['name']] = $result;
1013        }
1014
1015        return $return;
1016    }
1017}
Note: See TracBrowser for help on using the repository browser.