-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtws-admin-onboarding.php
172 lines (154 loc) · 5.29 KB
/
tws-admin-onboarding.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
<?php //phpcs:ignore WordPress.NamingConventions
/**
* Plugin Name: The Web Solver Onboarding Wizard
* Description: Boilerplate for enabling The Web Solver WordPress Admin Onboarding Wizard.
* Version: 1.0
* Author: TheWebSolver
*
* @package TheWebSolver\Core\Admin\Onboarding\Class
* @todo Configured to be tested as a WordPress plugin.
* Must create own plugin file and delete this file
* after implementing codes here inside own plugin file.
* TODO: Make changes to codes with {@todo} tags, where applicable.
*
* -----------------------------------
* DEVELOPED-MAINTAINED-SUPPPORTED BY
* -----------------------------------
* ███║ ███╗ ████████████████
* ███║ ███║ ═════════██████╗
* ███║ ███║ ╔══█████═╝
* ████████████║ ╚═█████
* ███║═════███║ █████╗
* ███║ ███║ █████═╝
* ███║ ███║ ████████████████╗
* ╚═╝ ╚═╝ ═══════════════╝
*/
/**
* Onboarding namespace.
*
* @todo MUST REPLACE AND USE OWN NAMESPACE.
*/
namespace My_Plugin\My_Feature;
/**
* Boilerplate plugin for The Web Solver WordPress Admin Onboarding Wizard.
*/
final class Onboarding {
/**
* Onboarding wizard prefix.
*
* @var string
* @todo Prefix for onboarding wizard. DO NOT CHANGE IT ONCE SET.\
* It will be used for WordPress Hooks, Options, Transients, etc.\
* MUST BE A UNIQUE PREFIX FOR YOUR PLUGIN.
*/
public $prefix = 'thewebsolver';
/**
* Onboarding Wizard Config.
*
* @var Config
*/
public $config;
/**
* Starts Onboarding.
*
* @return Onboarding
*/
public static function start() {
static $onboarding;
if ( ! is_a( $onboarding, get_class() ) ) {
$onboarding = new self();
}
return $onboarding;
}
/**
* Private constructor to prevent direct instantiation.
*/
private function __construct() {
$this->config();
register_activation_hook( __FILE__, array( $this, 'activate' ) );
register_deactivation_hook( __FILE__, array( $this, 'deactivate' ) );
/**
* If all onboarding steps are not completed, show admin notice.
*
* At last step of onboarding, $status => 'complete'.
*
* @var string
*
* @todo Need to perform additional checks before showing notice
* Such as show notice on plugins, themes and dashboard pages only.
*/
$status = get_option( $this->prefix . '_onboarding_steps_status' );
if ( 'pending' === $status ) {
add_action( 'admin_notices', array( $this, 'onboarding_notice' ) );
}
}
/**
* Instantiates onboarding config.
*/
private function config() {
// Onboarding config file path.
include_once __DIR__ . '/Config.php';
$config = array( '\\' . __NAMESPACE__ . '\\Config', 'get' );
// Only call config if it is on the same namespace.
if ( is_callable( $config ) ) {
$this->config = call_user_func( $config, $this->prefix );
}
}
/**
* Sets onboarding notice if not completed.
*/
public function onboarding_notice() {
$msg = sprintf(
'<p><b>%1$s</b> - %2$s.</p><p><a href="%3$s" class="button-primary">%4$s</a></p>',
__( 'Namaste! from The Web Solver Onboarding Wizard', 'tws-onboarding' ),
__( 'Let us help you quickly setup the plugin with our onboarding wizard', 'tws-onboarding' ),
admin_url( 'admin.php?page=' . $this->config->get_page() ),
__( 'Run the Wizard Now', 'tws-onboarding' )
);
echo '<div class="notice notice-info">' . wp_kses_post( $msg ) . '</div>';
}
/**
* Performs task on plugin activation.
*
* @todo Configured with example codes. Make changes as needed.
*/
public function activate() {
// Check if plugin is already installed.
$old_install = get_option( $this->prefix . '_install_version', false );
if ( ! $old_install ) {
// if new install => enable onboarding.
$check[] = 'true';
// Set the plugin install version to "1.0".
update_option( $this->prefix . '_install_version', '1.0' );
} else {
// There is now installed version "1.0" => disable onboarding.
$check[] = 'false';
}
// If PHP version less than or equal to "7.0" => disable onboarding.
if ( version_compare( phpversion(), '7.0', '<=' ) ) {
$check[] = 'false';
}
// Now onboarding will run on the basis of check parameter passed.
// If this is first activation or PHP > 7.0 => redirect to onboarding page.
// Lets also verify if config has been instantiated.
if ( is_object( $this->config ) ) {
$this->config->enable_onboarding( $check );
}
}
/**
* Performs task on plugin deactivation.
*
* @todo Configured to delete onboarding options on plugin deactivation.\
* Cane be safely deleted for production.
*/
public function deactivate() {
// Onboarding options.
delete_option( $this->prefix . '_onboarding_steps_status' );
delete_option( $this->prefix . '_onboarding_dependency_status' );
delete_option( $this->prefix . '_onboarding_dependency_name' );
delete_option( $this->prefix . '_install_version' );
// Onboarding transitents.
delete_transient( $this->prefix . '_onboarding_redirect' );
}
}
Onboarding::start();