Completed
Pull Request — master (#1653)
by Aristeides
02:08
created

class-kirki-installer-section.php ➔ kirki_installer_dismiss()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 8
nc 2
nop 0
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file adds a custom section in the customizer that recommends the installation of the Kirki plugin.
4
 * It can be used as-is in themes (drop-in).
5
 *
6
 * @package kirki-helpers
7
 */
8
9
if ( ! function_exists( 'kirki_installer_register' ) ) {
10
	/**
11
	 * Registers the section, setting & control for the kirki installer.
12
	 *
13
	 * @param object $wp_customize The main customizer object.
14
	 */
15
	function kirki_installer_register( $wp_customize ) {
16
17
		// Early exit if Kirki exists.
18
		if ( class_exists( 'Kirki' ) ) {
19
			return;
20
		}
21
22
		if ( class_exists( 'WP_Customize_Section' ) && ! class_exists( 'Kirki_Installer_Section' ) ) {
23
			/**
24
			 * Recommend the installation of Kirki using a custom section.
25
			 *
26
			 * @see WP_Customize_Section
27
			 */
28
			class Kirki_Installer_Section extends WP_Customize_Section {
29
30
				/**
31
				 * Customize section type.
32
				 *
33
				 * @access public
34
				 * @var string
35
				 */
36
				public $type = 'kirki_installer';
37
38
				/**
39
				 * Render the section.
40
				 *
41
				 * @access protected
42
				 */
43
				protected function render() {
44
45
					// Don't proceed any further if the user has dismissed this.
46
					if ( $this->is_dismissed() ) {
47
						return;
48
					}
49
50
					// Determine if the plugin is not installed, or just inactive.
51
					$plugins   = get_plugins();
52
					$installed = false;
53
					foreach ( $plugins as $plugin ) {
54
						if ( 'Kirki' === $plugin['Name'] || 'Kirki Toolkit' === $plugin['Name'] ) {
55
							$installed = true;
56
						}
57
					}
58
					// Get the plugin-installation URL.
59
					$plugin_install_url = add_query_arg(
60
						array(
61
							'action' => 'install-plugin',
62
							'plugin' => 'kirki',
63
						),
64
						self_admin_url( 'update.php' )
65
					);
66
					$plugin_install_url = wp_nonce_url( $plugin_install_url, 'install-plugin_kirki' );
67
					$classes = 'cannot-expand accordion-section control-section control-section-themes control-section-' . $this->type;
68
					?>
69
					<li id="accordion-section-<?php echo esc_attr( $this->id ); ?>" class="<?php echo esc_attr( $classes ); ?>" style="border-top:none;border-bottom:1px solid #ddd;padding:7px 14px 16px 14px;text-align:right;">
70
						<?php if ( ! $installed ) : ?>
71
							<?php $this->install_button(); ?>
72
						<?php else : ?>
73
							<?php $this->activate_button(); ?>
74
						<?php endif; ?>
75
						<?php $this->dismiss_button(); ?>
76
					</li>
77
					<?php
78
				}
79
80
				/**
81
				 * Check if the user has chosen to hide this.
82
				 *
83
				 * @static
84
				 * @access public
85
				 * @since 1.0.0
86
				 * @return bool
87
				 */
88
				public static function is_dismissed() {
89
					// Get the user-meta.
90
					$user_id   = get_current_user_id();
91
					$user_meta = get_user_meta( $user_id, 'dismiss-kirki-recommendation', true );
92
93
					return ( true === $user_meta || '1' === $user_meta || 1 === $user_meta );
94
				}
95
96
				/**
97
				 * Adds the install button.
98
				 *
99
				 * @since 1.0.0
100
				 * @return void
101
				 */
102
				protected function install_button() {
103
					?>
104
					<p style="text-align:left;margin-top:0;">
105
						<?php esc_attr_e( 'Please install the Kirki plugin to take full advantage of this theme\s customizer capabilities', 'textdomain' ); ?>
106
					</p>
107
					<a class="install-now button-primary button" data-slug="kirki" href="<?php echo esc_url_raw( $plugin_install_url ); ?>" aria-label="<?php esc_attr_e( 'Install Kirki Toolkit now', 'textdomain' ); ?>" data-name="Kirki Toolkit">
108
						<?php esc_html_e( 'Install Now', 'textdomain' ); ?>
109
					</a>
110
					<?php
111
				}
112
113
				/**
114
				 * Adds the install button.
115
				 *
116
				 * @since 1.0.0
117
				 * @return void
118
				 */
119
				protected function activate_button() {
120
					?>
121
					<p style="text-align:left;margin-top:0;">
122
						<?php esc_attr_e( 'You have installed Kirki. Activate it to take advantage of this theme\'s features in the customizer.', 'textdomain' ); ?>
123
					</p>
124
					<a class="activate-now button-primary button" data-slug="kirki" href="<?php echo esc_url_raw( self_admin_url( 'plugins.php' ) ); ?>" aria-label="<?php esc_attr_e( 'Activate Kirki Toolkit now', 'textdomain' ); ?>" data-name="Kirki Toolkit">
125
						<?php esc_html_e( 'Activate Now', 'textdomain' ); ?>
126
					</a>
127
					<?php
128
				}
129
130
				/**
131
				 * Adds the dismiss button and script.
132
				 *
133
				 * @since 1.0.0
134
				 * @return void
135
				 */
136
				protected function dismiss_button() {
137
138
					// Create the nonce.
139
					$ajax_nonce = wp_create_nonce( 'dismiss-kirki-recommendation' );
140
141
					// Show confirmation dialog on dismiss?
142
					$show_confirm = true;
143
					?>
144
					<a class="kirki-installer-dismiss button-secondary button" data-slug="kirki" href="#" aria-label="<?php esc_attr_e( 'Don\'t show this again', 'textdomain' ); ?>" data-name="<?php esc_attr_e( 'Dismiss', 'textdomain' ); ?>">
145
						<?php esc_attr_e( 'Don\'t show this again', 'textdomain' ); ?>
146
					</a>
147
148
					<script type="text/javascript">
149
					jQuery( document ).ready( function() {
150
						jQuery( '.kirki-installer-dismiss' ).on( 'click', function( event ) {
151
152
							event.preventDefault();
153
154
							<?php if ( $show_confirm ) : ?>
155
								if ( ! confirm( '<?php esc_attr_e( 'Are you sure? Dismissing this message will hide the installation recommendation and you will have to manually install and activate the plugin in the future.' ); ?>' ) ) {
156
									return;
157
								}
158
							<?php endif; ?>
159
160
							jQuery.post( ajaxurl, {
161
								action: 'kirki_installer_dismiss',
162
								security: '<?php echo $ajax_nonce; ?>',
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$ajax_nonce'
Loading history...
163
							}, function( response ) {
164
								jQuery( '#accordion-section-kirki_installer' ).remove();
165
							} );
166
						} );
167
					} );
168
					</script>
169
					<?php
170
				}
171
			}
172
		}
173
174
		// Early exit if the user has dismissed the notice.
175
		if ( is_callable( array( 'Kirki_Installer_Section', 'is_dismissed' ) ) && Kirki_Installer_Section::is_dismissed() ) {
176
			return;
177
		}
178
179
		$wp_customize->add_section(
180
			new Kirki_Installer_Section(
181
				$wp_customize, 'kirki_installer', array(
182
					'title'      => '',
183
					'capability' => 'install_plugins',
184
					'priority'   => 0,
185
				)
186
			)
187
		);
188
		$wp_customize->add_setting(
189
			'kirki_installer_setting', array(
190
				'sanitize_callback' => '__return_true',
191
			)
192
		);
193
		$wp_customize->add_control(
194
			'kirki_installer_control', array(
195
				'section'    => 'kirki_installer',
196
				'settings'   => 'kirki_installer_setting',
197
			)
198
		);
199
200
	}
201
}
202
add_action( 'customize_register', 'kirki_installer_register', 999 );
203
204
if ( ! function_exists( 'kirki_installer_dismiss' ) ) {
205
	/**
206
	 * Handles dismissing the plgin-install/activate recommendation.
207
	 * If the user clicks the "Don't show this again" button, save as user-meta.
208
	 *
209
	 * @since 1.0.0
210
	 * @return void
211
	 */
212
	function kirki_installer_dismiss() {
213
		check_ajax_referer( 'dismiss-kirki-recommendation', 'security' );
214
		$user_id   = get_current_user_id();
215
		if ( update_user_meta( $user_id, 'dismiss-kirki-recommendation', true ) ) {
216
			echo 'success! :-)';
217
			wp_die();
218
		}
219
		echo 'failed :-(';
220
		wp_die();
221
	}
222
}
223
add_action( 'wp_ajax_kirki_installer_dismiss', 'kirki_installer_dismiss' );
224