PluginProbe
Tableberg – Simple Gutenberg Table Block / 0.4.1
Tableberg – Simple Gutenberg Table Block v0.4.1
1.1.4 1.1.3 1.1.2 1.1.1 1.1.0 1.0.5 1.0.4 1.0.3 1.0.2 1.0.1 trunk 0.0.2 0.2.1 0.3.2 0.3.3 0.4.1 0.5.0 0.5.1 0.5.2 0.5.3 0.5.4 0.5.5 0.5.6 0.5.7 0.5.8 All 41 releases
tableberg / includes / Version_Control.php

Version_Control.php in Tableberg – Simple Gutenberg Table Block 0.4.1, at includes/Version_Control.php

306 lines 7.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Version control manager
4 *
5 * @package Tableberg
6 */
7
8 namespace Tableberg;
9
10 use Tableberg\includes\Version_Control_Upgrader_Skin;
11 use Exception;
12 use Plugin_Upgrader;
13 use Tableberg\includes\Version_Sync_Base;
14 require_once TABLEBERG_DIR_PATH . 'includes/Version_Sync_Base.php';
15 use Tableberg\includes\traits\Ajax_Response_Trait;
16 require_once TABLEBERG_DIR_PATH . 'includes/traits/Ajax_Response_Trait.php';
17 use Tableberg\includes\traits\Manager_Base_Trait;
18 require_once TABLEBERG_DIR_PATH . 'includes/traits/Manager_Base_Trait.php';
19 use WP_Error;
20 use function activate_plugin;
21 use function add_filter;
22 use function check_ajax_referer;
23 use function current_user_can;
24 use function get_admin_url;
25 use function get_plugin_data;
26 use function is_wp_error;
27 use function plugins_api;
28 use function wp_create_nonce;
29
30 /**
31 * Manager responsible for version related operations.
32 */
33 class Version_Control extends Version_Sync_Base {
34 use Manager_Base_Trait;
35 use Ajax_Response_Trait;
36
37 const VERSION_ROLLBACK_AJAX_ACTION = 'tableberg_version_control';
38
39 /**
40 * Main process that will be called during initialization of manager.
41 *
42 * @return void
43 */
44 protected function init_process() {
45 add_filter( 'tableberg/filter/admin_settings_menu_data', array( $this, 'add_settings_menu_data' ) );
46 add_action( 'wp_ajax_' . self::VERSION_ROLLBACK_AJAX_ACTION, array( $this, 'ajax_version_rollback' ) );
47
48 // subscribe to version sync.
49 $this->subscribe_to_version_sync();
50 }
51
52 /**
53 * Handle ajax version rollback requests.
54 *
55 * @return void
56 */
57 public function ajax_version_rollback() {
58 if ( current_user_can( 'manage_options' ) && check_ajax_referer(
59 self::VERSION_ROLLBACK_AJAX_ACTION,
60 'nonce',
61 false
62 ) ) {
63
64 try {
65 if ( ! isset( $_POST['version'] ) ) {
66 throw new Exception( esc_html__( 'Invalid request body.', 'tableberg' ) );
67 }
68
69 $target_version = $_POST['version'];
70 $current_plugin_version = $this->current_plugin_version();
71
72 if ( $target_version === $current_plugin_version ) {
73 throw new Exception( esc_html__( 'You are on the same plugin version.', 'tableberg' ) );
74 }
75
76 $available_versions = $this->get_plugin_versions_info();
77
78 if ( ! in_array( $target_version, array_keys( $available_versions ) ) ) {
79 throw new Exception( esc_html__( 'Target version is out of bounds.', 'tableberg' ) );
80 }
81
82 require_once ABSPATH . 'wp-admin/includes/file.php';
83 require_once ABSPATH . 'wp-admin/includes/misc.php';
84 require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
85 require_once ABSPATH . 'wp-admin/includes/class-plugin-upgrader.php';
86 require_once ABSPATH . 'wp-admin/includes/plugin-install.php';
87
88 $plugin_remote_info = plugins_api(
89 'plugin_information',
90 array(
91 'slug' => $this->get_version_slug(),
92 )
93 );
94
95 $download_url = $available_versions[ $target_version ];
96 $upgrader = new Plugin_Upgrader( new Version_Control_Upgrader_Skin( array(), $plugin_remote_info ) );
97
98 add_filter(
99 'upgrader_package_options',
100 function ( $options ) use ( $target_version ) {
101 $options['abort_if_destination_exists'] = false;
102 $options['hook_extra'] = array_merge(
103 $options['hook_extra'],
104 array(
105 'plugin' => 'tableberg/tableberg.php',
106 'version' => $target_version,
107 )
108 );
109
110 return $options;
111 }
112 );
113 $install_result = $upgrader->install( $download_url, array( 'overwrite_package' => true ) );
114 if ( is_wp_error( $install_result ) || false === $install_result ) {
115 throw new Exception(
116 esc_html__(
117 'An error occurred during rollback operation, try again later.',
118 'tableberg'
119 )
120 );
121 } else {
122 activate_plugin( 'tableberg/tableberg.php' );
123 $this->set_message(
124 esc_html__(
125 sprintf(
126 'Plugin version %1$s installed successfully.',
127 $target_version
128 ),
129 'tableberg'
130 )
131 );
132 }
133 } catch ( Exception $e ) {
134 $this->set_error( $e->getMessage() );
135 }
136 } else {
137 $this->set_error( esc_html__( 'You are not authorized to use this ajax endpoint.', 'tableberg' ) );
138 }
139
140 $this->send_json();
141 }
142
143 /**
144 * Get plugin versions info.
145 *
146 * @return array versions info
147 */
148 private function get_plugin_versions_info() {
149 require_once ABSPATH . 'wp-admin/includes/plugin-install.php';
150
151 $plugin_remote_info = plugins_api(
152 'plugin_information',
153 array(
154 'slug' => 'tableberg',
155 )
156 );
157
158 if ( is_wp_error( $plugin_remote_info ) ) {
159 $all_versions = array();
160 } else {
161 $all_versions = $plugin_remote_info->versions;
162 unset( $all_versions['trunk'] );
163 }
164
165 return array_slice( array_reverse( $all_versions, true ), 0, 5, true );
166 }
167
168 /**
169 * Current version of our plugin.
170 *
171 * @return string version
172 */
173 private function current_plugin_version() {
174 return get_plugin_data( trailingslashit( TABLEBERG_DIR_PATH ) . 'tableberg.php' )['Version'];
175 }
176
177 /**
178 * Add version control related data to settings menu frontend.
179 *
180 * @param array $data data.
181 *
182 * @return array filtered data
183 */
184 public function add_settings_menu_data( $data ) {
185 $current_version = $this->current_plugin_version();
186
187 $versions = array_keys( $this->get_plugin_versions_info() );
188
189 $data['versionControl'] = array(
190 'currentVersion' => $current_version,
191 'versions' => $versions,
192 'ajax' => array(
193 'versionRollback' => array(
194 'url' => get_admin_url( null, 'admin-ajax.php' ),
195 'action' => self::VERSION_ROLLBACK_AJAX_ACTION,
196 'nonce' => wp_create_nonce( self::VERSION_ROLLBACK_AJAX_ACTION ),
197 ),
198 ),
199 );
200
201 return $data;
202 }
203
204 /**
205 * Get slug of plugin/addon used in its distribution API.
206 *
207 * @return string slug
208 */
209 public function get_version_slug() {
210 return 'tableberg';
211 }
212
213 /**
214 * Get text domain of the plugin.
215 *
216 * It will be used for ajax upgraders to identify our plugin since slug is not supplied in plugin info property of that upgrader skin.
217 *
218 * @return string
219 */
220 public function get_text_domain() {
221 return 'tableberg';
222 }
223
224 /**
225 * Parse version number from package url.
226 *
227 * @param string $package package url.
228 *
229 * @return string|null version number
230 */
231 public function parse_version_from_package( $package ) {
232 $parsed_version = null;
233 $match = array();
234
235 preg_match( '/^.+(?:tableberg)\.(.+)(?:\..+)$/', $package, $match );
236
237 if ( $match[1] ) {
238 $parsed_version = $match[1];
239 }
240
241 return $parsed_version;
242 }
243
244 /**
245 * Plugin __FILE__
246 *
247 * @return string plugin file
248 */
249 public function plugin_file() {
250 return TABLEBERG_PLUGIN_FILE;
251 }
252
253 /**
254 * Callback hook for version sync manager when a subscriber attempted an installation operation.
255 *
256 * @param string $slug subscriber slug.
257 * @param string $version version to install.
258 *
259 * @return false|WP_Error false to permit install(I know, but it is what it is) or WP_Error to cancel it
260 */
261 public function version_sync_logic( $slug, $version ) {
262 $final_status = false;
263
264 if ( 'tableberg-pro' === $slug ) {
265 $final_status = $this->generic_sync_logic( $slug, $version );
266 }
267
268 return $final_status;
269 }
270
271 /**
272 * Plugin specific logic for fetching versions and their info.
273 *
274 * Use plugin version for keys and info for their values. Use 'url' property key for download link.
275 *
276 * @return array|WP_Error versions array
277 */
278 protected function get_plugin_versions() {
279 require_once ABSPATH . 'wp-admin/includes/plugin-install.php';
280
281 $versions = new WP_Error(
282 501,
283 esc_html__(
284 'An error occurred while fetching wp-table-builder versions, please try again later',
285 'wp-table-builder'
286 )
287 );
288
289 $info = (array) plugins_api( 'plugin_information', array( 'slug' => $this->get_version_slug() ) );
290
291 if ( isset( $info['versions'] ) ) {
292 $versions = array_reduce(
293 array_keys( $info['versions'] ),
294 function ( $carry, $key ) use ( $info ) {
295 $carry[ $key ] = array( 'url' => $info['versions'] [ $key ] );
296
297 return $carry;
298 },
299 array()
300 );
301 }
302
303 return $versions;
304 }
305 }
306