Completed
Pull Request — master (#1653)
by Aristeides
04:02 queued 01:59
created

Kirki_Installer_Section::activate_button()   A

Complexity

Conditions 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
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
					// @codingStandardsIgnoreLine WordPress.VIP.RestrictedFunctions.user_meta_get_user_meta)
92
					$user_meta = get_user_meta( $user_id, 'dismiss-kirki-recommendation', true );
93
94
					return ( true === $user_meta || '1' === $user_meta || 1 === $user_meta );
95
				}
96
97
				/**
98
				 * Adds the install button.
99
				 *
100
				 * @since 1.0.0
101
				 * @return void
102
				 */
103
				protected function install_button() {
104
					?>
105
					<p style="text-align:left;margin-top:0;">
106
						<?php esc_attr_e( 'Please install the Kirki plugin to take full advantage of this theme\s customizer capabilities', 'textdomain' ); ?>
107
					</p>
108
					<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">
109
						<?php esc_html_e( 'Install Now', 'textdomain' ); ?>
110
					</a>
111
					<?php
112
				}
113
114
				/**
115
				 * Adds the install button.
116
				 *
117
				 * @since 1.0.0
118
				 * @return void
119
				 */
120
				protected function activate_button() {
121
					?>
122
					<p style="text-align:left;margin-top:0;">
123
						<?php esc_attr_e( 'You have installed Kirki. Activate it to take advantage of this theme\'s features in the customizer.', 'textdomain' ); ?>
124
					</p>
125
					<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">
126
						<?php esc_html_e( 'Activate Now', 'textdomain' ); ?>
127
					</a>
128
					<?php
129
				}
130
131
				/**
132
				 * Adds the dismiss button and script.
133
				 *
134
				 * @since 1.0.0
135
				 * @return void
136
				 */
137
				protected function dismiss_button() {
138
139
					// Create the nonce.
140
					$ajax_nonce = wp_create_nonce( 'dismiss-kirki-recommendation' );
141
142
					// Show confirmation dialog on dismiss?
143
					$show_confirm = true;
144
					?>
145
					<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' ); ?>">
146
						<?php esc_attr_e( 'Don\'t show this again', 'textdomain' ); ?>
147
					</a>
148
149
					<script type="text/javascript">
150
					jQuery( document ).ready( function() {
151
						jQuery( '.kirki-installer-dismiss' ).on( 'click', function( event ) {
152
153
							event.preventDefault();
154
155
							<?php if ( $show_confirm ) : ?>
156
								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.' ); ?>' ) ) {
157
									return;
158
								}
159
							<?php endif; ?>
160
161
							jQuery.post( ajaxurl, {
162
								action: 'kirki_installer_dismiss',
163
								security: '<?php echo esc_attr( $ajax_nonce ); ?>',
164
							}, function( response ) {
165
								jQuery( '#accordion-section-kirki_installer' ).remove();
166
							} );
167
						} );
168
					} );
169
					</script>
170
					<?php
171
				}
172
			}
173
		}
174
175
		// Early exit if the user has dismissed the notice.
176
		if ( is_callable( array( 'Kirki_Installer_Section', 'is_dismissed' ) ) && Kirki_Installer_Section::is_dismissed() ) {
177
			return;
178
		}
179
180
		$wp_customize->add_section(
181
			new Kirki_Installer_Section(
182
				$wp_customize, 'kirki_installer', array(
183
					'title'      => '',
184
					'capability' => 'install_plugins',
185
					'priority'   => 0,
186
				)
187
			)
188
		);
189
		$wp_customize->add_setting(
190
			'kirki_installer_setting', array(
191
				'sanitize_callback' => '__return_true',
192
			)
193
		);
194
		$wp_customize->add_control(
195
			'kirki_installer_control', array(
196
				'section'    => 'kirki_installer',
197
				'settings'   => 'kirki_installer_setting',
198
			)
199
		);
200
201
	}
202
}
203
add_action( 'customize_register', 'kirki_installer_register', 999 );
204
205
if ( ! function_exists( 'kirki_installer_dismiss' ) ) {
206
	/**
207
	 * Handles dismissing the plgin-install/activate recommendation.
208
	 * If the user clicks the "Don't show this again" button, save as user-meta.
209
	 *
210
	 * @since 1.0.0
211
	 * @return void
212
	 */
213
	function kirki_installer_dismiss() {
214
		check_ajax_referer( 'dismiss-kirki-recommendation', 'security' );
215
		$user_id   = get_current_user_id();
216
		// @codingStandardsIgnoreLine WordPress.VIP.RestrictedFunctions.user_meta_update_user_meta
217
		if ( update_user_meta( $user_id, 'dismiss-kirki-recommendation', true ) ) {
218
			echo 'success! :-)';
219
			wp_die();
220
		}
221
		echo 'failed :-(';
222
		wp_die();
223
	}
224
}
225
add_action( 'wp_ajax_kirki_installer_dismiss', 'kirki_installer_dismiss' );
226