Conditions | 6 |
Paths | 5 |
Total Lines | 187 |
Code Lines | 81 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
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 | } |
||
226 |