Skip to content

Commit 48e16b0

Browse files
authored
v1.2 - Merge pull request #2 from JoryHogeveen/dev
v1.2
2 parents 48b59f0 + a76da19 commit 48e16b0

File tree

3 files changed

+447
-53
lines changed

3 files changed

+447
-53
lines changed

genesis-widget-column-classes.php

Lines changed: 160 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* Plugin Name: Genesis Widget Column Classes
77
* Description: Add Genesis (old Bootstrap) column classes to widgets
88
* Plugin URI: https://wordpress.org/plugins/genesis-widget-column-classes/
9-
* Version: 1.1.4
9+
* Version: 1.2
1010
* Author: Jory Hogeveen
1111
* Author URI: http://www.keraweb.nl
1212
* Text Domain: genesis-widget-column-classes
@@ -33,12 +33,12 @@
3333
* MA 02110-1301, USA.
3434
*
3535
*/
36-
36+
3737
! defined( 'ABSPATH' ) and die( 'You shall not pass!' );
3838

3939
if ( ! class_exists( 'WCC_Genesis_Widget_Column_Classes' ) ) {
40-
41-
final class WCC_Genesis_Widget_Column_Classes
40+
41+
final class WCC_Genesis_Widget_Column_Classes
4242
{
4343

4444
/**
@@ -55,7 +55,7 @@ final class WCC_Genesis_Widget_Column_Classes
5555
* @since 1.1
5656
* @var string
5757
*/
58-
private $version = '1.1.4';
58+
private $version = '1.2';
5959

6060
/**
6161
* User ignore nag key
@@ -64,35 +64,35 @@ final class WCC_Genesis_Widget_Column_Classes
6464
* @var string
6565
*/
6666
private $noticeKey = 'wcc_ignore_genesis_notice';
67-
67+
6868
/**
6969
* Array of possible column classes
7070
*
7171
* @since 1.1.4
7272
* @var array
73-
*/
74-
private $column_classes = array(
75-
'one-half',
76-
'one-third',
77-
'one-fourth',
78-
'one-sixth',
79-
'two-thirds',
80-
'two-fourths',
73+
*/
74+
private $column_classes = array(
75+
'one-half',
76+
'one-third',
77+
'one-fourth',
78+
'one-sixth',
79+
'two-thirds',
80+
'two-fourths',
8181
'two-sixths',
8282
'three-fourths',
8383
'three-sixths',
8484
'four-sixths',
8585
'five-sixths'
8686
);
87-
87+
8888
/**
8989
* Current user object
9090
*
9191
* @since 1.1
9292
* @var object
93-
*/
93+
*/
9494
private $curUser = false;
95-
95+
9696
/**
9797
* Init function to register plugin hook
9898
*
@@ -102,11 +102,11 @@ final class WCC_Genesis_Widget_Column_Classes
102102
*/
103103
private function __construct() {
104104
self::$_instance = $this;
105-
105+
106106
// Lets start!
107107
add_action( 'init', array( $this, 'init' ) );
108108
}
109-
109+
110110
/**
111111
* Main Genesis Widget Column Classes Instance.
112112
*
@@ -133,30 +133,30 @@ public static function get_instance() {
133133
* @return void
134134
*/
135135
public function init() {
136-
136+
137137
// Get the current user
138138
$this->curUser = wp_get_current_user();
139139

140140
/**
141141
* Change the default column classes
142-
*
142+
*
143143
* @since 1.1.4
144144
* @param array The column classes
145145
*/
146146
$this->column_classes = apply_filters( 'genesis_widget_column_classes', $this->column_classes );
147147

148148
if ( isset( $this->curUser->ID ) ) {
149-
add_action( 'admin_notices', array( $this, 'genesis_notice' ) );
149+
add_action( 'admin_notices', array( $this, 'genesis_notice' ) );
150150
add_action( 'wp_ajax_'.$this->noticeKey, array( $this, 'ignore_genesis_notice' ) );
151151
}
152152
// widget_form_callback instead of in_widget_form because we want these fields to show BEFORE the other fields
153153
add_filter( 'widget_form_callback', array( $this, 'widget_form_extend' ), 10, 2 );
154154
add_filter( 'widget_update_callback', array( $this, 'widget_update' ), 10, 2 );
155155
add_filter( 'dynamic_sidebar_params', array( $this, 'sidebar_params' ), 99999 ); // Make sure to be the last one
156-
add_action( 'plugins_loaded', array( $this, 'load_textdomain' ) );
156+
add_action( 'init', array( $this, 'load_textdomain' ) );
157157

158158
}
159-
159+
160160
/**
161161
* Add notice when theme is nog based on the Genesis Framework
162162
* Checks for version in the notice ignore meta value. If the version is the same (user has clicked ignore), then hide it
@@ -177,7 +177,7 @@ public function genesis_notice() {
177177
}
178178
}
179179
}
180-
180+
181181
/**
182182
* AJAX handler
183183
* Stores plugin version
@@ -192,7 +192,7 @@ public function ignore_genesis_notice() {
192192
update_user_meta( $this->curUser->ID, $this->noticeKey, $this->version );
193193
wp_die();
194194
}
195-
195+
196196
/**
197197
* Add options to the widgets
198198
*
@@ -204,35 +204,35 @@ public function ignore_genesis_notice() {
204204
*/
205205
public function widget_form_extend( $instance, $widget ) {
206206

207-
$instance = wp_parse_args( (array) $instance,
208-
array(
209-
'column-classes' => '',
210-
'column-classes-first' => ''
211-
)
207+
$instance = wp_parse_args( (array) $instance,
208+
array(
209+
'column-classes' => '',
210+
'column-classes-first' => ''
211+
)
212212
);
213-
214-
213+
214+
215215
$row = '<p style="border: 1px solid #eee; padding: 5px; background: #f5f5f5;">';
216216
$row .= '<label for="' . $widget->get_field_id( 'column-classes' ) . '">' . __('Width', 'genesis-widget-column-classes') . ':</label> ';
217217
$row .= '<select name="' . $widget->get_field_name( 'column-classes' ) . '" id="' . $widget->get_field_id( 'column-classes' ) . '">';
218-
218+
219219
$row .= '<option value="">- ' . __('none', 'genesis-widget-column-classes') . ' -</option>';
220-
220+
221221
foreach ( $this->column_classes as $class_name ) {
222222
if ( ! empty( $class_name ) ) {
223223
$class_label = $class_name;
224224
$row .= '<option value="' . $class_name . '" ' . selected( $instance['column-classes'], $class_name, false ) . '>' . $class_label . '</option>';
225225
}
226226
}
227-
227+
228228
$row .= '</select>';
229229
$row .= ' <label for="' . $widget->get_field_id( 'column-classes-first' ) . '">' . __('First', 'genesis-widget-column-classes') . ':</label> <input type="checkbox" value="1" name="' . $widget->get_field_name( 'column-classes-first' ) . '" id="' . $widget->get_field_id( 'column-classes-first' ) . '" ' . checked( $instance['column-classes-first'], 1, false ) . '>';
230230
$row .= '</p>';
231-
231+
232232
echo $row;
233233
return $instance;
234234
}
235-
235+
236236
/**
237237
* Add the new fields to the update instance
238238
*
@@ -254,7 +254,7 @@ public function widget_update( $instance, $new_instance ) {
254254

255255
return $instance;
256256
}
257-
257+
258258
/**
259259
* Add classes to the widget
260260
*
@@ -289,20 +289,114 @@ public function sidebar_params( $params ) {
289289
if ( empty( $widget_opt[ $widget_num ] ) ) {
290290
return $params;
291291
}
292-
293-
$widget_extra_classes = '';
292+
293+
/**
294+
* Compat with plugins that filter the display callback
295+
*
296+
* @see https://developer.wordpress.org/reference/hooks/widget_display_callback/
297+
*
298+
* @since 1.2
299+
*
300+
* @param array $instance The current widget instance's settings.
301+
* @param WP_Widget $this The current widget instance.
302+
* @param array $args An array of default widget arguments.
303+
*/
304+
$widget_opt[ $widget_num ] = apply_filters( 'widget_display_callback', $widget_opt[ $widget_num ], $widget_obj['callback'][0], $params[0] );
305+
306+
if ( ! is_array( $widget_opt[ $widget_num ] ) ) {
307+
return $params;
308+
}
309+
310+
$classes_extra = '';
294311
if ( ! empty( $widget_opt[ $widget_num ]['column-classes'] ) ) {
295-
$widget_extra_classes .= $widget_opt[ $widget_num ]['column-classes'].' ';
312+
$classes_extra .= $widget_opt[ $widget_num ]['column-classes'].' ';
296313
}
297314
if ( isset( $widget_opt[ $widget_num ]['column-classes-first'] ) && 1 == $widget_opt[ $widget_num ]['column-classes-first'] ) {
298-
$widget_extra_classes .= 'first ';
315+
$classes_extra .= 'first ';
316+
}
317+
318+
if ( ! empty( $classes_extra ) ) {
319+
320+
if ( ! empty( $params[0]['before_widget'] ) ) {
321+
// Add the classes
322+
$params[0]['before_widget'] = $this->append_to_attribute( $params[0]['before_widget'], 'class', $classes_extra, true );
323+
}
324+
325+
//$params[0]['before_widget'] = str_replace( 'class="', 'class="'.$classes_extra , $params[0]['before_widget'] );
299326
}
300-
301-
$params[0]['before_widget'] = preg_replace( '/class="/', 'class="'.$widget_extra_classes , $params[0]['before_widget'], 1 );
302-
327+
303328
return $params;
304329
}
305-
330+
331+
/**
332+
* Find the class attribute and add the classes in a HTML string
333+
*
334+
* @since 1.2
335+
*
336+
* @param string $str
337+
* @param string $attr The attribute to find
338+
* @param string $content_extra The content that needs to be appended
339+
* @param bool $unique Do we need to filter for unique values?
340+
*
341+
* @return string
342+
*/
343+
public function append_to_attribute( $str, $attr, $content_extra, $unique = false ) {
344+
345+
// Find attr with double quotes
346+
if ( $start = stripos( $str, $attr . '="' ) ) {
347+
$quote = '"';
348+
349+
// Find attr with single quotes
350+
} elseif( $start = stripos( $str, $attr . "='" ) ) {
351+
$quote = "'";
352+
353+
// Not found
354+
} else {
355+
return $str;
356+
}
357+
358+
// Add quote (for filtering purposes)
359+
$attr .= '=' . $quote;
360+
361+
$content_extra = trim( $content_extra );
362+
363+
if ( $unique ) {
364+
365+
// Set start pointer to after "
366+
$start += strlen( $attr );
367+
// Find first " after the start pointer
368+
$end = strpos( $str, $quote, $start );
369+
// Get the current content
370+
$content = explode( ' ', substr( $str, $start, $end - $start ) );
371+
// Get our extra content
372+
$content_extra = explode( ' ', $content_extra );
373+
foreach ( $content_extra as $class ) {
374+
if ( ! empty( $class ) && ! in_array( $class, $content ) ) {
375+
// This one can be added!
376+
$content[] = $class;
377+
}
378+
}
379+
// Remove duplicates
380+
$content = array_unique( $content );
381+
// Convert to space separated string
382+
$content = implode( ' ', $content );
383+
// Get HTML before content
384+
$before_content = substr( $str, 0, $start );
385+
// Get HTML after content
386+
$after_content = substr( $str, $end );
387+
388+
// Combine the string again
389+
$str = $before_content . $content . $after_content;
390+
391+
} else {
392+
393+
$str = str_replace( $attr, $attr . $content_extra . ' ' , $str );
394+
}
395+
396+
// Return full HTML string
397+
return $str;
398+
}
399+
306400
/**
307401
* Load plugin textdomain.
308402
*
@@ -319,7 +413,7 @@ public function load_textdomain() {
319413
*
320414
* @since 1.1.3
321415
* @access public
322-
* @return void
416+
* @return string
323417
*/
324418
public function __toString() {
325419
return get_class( $this );
@@ -333,7 +427,11 @@ public function __toString() {
333427
* @return void
334428
*/
335429
public function __clone() {
336-
_doing_it_wrong( __FUNCTION__, esc_html__( 'Whoah, partner!', 'genesis-widget-column-classes' ), '1.0.0' );
430+
_doing_it_wrong(
431+
__FUNCTION__,
432+
get_class( $this ) . ': ' . esc_html__( 'This class does not want to be cloned', 'view-admin-as' ),
433+
null
434+
);
337435
}
338436

339437
/**
@@ -344,22 +442,32 @@ public function __clone() {
344442
* @return void
345443
*/
346444
public function __wakeup() {
347-
_doing_it_wrong( __FUNCTION__, esc_html__( 'Whoah, partner!', 'genesis-widget-column-classes' ), '1.0.0' );
445+
_doing_it_wrong(
446+
__FUNCTION__,
447+
get_class( $this ) . ': ' . esc_html__( 'This class does not want to wake up', 'view-admin-as' ),
448+
null
449+
);
348450
}
349451

350452
/**
351453
* Magic method to prevent a fatal error when calling a method that doesn't exist.
352454
*
353455
* @since 1.1.3
354456
* @access public
457+
* @param string
458+
* @param array
355459
* @return null
356460
*/
357461
public function __call( $method = '', $args = array() ) {
358-
_doing_it_wrong( get_class( $this ) . "::{$method}", esc_html__( 'Method does not exist.', 'genesis-widget-column-classes' ), '1.0.0' );
462+
_doing_it_wrong(
463+
get_class( $this ) . "::{$method}",
464+
esc_html__( 'Method does not exist.', 'view-admin-as' ),
465+
null
466+
);
359467
unset( $method, $args );
360468
return null;
361469
}
362-
470+
363471
}
364472

365473
/**

0 commit comments

Comments
 (0)