PluginProbe
PlugVersions – Easily roll back to previous versions of your plugins. / 0.1.0
PlugVersions – Easily roll back to previous versions of your plugins. v0.1.0
0.0.6.RC-1 0.0.7 0.0.8 0.1.0 0.2.0 0.2.1 trunk 0.0.1 0.0.2 0.0.3 0.0.4 0.0.5 0.0.6
plugversions / admin / classes / class-plugversions-restoring-link.php

class-plugversions-restoring-link.php in PlugVersions – Easily roll back to previous versions of your plugins. 0.1.0, at admin/classes/class-plugversions-restoring-link.php

150 lines 5.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Class to add the restoring link.
4 * This class is responsible for adding a link to restore plugin versions in the plugins list.
5 * It checks for existing plugin versions, adds action links to restore them,
6 * and cleans up the plugins list by removing unzipped versions.
7 *
8 * @version 0.0.8
9 *
10 * @package Plugversions
11 */
12
13 defined( 'PLUGIN_REVISIONS_PLUGIN_DIR' ) || exit; // Exit if not accessed from PlugVersions.
14
15 /**
16 * Class PlugVersions Restoring Link
17 * This class is used to add a link for restoring plugin versions.
18 * It checks for existing plugin versions in the plugins directory,
19 * adds action links to restore them, and cleans up the plugins list
20 *
21 *
22 * @version 0.0.6
23 * @package PlugVersions
24 */
25 class PlugVersions_Restoring_Link {
26
27 /**
28 * Key.
29 *
30 * @var string $this->key
31 * @since 0.0.6
32 */
33 public $key;
34
35 /**
36 * Plugins.
37 *
38 * @var array $plugins
39 * @since 0.0.8
40 */
41 public $plugins;
42
43 /**
44 * Main Constructor
45 * This constructor initializes the class, checks for the plugin revision key,
46 * populates the plugins data, and adds necessary filters for action links and cleaning the plugins list.
47 *
48 * @since 0.0.6
49 */
50 public function __construct() {
51 $this->key = eos_plugin_revision_key();
52 $this->plugins = array();
53 if( $this->key && current_user_can( 'activate_plugin' ) ) {
54 $this->plugins = $this->populate_plugins();
55 if( ! empty( $this->plugins ) ) {
56 add_filter( 'plugin_action_links' , array( $this, 'add_link' ), 10, 4 );
57 }
58 add_filter( 'all_plugins', array( $this, 'clean_plugins_list' ) );
59 }
60 }
61
62 /**
63 * Populate plugins data
64 * This function scans the plugins directory for plugin zip files that match the revision key.
65 * It extracts the plugin name and version from the zip file names and organizes them into an array.
66 * The array contains plugin names as keys, with their versions and zip file names as values.
67 *
68 * @since 0.0.6
69 */
70 public function populate_plugins() {
71 $all_plugin_files = scandir( WP_PLUGIN_DIR );
72 if( $all_plugin_files && is_array( $all_plugin_files ) && ! empty( $all_plugin_files ) ) {
73 $plugins = array();
74 foreach( $all_plugin_files as $plugin ) {
75 if( 'zip' === pathinfo( $plugin, PATHINFO_EXTENSION ) ) {
76 if( false !== strpos( $plugin, 'pr-' . $this->key .'-' ) && false !== strpos( $plugin, '-ver-' ) ) {
77 $arr = explode( 'ver-', $plugin );
78 $arr = explode( '-', $arr[0] );
79 if( 'pr' === $arr[0] && isset( $arr[1] ) && $this->key === $arr[1] && isset( $arr[2] ) ) {
80 $version = $arr[2];
81 $plugin_name = str_replace( array( 'pr-' . $this->key . '-' . $version . '-ver-', '.zip' ), array( '', '' ), $plugin );
82 if( is_dir( WP_PLUGIN_DIR . '/' . $plugin_name ) ) {
83 if( ! isset( $plugins[$plugin_name]['versions'] ) ) {
84 $plugins[$plugin_name]['versions'] = array();
85 }
86 if( ! isset( $plugins[$plugin_name]['zips'] ) ) {
87 $plugins[$plugin_name]['zips'] = array();
88 }
89 $plugins[$plugin_name]['versions'][] = $version;
90 $plugins[$plugin_name]['zips'][] = $plugin;
91 }
92 }
93 }
94 }
95 }
96 return $plugins;
97 }
98 }
99
100 /**
101 * Remove unzipped versions from the plugins list
102 * This function iterates through the plugins list and removes any plugin that has a name starting with 'pr-' followed by the revision key.
103 * It ensures that only the original plugins remain in the list, excluding any unzipped versions created by the plugin revisions.
104 *
105 * @param array $plugins
106 * @param 0.0.8
107 */
108 public function clean_plugins_list( $plugins ) {
109 foreach( $plugins as $plugin => $arr ) {
110 if( false !== strpos( '_' . $plugin, 'pr-' . $this->key ) ) {
111 unset( $plugins[$plugin] );
112 }
113 }
114 return $plugins;
115 }
116
117 /**
118 * Add action link to restore plugin version.
119 * This function adds a link to the plugin action links in the plugins list.
120 * It checks if the plugin has any versions available for restoration,
121 * and if so, it adds a link to restore each version.
122 *
123 * @param array $actions
124 * @param string $plugin_file
125 * @param array $plugin_data
126 * @param string $context
127 * @param 0.0.6
128 */
129 public function add_link( $actions, $plugin_file, $plugin_data, $context ) {
130 if( isset( $this->plugins[dirname( $plugin_file )] ) ) {
131 $plugin_vers = $this->plugins[dirname( $plugin_file )];
132 if( $plugin_vers && ! empty( $plugin_vers ) ) {
133 $links = '';
134 $n = 0;
135 foreach( $plugin_vers['versions'] as $version ) {
136 if( isset( $plugin_vers['zips'][$n] ) ) {
137 $zip_name = $plugin_vers['zips'][$n];
138 /* translators: version of the plugin. */
139 $links .= '<a class="plugin-revision-action" href="#" data-parent_plugin="'.esc_attr( $plugin_file ).'" data-dir="'.esc_attr( $zip_name ).'">'.sprintf( esc_html__( 'Replace with version: %s','plugversions' ), esc_attr( $version ) ).'</a> ';
140 }
141 ++$n;
142 }
143 /* translators: the plugin revisions. */
144 $actions['versions'] = '<span class="plugin-revision-wrp"><a href="#">' . esc_html__( 'Revisions','plugversions' ) . '</a><span class="plugin-revisions-vers">' . rtrim( $links, ' ' ) . '</span></span>';
145
146 }
147 }
148 return $actions;
149 }
150 }