plugins = $items; } /** * Returns an array of columns. * @return array */ function get_columns(){ $columns = array( 'cb' => '', 'title' => 'Plugin Name', 'version' => 'Plugin Version', 'last_updated' => 'Last Updated', 'status' => 'Status', ); return $columns; } /** * Prepare all the items to be displayed * @return void */ function prepare_items() { $hidden = array(); $sortable = array(); $columns = $this->get_columns(); //handle bulk and single actions $this->handle_actions(); $this->_column_headers = array($columns, $hidden, $sortable); $this->items = $this->plugins ; } /** * Overwrite of the get_bulk_actions method * @return Array of actions */ function get_bulk_actions() { $actions = array( 'install' => 'Install', 'activate' => 'Activate', 'deactivate' => 'Deactivate', 'delete' => 'Delete', ); return $actions; } /** * We handle all the actions here * @return void */ function handle_actions(){ require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; // Need for upgrade classes. // we get current installed plugins $current = get_option( 'active_plugins' ); if( 'install' === $this->current_action() ) { require_once( 'class-wpfavs-plugin-installer-skin.php' ); // Needed for installs. // Install a single plugin if( !isset( $_POST['plugin'] ) && isset( $_GET['plugin_id'] ) && 'install' == $_GET['action'] ) { // Create a new instance of Plugin_Upgrader. $upgrader = new Plugin_Upgrader ( $skin = new WpFavs_Plugin_Installer_Skin( ) ); // Perform the action and install the plugin from wordpress.org $upgrader->install( $this->plugins[$_GET['plugin_id']]['dlink'] ); //We change status $this->plugins[$_GET['plugin_id']]['status'] = 'inactive'; } //Install Bulk if( isset( $_POST['plugin'] ) ) { //We update status foreach( $this->plugins as $plugin_id => $plugin ) { //We check if the plugin is installed (if is not installed we cannot uninstall it) if( in_array( $plugin_id, $_POST['plugin'] ) && 'not-installed' == $plugin['status'] ) { // Create a new instance of Plugin_Upgrader. $upgrader = new Plugin_Upgrader ( $skin = new WpFavs_Plugin_Installer_Skin( array( 'multi' => 'true' ) ) ); // Perform the action and install the plugin from wordpress.org $upgrader->install( $this->plugins[$plugin_id]['dlink'] ); $this->plugins[$plugin_id]['status'] = 'inactive'; } } } // Flush plugins cache so we can make sure that the installed plugins list is always up to date. wp_cache_flush(); } if( 'activate' === $this->current_action() ) { // Activate a single plugin if( !isset( $_POST['plugin'] ) && isset( $_GET['plugin_id'] ) && 'activate' == $_GET['action'] ) { activate_plugins( $this->plugins[$_GET['plugin_id']]['file_path'] ); //We change status $this->plugins[$_GET['plugin_id']]['status'] = 'active'; } //Activate Bulk if( isset( $_POST['plugin'] ) ) { activate_plugins( $_POST['plugin'] ); //We update status foreach( $this->plugins as $plugin_id => $plugin ) { //We check if the plugin is inactive (if is not installed we cannot activate it) if( in_array( $plugin['file_path'], $_POST['plugin'] ) && 'inactive' == $plugin['status'] ) { $this->plugins[$plugin_id]['status'] = 'active'; } } } // Flush plugins cache so we can make sure that the installed plugins list is always up to date. wp_cache_flush(); } if( 'deactivate' === $this->current_action() ) { // Deactivate a single plugin if( !isset( $_POST['plugin'] ) && isset( $_GET['plugin_id'] ) && 'deactivate' == $_GET['action'] ) { deactivate_plugins( $this->plugins[$_GET['plugin_id']]['file_path'] ); //We change status $this->plugins[$_GET['plugin_id']]['status'] = 'inactive'; } //Deactivate Bulk if( isset( $_POST['plugin'] ) ) { $this->deactive_bulk( $_POST['plugin'] ); } // Flush plugins cache so we can make sure that the installed plugins list is always up to date. wp_cache_flush(); } if( 'delete' === $this->current_action() ) { // Delete a single plugin if( !isset( $_POST['plugin'] ) && isset( $_GET['plugin_id'] ) && 'delete' == $_GET['action'] ) { delete_plugins( array($this->plugins[$_GET['plugin_id']]['file_path'] ) ); //We change status $this->plugins[$_GET['plugin_id']]['status'] = 'not-installed'; } // Delete Bulk if( isset( $_POST['plugin'] ) ) { $this->deactive_bulk( $_POST['plugin'] ); @delete_plugins( $_POST['plugin'] ); //We update status foreach( $this->plugins as $plugin_id => $plugin ) { //We check if the plugin is inactive (if is not inactive we cannot uninstall it) if( in_array( $plugin['file_path'], $_POST['plugin'] ) && 'inactive' == $plugin['status'] ) { $this->plugins[$plugin_id]['status'] = 'not-installed'; } } } // Flush plugins cache so we can make sure that the installed plugins list is always up to date. wp_cache_flush(); } } /** * Fucntion to deativate plugins in bulk. Used in deactivate action and delete action * @param array $plugins Array of plugin passed in posts var * @return void */ private function deactive_bulk( $plugins ) { deactivate_plugins( $plugins ); //We update status foreach( $this->plugins as $plugin_id => $plugin ) { //We check if the plugin is active (if is not installed we cannot deactivate it) if( in_array( $plugin['file_path'], $plugins ) && 'active' == $plugin['status'] ) { $this->plugins[$plugin_id]['status'] = 'inactive'; } } } /** * Default behaviour for columns * @param array $item item array * @param string $column_name column name string * @return string return the item name */ function column_default( $item, $column_name ) { switch( $column_name ) { case 'title': case 'description': case 'link': case 'version': case 'last_updated': return $item[ $column_name ]; default: return print_r( $item, true ) ; //Show the whole array for troubleshooting purposes } } /** * Checkbox column * @param array $item item array * @return string return the item name */ function column_cb( $item ) { $cb = ! empty( $item['file_path'] ) ? $item['file_path'] : $item['id']; return sprintf( '', $cb ); } /** * Show actions to do on the list * @param array $item The item displayed * @return array Array of actions to perform */ function column_title( $item ) { $wpfav = Wpfavs_Admin::get_instance(); $actions = array( 'view' => sprintf('View Plugin Page',$item['link']), ); if( $item['status'] == 'not-installed') { $actions['install'] = 'Install'; } elseif ( $item['status'] == 'inactive' ) { $actions['activate'] = 'Activate'; $actions['delete'] = 'Delete'; } elseif ( $item['status'] == 'active' ) { $actions['deactivate'] = 'Deactivate'; } return sprintf('%1$s %2$s', $item['title'], $this->row_actions($actions) ); } function column_status( $item ) { $wpfav = Wpfavs_Admin::get_instance(); switch ( $item['status'] ) { case 'not-installed': return '' . __( "Not Installed", $wpfav->plugin_slug ) . ''; break; case 'active': return '' . __( "Active", $wpfav->plugin_slug ) . ''; break; case 'inactive': return '' . __( "Inactive", $wpfav->plugin_slug ) . ''; break; default: return '' . __( "Not Installed", $wpfav->plugin_slug ) . ''; break; } } }