Skip to content

Commit

Permalink
New setting: translate_classes
Browse files Browse the repository at this point in the history
Related #29
  • Loading branch information
JoryHogeveen committed Aug 22, 2017
1 parent 85e4c2e commit 07d5653
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 5 deletions.
1 change: 1 addition & 0 deletions includes/widget-css-classes-library.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,7 @@ public static function get_default_settings() {
'show_evenodd' => true,
'fix_widget_params' => false,
'filter_unique' => false,
'translate_classes' => false,
);

// Prevent passing by reference.
Expand Down
6 changes: 6 additions & 0 deletions includes/widget-css-classes-settings.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ public function register_general_settings() {
'key' => 'filter_unique',
'desc' => esc_html__( 'Plugins that run after this plugin could still add duplicates.', WCSSC_Lib::DOMAIN ),
) );
add_settings_field( 'translate_classes', esc_attr__( 'Translate classes', WCSSC_Lib::DOMAIN ), array( $this, 'show_yes_no_option' ), $this->general_key, 'section_general', array(
'key' => 'translate_classes',
'desc' => esc_html__( 'Translate classes like `widget-first` and `widget-even`.', WCSSC_Lib::DOMAIN )
// Translators: %s stands for a link to translate.wordpress.org.
. ' ' . sprintf( esc_html__( 'Translations are taken from %s', WCSSC_Lib::DOMAIN ), '<a href="https://translate.wordpress.org/projects/wp-plugins/widget-css-classes" target="_blank">translate.wordpress.org</a>' ),
) );
do_action( 'widget_css_classes_settings' );
}

Expand Down
48 changes: 43 additions & 5 deletions includes/widget-css-classes.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,20 @@ class WCSSC {
*/
public static $widget_counter = array();

/**
* Container for core class tipes.
* @static
* @since 1.5.2
* @var array
*/
public static $core_classes = array(
'widget_prefix' => 'widget-',
'widget_first' => 'widget-first',
'widget_last' => 'widget-last',
'widget_even' => 'widget-even',
'widget_odd' => 'widget-odd',
);

/**
* Default capabilities to display the WCC form in widgets.
* @static
Expand Down Expand Up @@ -70,6 +84,30 @@ public static function init() {
$done = true;
}

/**
* Initializer for plugin frontend.
* @since 1.5.2
*/
public static function init_front() {
static $done;
if ( $done ) return;

/**
* Do not translate by default but make it optionally.
* @since 1.5.2
*/
if ( WCSSC_Lib::get_settings( 'translate_classes' ) ) {
// Translate with readable string instead of variable for compatibility.
self::$core_classes['widget_prefix'] = __( 'widget-', WCSSC_Lib::DOMAIN );
self::$core_classes['widget_first'] = __( 'widget-first', WCSSC_Lib::DOMAIN );
self::$core_classes['widget_last'] = __( 'widget-last', WCSSC_Lib::DOMAIN );
self::$core_classes['widget_even'] = __( 'widget-even', WCSSC_Lib::DOMAIN );
self::$core_classes['widget_odd'] = __( 'widget-odd', WCSSC_Lib::DOMAIN );
}

$done = true;
}

/**
* Adds form fields to Widget
* @static
Expand Down Expand Up @@ -480,16 +518,16 @@ public static function add_widget_classes( $params ) {
}

if ( WCSSC_Lib::get_settings( 'show_number' ) ) {
$class = apply_filters( 'widget_css_classes_number', esc_attr__( 'widget-', WCSSC_Lib::DOMAIN ) ) . self::$widget_counter[ $this_id ];
$class = apply_filters( 'widget_css_classes_number', self::$core_classes['widget_prefix'] ) . self::$widget_counter[ $this_id ];
array_unshift( $classes, $class );
}

if ( WCSSC_Lib::get_settings( 'show_location' ) &&
isset( $arr_registered_widgets[ $this_id ] ) &&
is_array( $arr_registered_widgets[ $this_id ] )
) {
$widget_first = apply_filters( 'widget_css_classes_first', esc_attr__( 'widget-first', WCSSC_Lib::DOMAIN ) );
$widget_last = apply_filters( 'widget_css_classes_last', esc_attr__( 'widget-last', WCSSC_Lib::DOMAIN ) );
$widget_first = apply_filters( 'widget_css_classes_first', self::$core_classes['widget_first'] );
$widget_last = apply_filters( 'widget_css_classes_last', self::$core_classes['widget_last'] );
if ( 1 === (int) self::$widget_counter[ $this_id ] ) {
array_unshift( $classes, $widget_first );
}
Expand All @@ -499,8 +537,8 @@ public static function add_widget_classes( $params ) {
}

if ( WCSSC_Lib::get_settings( 'show_evenodd' ) ) {
$widget_even = apply_filters( 'widget_css_classes_even', esc_attr__( 'widget-even', WCSSC_Lib::DOMAIN ) );
$widget_odd = apply_filters( 'widget_css_classes_odd', esc_attr__( 'widget-odd', WCSSC_Lib::DOMAIN ) );
$widget_even = apply_filters( 'widget_css_classes_even', self::$core_classes['widget_even'] );
$widget_odd = apply_filters( 'widget_css_classes_odd', self::$core_classes['widget_odd'] );
$class = ( ( self::$widget_counter[ $this_id ] % 2 ) ? $widget_odd : $widget_even );
array_unshift( $classes, $class );
}
Expand Down
1 change: 1 addition & 0 deletions widget-css-classes.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ function widget_css_classes_loader() {
function widget_css_classes_frontend_hook() {
if ( ! is_admin() ) {
include_once 'includes/widget-css-classes.class.php';
WCSSC::init_front();
add_filter( 'dynamic_sidebar_params', array( 'WCSSC', 'add_widget_classes' ) );
}
}
Expand Down

0 comments on commit 07d5653

Please sign in to comment.