Skip to content

Commit 917c70e

Browse files
committed
added rollback function
1 parent 4b216bb commit 917c70e

File tree

6 files changed

+299
-7
lines changed

6 files changed

+299
-7
lines changed

admin.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
function mfbfw_options_page()
44
{
5-
65
require_once(FBFW_PATH . 'lib/admin-head.php');
6+
77
?>
88

99
<div class="wrap about-wrap full-width-layout">
@@ -77,7 +77,6 @@ function mfbfw_options_page()
7777
class="button-secondary" value="<?php esc_attr_e('Revert to defaults', 'mfbfw'); ?>"/>
7878
<input type="hidden" name="action" value="reset"/>
7979
</div>
80-
</form>
8180

8281
<div id="mfbfwd"
8382
style="border-top:1px dashed #DDDDDD;margin:20px 0 40px;overflow:hidden;padding-top:25px;width:100%;float:left">

assets/js/admin.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
jQuery(function () {
2-
2+
33
// Tabs
44
jQuery("#fbfwTabs").tabs();
55

@@ -130,4 +130,8 @@ function confirmDefaults() {
130130
return false;
131131
}
132132

133-
var defaults_prompt = "Are you sure you want to restore FancyBox for WordPress to default settings?";
133+
var defaults_prompt = "Are you sure you want to restore FancyBox for WordPress to default settings?";
134+
135+
function revertVersion(){
136+
137+
}

assets/js/rollback.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
jQuery(document).ready(function(){
2+
//rollback function
3+
jQuery('.fbfw-rollback-button').on('click', function (event) {
4+
var checkD = confirm('Are you sure you want to reinstall previous version?');
5+
if( checkD == true ){
6+
return true;
7+
} else {
8+
return false;
9+
}
10+
});
11+
});

fancybox.php

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@
2222
define('FBFW_VERSION', '3.0.14');
2323
define('FBFW_PATH', plugin_dir_path(__FILE__));
2424
define('FBFW_URL', plugin_dir_url(__FILE__));
25+
define('FBFW_PLUGIN_BASE', plugin_basename(__FILE__));
26+
define('FBFW_PREVIOUS_PLUGIN_VERSION', '3.0.10');
27+
define('FBFW_FILE_', __FILE__);
28+
define('PLUGIN_NAME', 'fancybox-for-wordpress');
29+
2530

2631
// Get Main Settings
2732
$mfbfw = get_option('mfbfw');
@@ -202,9 +207,9 @@ function mfbfw_init()
202207
}); ';
203208

204209

205-
$mfbfw['customExpression'] = str_replace('"rel"','"data-fancybox"',$mfbfw['customExpression']);
210+
$mfbfw['customExpression'] = str_replace('"rel"', '"data-fancybox"', $mfbfw['customExpression']);
206211

207-
//title position settings
212+
//title position settings
208213
if (isset($mfbfw['titlePosition'])) {
209214
if ($mfbfw['titlePosition'] == 'inside') {
210215
$captionPosition = 'div.fancybox-caption p.caption-title {background:#fff; width:auto;display:inline-block;padding:10px 30px;}';
@@ -358,6 +363,24 @@ function mfbfw_textdomain()
358363

359364
add_action('init', 'mfbfw_textdomain');
360365

366+
367+
/**
368+
* Insert Rollback link for plugin in plugins page
369+
*/
370+
371+
function extra_settings_links($links)
372+
{
373+
374+
if (apply_filters('fbfw_show_rollback_link', true)) {
375+
$links['rollback'] = sprintf('<a href="%s" class="fbfw-rollback-button">%s</a>', wp_nonce_url(admin_url('admin-post.php?action=fbfw_rollback'), 'fbfw_rollback'), __('Rollback version', 'mfbfw'));
376+
}
377+
378+
return $links;
379+
}
380+
381+
add_action('plugin_action_links_' . plugin_basename(__FILE__), 'extra_settings_links');
382+
383+
361384
/**
362385
* Register options
363386
*/
@@ -421,6 +444,10 @@ function mfbfw_admin_scripts()
421444
/**
422445
* Settings Button on Plugins Panel
423446
*/
447+
448+
449+
require FBFW_PATH . '/lib/class-fbfw-plugin-rollback.php';
450+
require FBFW_PATH . '/lib/class-fbfw-rollback.php';
424451
function mfbfw_plugin_action_links($links,
425452
$file)
426453
{
@@ -453,4 +480,5 @@ function hexTorgba($hexColor, $opacity)
453480
$rgb = 'rgba(' . $r . ',' . $g . ',' . $b . ')';
454481
}
455482
return $rgb;
456-
}
483+
}
484+

lib/class-fbfw-plugin-rollback.php

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
3+
class FBFW_Plugin_RollBack {
4+
5+
public function __construct() {
6+
7+
/**
8+
* $_POST action hook
9+
*
10+
* @see: https://codex.wordpress.org/Plugin_API/Action_Reference/admin_post_(action)
11+
*
12+
*/
13+
add_action( 'admin_post_fbfw_rollback', array( $this, 'post_fbfw_rollback' ) );
14+
15+
/**
16+
* Hook responsible for loading our Rollback JS script
17+
*/
18+
add_action( 'admin_enqueue_scripts', array( $this, 'rollback_scripts' ) );
19+
20+
}
21+
22+
/**
23+
* FBFW version rollback.
24+
*
25+
* Rollback to previous {plugin} version.
26+
*
27+
* Fired by `admin_post_epfw_rollback` action.
28+
*
29+
* @since 1.5.0
30+
* @access public
31+
*/
32+
public function post_fbfw_rollback() {
33+
34+
check_admin_referer( 'fbfw_rollback' );
35+
36+
$plugin_slug = basename( FBFW_FILE_, '.php' );
37+
38+
// check for const defines
39+
if ( ! defined( 'FBFW_PREVIOUS_PLUGIN_VERSION' ) || ! defined( 'FBFW_PLUGIN_BASE' ) ) {
40+
wp_die(
41+
new WP_Error( 'broke', esc_html__( 'Previous plugin version or plugin basename CONST aren\'t defined.', 'epfw' ) )
42+
);
43+
}
44+
45+
if ( class_exists( 'FBFW_Rollback' ) ) {
46+
$rollback = new FBFW_Rollback(
47+
[
48+
'version' => FBFW_PREVIOUS_PLUGIN_VERSION,
49+
'plugin_name' => FBFW_PLUGIN_BASE,
50+
'plugin_slug' => $plugin_slug,
51+
'package_url' => sprintf( 'https://downloads.wordpress.org/plugin/%s.%s.zip', PLUGIN_NAME, FBFW_PREVIOUS_PLUGIN_VERSION ),
52+
]
53+
);
54+
$rollback->run();
55+
}
56+
57+
wp_die(
58+
'', __( 'Rollback to Previous Version', 'mfbfw' ), [
59+
'response' => 200,
60+
]
61+
);
62+
}
63+
64+
public function rollback_scripts() {
65+
wp_enqueue_script('rollback-script', FBFW_URL . 'assets/js/rollback.js', FBFW_VERSION); // Load Rollback script
66+
wp_enqueue_script( 'rollback-script' );
67+
68+
}
69+
70+
}
71+
72+
new FBFW_Plugin_Rollback();

lib/class-fbfw-rollback.php

Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
<?php
2+
3+
if ( ! defined( 'ABSPATH' ) ) {
4+
exit; // Exit if accessed directly.
5+
}
6+
7+
/**
8+
* EPFW rollback class.
9+
*
10+
* EPFW rollback handler class is responsible for rolling back EPFW to
11+
* previous version.
12+
*
13+
* @since 1.0.0
14+
*/
15+
class FBFW_Rollback {
16+
17+
/**
18+
* Package URL.
19+
*
20+
* Holds the package URL.
21+
*
22+
* @since 1.0.0
23+
* @access protected
24+
*
25+
* @var string Package URL.
26+
*/
27+
protected $package_url;
28+
29+
/**
30+
* Version.
31+
*
32+
* Holds the version.
33+
*
34+
* @since 1.0.0
35+
* @access protected
36+
*
37+
* @var string Package URL.
38+
*/
39+
protected $version;
40+
41+
/**
42+
* Plugin name.
43+
*
44+
* Holds the plugin name.
45+
*
46+
* @since 1.0.0
47+
* @access protected
48+
*
49+
* @var string Plugin name.
50+
*/
51+
protected $plugin_name;
52+
53+
/**
54+
* Plugin slug.
55+
*
56+
* Holds the plugin slug.
57+
*
58+
* @since 1.0.0
59+
* @access protected
60+
*
61+
* @var string Plugin slug.
62+
*/
63+
protected $plugin_slug;
64+
65+
/**
66+
* Rollback constructor.
67+
*
68+
* Initializing EPFW rollback.
69+
*
70+
* @since 1.0.0
71+
* @access public
72+
*
73+
* @param array $args Optional. Rollback arguments. Default is an empty array.
74+
*/
75+
public function __construct( $args = [] ) {
76+
foreach ( $args as $key => $value ) {
77+
$this->{$key} = $value;
78+
}
79+
}
80+
81+
/**
82+
* Print inline style.
83+
*
84+
* Add an inline CSS to the rollback page.
85+
*
86+
* @since 1.0.0
87+
* @access private
88+
*/
89+
private function print_inline_style() {
90+
?>
91+
<style>
92+
.wrap {
93+
overflow: hidden;
94+
}
95+
96+
h1 {
97+
background: #9b0a46;
98+
text-align: center;
99+
color: #fff !important;
100+
padding: 70px !important;
101+
text-transform: uppercase;
102+
letter-spacing: 1px;
103+
}
104+
105+
h1 img {
106+
max-width: 300px;
107+
display: block;
108+
margin: auto auto 50px;
109+
}
110+
</style>
111+
<?php
112+
}
113+
114+
/**
115+
* Apply package.
116+
*
117+
* Change the plugin data when WordPress checks for updates. This method
118+
* modifies package data to update the plugin from a specific URL containing
119+
* the version package.
120+
*
121+
* @since 1.0.0
122+
* @access protected
123+
*/
124+
protected function apply_package() {
125+
$update_plugins = get_site_transient( 'update_plugins' );
126+
if ( ! is_object( $update_plugins ) ) {
127+
$update_plugins = new \stdClass();
128+
}
129+
130+
$plugin_info = new \stdClass();
131+
$plugin_info->new_version = $this->version;
132+
$plugin_info->slug = $this->plugin_slug;
133+
$plugin_info->package = $this->package_url;
134+
135+
$update_plugins->response[ $this->plugin_name ] = $plugin_info;
136+
137+
set_site_transient( 'update_plugins', $update_plugins );
138+
}
139+
140+
/**
141+
* Upgrade.
142+
*
143+
* Run WordPress upgrade to rollback EPFW to previous version.
144+
*
145+
* @since 1.0.0
146+
* @access protected
147+
*/
148+
protected function upgrade() {
149+
require_once( ABSPATH . 'wp-admin/includes/class-wp-upgrader.php' );
150+
151+
$logo_url = FBFW_URL . 'assets/images/icon.jpg';
152+
153+
$upgrader_args = [
154+
'url' => 'update.php?action=upgrade-plugin&plugin=' . rawurlencode( $this->plugin_name ),
155+
'plugin' => $this->plugin_name,
156+
'nonce' => 'upgrade-plugin_' . $this->plugin_name,
157+
'title' => '<img src="' . $logo_url . '" alt="FBFW logo">' . __( 'Rollback to Previous Version', 'epfw' ),
158+
];
159+
160+
$this->print_inline_style();
161+
162+
$upgrader = new \Plugin_Upgrader( new \Plugin_Upgrader_Skin( $upgrader_args ) );
163+
$upgrader->upgrade( $this->plugin_name );
164+
}
165+
166+
/**
167+
* Run.
168+
*
169+
* Rollback EPFW to previous versions.
170+
*
171+
* @since 1.0.0
172+
* @access public
173+
*/
174+
public function run() {
175+
$this->apply_package();
176+
$this->upgrade();
177+
}
178+
}

0 commit comments

Comments
 (0)