Plugin Directory

Changeset 2021642


Ignore:
Timestamp:
01/30/2019 05:56:12 AM (7 years ago)
Author:
wpdashboard
Message:

getting the API laid out in WP Rest, and fixing some webhook issues

Location:
wp-dashboard/trunk
Files:
3 added
5 edited

Legend:

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

    r1824830 r2021642  
    5757
    5858    public function init() {
    59         // Silence is Golden
     59
    6060    }
    6161
  • wp-dashboard/trunk/api/lib/class-wpdashboard-plugins.php

    r1824829 r2021642  
    2626require_once 'class-wp-upgrade-skin.php';
    2727
    28 class WpDashboard_Api_Plugins extends WpDashboard_Api_Base {
     28class WpDashboard_Api_Plugins extends WP_REST_Controller
     29{
    2930
    3031    /**
     
    3536    public function __construct($plugin_name, $version)
    3637    {
    37 
    38         parent::__construct($plugin_name, $version);
     38        $this::register_routes();
    3939    }
    4040
     
    4343     * @return array
    4444     */
    45     public function get_all_plugins(WP_REST_Request $request) {
     45    public function get_all_plugins(WP_REST_Request $request)
     46    {
    4647        $plugins = get_plugins();
    4748        return $plugins;
     
    5253     * @return array|WP_Error
    5354     */
    54     public function install_plugin(WP_REST_Request $request) {
    55         $plugin = $request->get_param('plugin');
    56         if($this->check_plugin_installed($plugin)) {
     55    public function install_plugin(WP_REST_Request $request)
     56    {
     57        $plugin = $request->get_param('plugin');
     58        if ($this->check_plugin_installed($plugin)) {
    5759            $error = new WP_Error('plugin_already_installed', 'The plugin is already installed on the site.', ['plugin' => $plugin]);
    5860            return $error;
    5961        }
    6062        $plugin_slug = explode('/', $plugin)[0];
    61         $api = plugins_api( 'plugin_information', array(
     63        $api = plugins_api('plugin_information', array(
    6264            'slug' => $plugin_slug,
    6365            'fields' => array(
     
    7577                'donate_link' => false,
    7678            ),
    77         ) );
    78         if(is_wp_error($api)) {
     79        ));
     80        if (is_wp_error($api)) {
    7981            $error = new WP_Error('plugin_not_found', 'The plugin was not found in the WordPress repository.', ['plugin' => $plugin]);
    8082            return $error;
    8183        }
    8284        $skin = new WPDashboardUpdateSkin();
    83         $upgrader = new Plugin_Upgrader( $skin );
     85        $upgrader = new Plugin_Upgrader($skin);
    8486        $install = $upgrader->install($api->download_link);
    85         if($install) {
    86             return  [
     87        if ($install) {
     88            return [
    8789                'plugin' => $plugin,
    8890                'installed' => true
     
    98100     * @return array|bool|string|WP_Error
    99101     */
    100     public function update_plugin(WP_REST_Request $request) {
     102    public function update_plugin(WP_REST_Request $request)
     103    {
    101104        $skin = new WPDashboardUpdateSkin();
    102         $current = get_site_transient( 'update_plugins' );
    103         $upgrader = new Plugin_Upgrader( $skin );
     105        $current = get_site_transient('update_plugins');
     106        $upgrader = new Plugin_Upgrader($skin);
    104107        $file = null;
    105108        $info = null;
    106         foreach( $current->response AS $f => $i) {
    107             if($f == $request->get_param('plugin')) {
     109        foreach ($current->response AS $f => $i) {
     110            if ($f == $request->get_param('plugin')) {
    108111                $file = $f;
    109112                $info = $i;
     
    112115        }
    113116        $update = $this->do_plugin_upgrade($upgrader, $file, $info);
    114         if($update) {
    115             return  [$file => $skin->result, 'update_info' => $update];
     117        if ($update) {
     118            return [$file => $skin->result, 'update_info' => $update];
    116119        } else {
    117120            return $skin->result;
     
    123126     * @return array|null|WP_Error
    124127     */
    125     public function activate_plugin(WP_REST_Request $request) {
    126         $plugin = $request->get_param('plugin');
    127         if(!$this->check_plugin_status($plugin)) {
     128    public function activate_plugin(WP_REST_Request $request)
     129    {
     130        $plugin = $request->get_param('plugin');
     131        if (!$this->check_plugin_status($plugin)) {
    128132            $activate = activate_plugin($plugin);
    129             if ( is_wp_error( $activate ) ) {
     133            if (is_wp_error($activate)) {
    130134                return $activate;
    131135            } else {
     
    145149     * @return array|WP_Error
    146150     */
    147     public function deactivate_plugin(WP_REST_Request $request) {
    148         $plugin = $request->get_param('plugin');
    149         if($this->check_plugin_status($plugin)) {
     151    public function deactivate_plugin(WP_REST_Request $request)
     152    {
     153        $plugin = $request->get_param('plugin');
     154        if ($this->check_plugin_status($plugin)) {
    150155            deactivate_plugins($plugin);
    151156            return [
     
    163168     * @return array|bool|null|WP_Error
    164169     */
    165     public function delete_plugin(WP_REST_Request $request) {
    166         $plugin = $request->get_param('plugin');
    167         if($this->check_plugin_status($plugin)) {
     170    public function delete_plugin(WP_REST_Request $request)
     171    {
     172        $plugin = $request->get_param('plugin');
     173        if ($this->check_plugin_status($plugin)) {
    168174            $error = new WP_Error('plugin_is_active', 'Your plugin is active, you must deactivate it before deleting it.', ['plugin' => $plugin]);
    169175            return $error;
     
    171177        $uninstall = uninstall_plugin($plugin);
    172178        $delete = delete_plugins([$plugin]);
    173         if ( is_wp_error( $delete ) ) {
     179        if (is_wp_error($delete)) {
    174180            return $delete;
    175181        } else {
     
    185191     * @return bool
    186192     */
    187     protected function check_plugin_status($plugin) {
     193    protected function check_plugin_status($plugin)
     194    {
    188195        return is_plugin_active($plugin);
    189196    }
     
    193200     * @return bool
    194201     */
    195     protected function check_plugin_installed($check) {
     202    protected function check_plugin_installed($check)
     203    {
    196204        $plugins = get_plugins();
    197         foreach($plugins AS $f => $i) {
    198             if($f == $check) {
     205        foreach ($plugins AS $f => $i) {
     206            if ($f == $check) {
    199207                return true;
    200208            }
     
    209217     * @return null
    210218     */
    211     protected function do_plugin_upgrade($upgrader, $file, $info) {
    212         if($upgrader == null || $file == null || $info == null) {
     219    protected function do_plugin_upgrade($upgrader, $file, $info)
     220    {
     221        if ($upgrader == null || $file == null || $info == null) {
    213222            return null;
    214223        } else {
     
    226235        }
    227236    }
    228 
    229237}
  • wp-dashboard/trunk/includes/class-wpdashboard.php

    r2020673 r2021642  
    119119
    120120        /**
    121          * The class responsible for defining all actions that occur in the api.
    122          */
    123         require_once plugin_dir_path( dirname( __FILE__ ) ) . 'api/class-wpdashboard-api.php';
     121         * The classes responsible for defining all actions that occur in the api.
     122         */
     123        require_once plugin_dir_path( dirname( __FILE__ ) ) . 'api/class-wpdashboard-plugin-routes.php';
    124124
    125125        /**
     
    199199     */
    200200    private function define_api_hooks() {
    201 
    202         $plugin_api = new Wpdashboard_Api( $this->get_plugin_name(), $this->get_version() );
    203 
    204         $this->loader->add_action( 'parse_request', $plugin_api, 'detect_api_request' );
    205 
    206         $this->loader->add_action( 'parse_request', $plugin_api, 'detect_redirects' );
    207 
     201        $api = new WpDashboard_Plugin_Routes( $this->get_plugin_name(), $this->get_version() );
     202        $this->loader->add_action( 'rest_api_init', $api, 'register_routes' );
    208203        $webhook_api = new Wpdashboard_WebHooks( $this->get_plugin_name(), $this->get_version() );
    209 
    210204        $webhook_api->init();
    211 
    212205    }
    213206
  • wp-dashboard/trunk/webhooks/class-wpdashboard-webhooks.php

    r2020684 r2021642  
    2525 */
    2626require_once 'lib/class-wpdashboard-plugins.php';
     27require_once 'lib/class-wpdashboard-posts.php';
    2728require_once 'lib/class-wpdashboard-themes.php';
    2829require_once 'lib/class-wpdashboard-users.php';
     
    3435    use Wpdashboard_Themes;
    3536    use Wpdashboard_Users;
     37    use Wpdashboard_Posts;
    3638    use Wpdashboard_Wordpress;
    3739
     
    8082     */
    8183
    82 //    private $url = 'https://my.wpdashboard.io/api/site/';
    83     private $url = 'https://beta.wpdashboard.io/api/site/';
    84 //    private $url = 'http://wpdashboard.io.test/api/site/';
     84//    private $url = 'https://my.wpdashboard.io/api/v2/site/';
     85    private $url = 'http://wpdashboard.io.test/api/site/';
    8586
    8687    /**
     
    114115        $this->initUsers();
    115116        $this->initWordpress();
     117        $this->initPosts();
    116118    }
    117119
  • wp-dashboard/trunk/webhooks/lib/class-wpdashboard-plugins.php

    r2020673 r2021642  
    124124     */
    125125    public function plugin_updated($upgrader, $hook) {
    126         if($hook['type'] == 'plugin') {
     126        if($hook['type'] == 'plugin' && $hook['action'] != 'install') {
    127127            foreach($hook['plugins'] AS $p) {
    128128                $plugin = get_plugin_data(ABSPATH . 'wp-content/plugins/' . $p);
Note: See TracChangeset for help on using the changeset viewer.