Plugin Directory

Changeset 2022347


Ignore:
Timestamp:
01/31/2019 01:03:01 AM (7 years ago)
Author:
wpdashboard
Message:

added in plugin updating, and detecting updates for plugins

Location:
wp-dashboard/trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • wp-dashboard/trunk/api/class-wpdashboard-plugin-routes.php

    r2021642 r2022347  
    8686        $namespace = $this->plugin_name . '/v' . $version;
    8787        $base = 'plugin';
     88        register_rest_route( $namespace, '/' . $base . '', array(
     89            array(
     90                'methods'             => WP_REST_Server::READABLE,
     91                'callback'            => array( $this, 'get_plugins' ),
     92                'permission_callback' => array( $this, 'permission_check' ),
     93                'args'                => [
     94
     95                ]
     96            ),
     97        ) );
    8898        register_rest_route( $namespace, '/' . $base . '/install', array(
    8999            array(
     
    106116            ),
    107117        ) );
     118        register_rest_route( $namespace, '/' . $base . '/update', array(
     119            array(
     120                'methods'             => WP_REST_Server::CREATABLE,
     121                'callback'            => array( $this, 'update_plugin' ),
     122                'permission_callback' => array( $this, 'permission_check' ),
     123                'args'                => [
     124
     125                ]
     126            ),
     127        ) );
    108128        register_rest_route( $namespace, '/' . $base . '/deactivate', array(
    109129            array(
     
    126146            ),
    127147        ) );
     148    }
     149
     150    /**
     151     * Get Plugins
     152     *
     153     * Get the plugins if authenticated
     154     * to do so.
     155     *
     156     * @param WP_REST_Request $request Full data about the request.
     157     *
     158     * @return WP_Error|WP_REST_Response
     159     */
     160    public function get_plugins( $request ) {
     161
     162        $this->required();
     163        $plugins = get_plugins();
     164        foreach($plugins AS $plugin => $info) {
     165            $plugins[$plugin]['Active'] = is_plugin_active($plugin);
     166        }
     167        return new WP_REST_Response($plugins);
     168
    128169    }
    129170
     
    217258
    218259    /**
     260     * Update Plugin
     261     *
     262     * Updates the plugin on
     263     * the site.
     264     *
     265     * @since 2.0.0
     266     *
     267     * @param WP_REST_Request $request
     268     *
     269     * @return WP_Error|WP_REST_Response
     270     */
     271    public function update_plugin($request)
     272    {
     273        $this->required();
     274        $skin = new WPDashboardUpdateSkin();
     275        $current = get_site_transient('update_plugins');
     276        $upgrader = new Plugin_Upgrader($skin);
     277        $file = null;
     278        $info = null;
     279        foreach ($current->response AS $f => $i) {
     280            if ($f == $request->get_param('plugin')) {
     281                $file = $f;
     282                $info = $i;
     283                break;
     284            }
     285        }
     286        if ($upgrader == null || $file == null || $info == null) {
     287            $update = null;
     288        } else {
     289            $update = $upgrader->run(array(
     290                'package' => $info->package,
     291                'destination' => WP_PLUGIN_DIR,
     292                'clear_destination' => true,
     293                'clear_working' => true,
     294                'hook_extra' => array(
     295                    'plugin' => $file,
     296                    'type' => 'plugin',
     297                    'action' => 'update',
     298                ),
     299            ));
     300        }
     301        if ($update) {
     302            return new WP_REST_Response([$file => $skin->result, 'update_info' => $update]);
     303        } else {
     304            return $skin->result;
     305        }
     306    }
     307
     308    /**
    219309     * Deactivate Plugin
    220310     *
  • wp-dashboard/trunk/webhooks/class-wpdashboard-webhooks.php

    r2021642 r2022347  
    8383
    8484//    private $url = 'https://my.wpdashboard.io/api/v2/site/';
    85     private $url = 'http://wpdashboard.io.test/api/site/';
     85//    private $url = 'http://wpdashboard.io.test/api/site/';
     86    private $url = 'https://beta.wpdashboard.io/api/site/';
    8687
    8788    /**
  • wp-dashboard/trunk/webhooks/lib/class-wpdashboard-plugins.php

    r2021642 r2022347  
    4141        add_action( 'upgrader_process_complete', [$this, 'plugin_installed'], 10, 2);
    4242        add_action( 'activated_plugin', [$this, 'plugin_activated'], 10, 2);
     43//        add_action( 'set_site_transient_update_plugins', [$this, 'plugin_has_updates'], 10, 3);
     44        add_filter( "pre_set_site_transient_update_plugins", [$this, 'plugin_has_updates'], 10, 2 );
     45        add_action( 'upgrader_process_complete', [$this, 'plugin_updated'], 10, 2);
    4346        add_action( 'deactivated_plugin', [$this, 'plugin_deactivated'], 10, 2);
    44         add_action( 'upgrader_process_complete', [$this, 'plugin_updated'], 10, 2);
    4547        add_action( 'deleted_plugin', [$this, 'plugin_deleted'], 10, 2);
    4648    }
     
    9395
    9496    /**
     97     * Plugin Has Updates
     98     *
     99     * Fires when a plugin has updates
     100     * available to it.
     101     *
     102     * @since 2.0.0
     103     *
     104     * @param $transient
     105     * @param $value
     106     *
     107     * @return mixed
     108     */
     109    public function plugin_has_updates($value, $transient) {
     110            if(isset($value->response)) {
     111                foreach ($value->response AS $plugin => $update) {
     112                    $p = get_plugin_data(ABSPATH . 'wp-content/plugins/' . $plugin);
     113                    $body = [
     114                        'plugin' => $plugin,
     115                        'name' => $p['Name'],
     116                        'version' => $p['Version'],
     117                        'description' => $p['Description'],
     118                        'new_version' => $update->new_version,
     119                    ];
     120                    if(isset($update->icons['2x'])) {
     121                        $body['icon'] =  $update->icons['2x'];
     122                    }
     123                    $this->post('plugin/has-update', $body);
     124                }
     125            }
     126            return $value;
     127    }
     128
     129    /**
     130     * Plugin Updated
     131     *
     132     * Fires when a plugin is updated.
     133     *
     134     * @since 2.0.0
     135     *
     136     * @param $plugin
     137     * @param $network_deactivation
     138     */
     139    public function plugin_updated($upgrader, $hook) {
     140        if($hook['type'] == 'plugin' && $hook['action'] != 'install') {
     141            if(!isset($hook['plugins'])) {
     142                $plugin = get_plugin_data(ABSPATH . 'wp-content/plugins/' . $hook['plugin']);
     143                $body = [
     144                    'action' => $hook['action'],
     145                    'plugin' => $hook['plugin'],
     146                    'name' => $plugin['Name'],
     147                    'version' => $plugin['Version'],
     148                    'description' => $plugin['Description'],
     149                ];
     150                $this->post('plugin/update', $body);
     151            } else {
     152                foreach ($hook['plugins'] AS $p) {
     153                    $plugin = get_plugin_data(ABSPATH . 'wp-content/plugins/' . $p);
     154                    $body = [
     155                        'action' => $hook['action'],
     156                        'plugin' => $p,
     157                        'name' => $plugin['Name'],
     158                        'version' => $plugin['Version'],
     159                        'description' => $plugin['Description'],
     160                    ];
     161                    $this->post('plugin/update', $body);
     162                }
     163            }
     164        }
     165    }
     166
     167    /**
    95168     * Plugin Deactivated
    96169     *
     
    114187
    115188    /**
    116      * Plugin Updated
    117      *
    118      * Fires when a plugin is updated.
    119      *
    120      * @since 2.0.0
    121      *
    122      * @param $plugin
    123      * @param $network_deactivation
    124      */
    125     public function plugin_updated($upgrader, $hook) {
    126         if($hook['type'] == 'plugin' && $hook['action'] != 'install') {
    127             foreach($hook['plugins'] AS $p) {
    128                 $plugin = get_plugin_data(ABSPATH . 'wp-content/plugins/' . $p);
    129                 $body = [
    130                     'action' => $hook['action'],
    131                     'plugin' => $p,
    132                     'name' => $plugin['Name'],
    133                     'version' => $plugin['Version'],
    134                     'description' => $plugin['Description'],
    135                 ];
    136                 $this->post('plugin/update', $body);
    137             }
    138         }
    139     }
    140 
    141     /**
    142189     * Plugin Deleted
    143190     *
Note: See TracChangeset for help on using the changeset viewer.