Skip to content

v1.5.2 #30

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Oct 13, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 16 additions & 8 deletions includes/widget-css-classes-library.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public static function add_settings_link( $links, $file ) {
}

if ( $file === $this_plugin ) {
$settings_link = '<a href="' . admin_url( 'options-general.php?page=widget-css-classes-settings' ) . '">' . esc_attr__( 'Settings', WCSSC_Lib::DOMAIN ) . '</a>';
$settings_link = '<a href="' . admin_url( 'options-general.php?page=widget-css-classes-settings' ) . '">' . esc_attr__( 'Settings', self::DOMAIN ) . '</a>';
array_unshift( $links, $settings_link );
}

Expand All @@ -72,10 +72,10 @@ public static function add_settings_link( $links, $file ) {
public static function admin_footer() {
$plugin_data = get_plugin_data( WCSSC_FILE );
echo $plugin_data['Title'] // @codingStandardsIgnoreLine >> no valid esc function.
. ' | ' . esc_attr__( 'Version', WCSSC_Lib::DOMAIN ) . ' ' . esc_html( $plugin_data['Version'] )
. ' | ' . $plugin_data['Author'] // @codingStandardsIgnoreLine >> no valid esc function.
. ' | ' . esc_attr__( 'Version', WCSSC_Lib::DOMAIN ) . ' ' . esc_html( $plugin_data['Version'] )
. ' | ' . $plugin_data['Author'] // @codingStandardsIgnoreLine >> no valid esc function.
. ' | <a href="http://codebrainmedia.com">CodeBrain Media</a>'
. ' | <a href="https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=cindy@cleverness.org">' . esc_attr__( 'Donate', WCSSC_Lib::DOMAIN ) . '</a>
. ' | <a href="https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=cindy@cleverness.org">' . esc_attr__( 'Donate', self::DOMAIN ) . '</a>
<br />';
}

Expand Down Expand Up @@ -122,9 +122,9 @@ private static function update( $version ) {
}

if ( version_compare( $version, '1.2', '<' ) ) {
$settings['show_number'] = 1;
$settings['show_location'] = 1;
$settings['show_evenodd'] = 1;
$settings['show_number'] = true;
$settings['show_location'] = true;
$settings['show_evenodd'] = true;
}

if ( version_compare( $version, '1.3', '<' ) ) {
Expand All @@ -134,12 +134,19 @@ private static function update( $version ) {
}
// dropdown settings are renamed to defined_classes
if ( ! isset( $settings['dropdown'] ) ) {
$settings['dropdown'] = '';
$settings['dropdown'] = array();
}
$settings['defined_classes'] = $settings['dropdown'];
unset( $settings['dropdown'] );
}

if ( version_compare( $version, '1.5.2', '<' ) ) {
// set on true because this was default in previous versions.
if ( ! isset( $settings['translate_classes'] ) ) {
$settings['translate_classes'] = true;
}
}

self::update_settings( $settings );
}

Expand Down Expand Up @@ -326,6 +333,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
26 changes: 20 additions & 6 deletions includes/widget-css-classes-settings.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,20 +44,34 @@ public function register_general_settings() {

register_setting( $this->general_key, $this->general_key, array( $this, 'validate_input' ) );
add_settings_section( 'section_general', esc_attr__( 'Widget CSS Classes Settings', WCSSC_Lib::DOMAIN ), array( $this, 'section_general_desc' ), $this->general_key );
add_settings_field( 'show_number', esc_attr__( 'Add Widget Number Classes', WCSSC_Lib::DOMAIN ), array( $this, 'show_yes_no_option' ), $this->general_key, 'section_general', array( 'key' => 'show_number' ) );
add_settings_field( 'show_location', esc_attr__( 'Add First/Last Classes', WCSSC_Lib::DOMAIN ), array( $this, 'show_yes_no_option' ), $this->general_key, 'section_general', array( 'key' => 'show_location' ) );
add_settings_field( 'show_evenodd', esc_attr__( 'Add Even/Odd Classes', WCSSC_Lib::DOMAIN ), array( $this, 'show_yes_no_option' ), $this->general_key, 'section_general', array( 'key' => 'show_evenodd' ) );
add_settings_field( 'show_id', esc_attr__( 'Show Additional Field for ID', WCSSC_Lib::DOMAIN ), array( $this, 'show_yes_no_option' ), $this->general_key, 'section_general', array( 'key' => 'show_id' ) );
add_settings_field( 'show_number', esc_attr__( 'Add Widget Number Classes', WCSSC_Lib::DOMAIN ), array( $this, 'show_yes_no_option' ), $this->general_key, 'section_general', array(
'key' => 'show_number',
) );
add_settings_field( 'show_location', esc_attr__( 'Add First/Last Classes', WCSSC_Lib::DOMAIN ), array( $this, 'show_yes_no_option' ), $this->general_key, 'section_general', array(
'key' => 'show_location',
) );
add_settings_field( 'show_evenodd', esc_attr__( 'Add Even/Odd Classes', WCSSC_Lib::DOMAIN ), array( $this, 'show_yes_no_option' ), $this->general_key, 'section_general', array(
'key' => 'show_evenodd',
) );
add_settings_field( 'show_id', esc_attr__( 'Show Additional Field for ID', WCSSC_Lib::DOMAIN ), array( $this, 'show_yes_no_option' ), $this->general_key, 'section_general', array(
'key' => 'show_id',
) );
add_settings_field( 'type', esc_attr__( 'Class Field Type', WCSSC_Lib::DOMAIN ), array( $this, 'type_option' ), $this->general_key, 'section_general' );
add_settings_field( 'defined_classes', esc_attr__( 'Predefined Classes', WCSSC_Lib::DOMAIN ), array( $this, 'defined_classes_option' ), $this->general_key, 'section_general' );
add_settings_field( 'fix_widget_params', esc_attr__( 'Fix widget parameters', WCSSC_Lib::DOMAIN ), array( $this, 'show_yes_no_option' ), $this->general_key, 'section_general', array(
'key' => 'fix_widget_params',
'key' => 'fix_widget_params',
'desc' => esc_html__( 'Wrap widget in a <div> element if the parameters are invalid.', WCSSC_Lib::DOMAIN ),
) );
add_settings_field( 'filter_unique', esc_attr__( 'Remove duplicate classes', WCSSC_Lib::DOMAIN ), array( $this, 'show_yes_no_option' ), $this->general_key, 'section_general', array(
'key' => 'filter_unique',
'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
87 changes: 69 additions & 18 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 All @@ -34,6 +48,10 @@ class WCSSC {
'defined' => 'edit_theme_options',
);

/**
* Initializer for plugin backend.
* @since 1.5.0
*/
public static function init() {
static $done;
if ( $done ) return;
Expand Down Expand Up @@ -70,6 +88,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 @@ -113,23 +155,23 @@ public static function extend_widget_form( $widget, $return, $instance ) {
} else {
$fields .= self::do_hidden( $widget->get_field_name( 'classes' ), $instance['classes'] );
}
break;
break;
case 2:
// show classes predefined only.
if ( $access_predefined ) {
$fields .= self::do_predefined_field( $widget, $instance, null );
} else {
$fields .= self::do_hidden( $widget->get_field_name( 'classes' ), $instance['classes'] );
}
break;
break;
case 3:
// show both.
if ( $access_predefined ) {
$fields .= self::do_predefined_field( $widget, $instance, $access_class );
} else {
$fields .= self::do_hidden( $widget->get_field_name( 'classes' ), $instance['classes'] );
}
break;
break;
}

if ( $fields ) {
Expand Down Expand Up @@ -230,29 +272,38 @@ private static function do_predefined_field( $widget, $instance, $do_class_field

// Do we have existing classes and is the user allowed to select defined classes?
if ( ! empty( $instance['classes'] ) ) {

$text_classes = explode( ' ', $instance['classes'] );
foreach ( $text_classes as $key => $value ) {
if ( in_array( $value, $predefined_classes, true ) ) {
if ( ! in_array( $value, $instance['classes-defined'], true ) ) {
$instance['classes-defined'][] = $value;
}
unset( $text_classes[ $key ] );
}
}

// Get the classes that exist in the predefined classes and merge them with the existing.
$exists_defined = array_intersect( $text_classes, $predefined_classes );

// Add them to the defined classes of this instance.
$instance['classes-defined'] = array_merge( $instance['classes-defined'], $exists_defined );

// Remove classes that exist in the predefined classes from the normal (custom) classes.
$text_classes = array_diff( $text_classes, $predefined_classes );

$instance['classes'] = implode( ' ', $text_classes );
}

$style = array(
'padding' => 'padding: 5px;',
'max-height' => 'max-height: 70px;',
'max-height' => 'max-height: 75px;',
'overflow' => 'overflow: hidden;',
'overflow-y' => 'overflow-y: auto;',
'border' => 'border: 1px solid #ddd;',
'box-shadow' => 'box-shadow: 0 1px 2px rgba(0, 0, 0, 0.07) inset;',
'color' => 'color: #32373c;',
'margin-top' => 'margin-top: 1px;',
'margin-top' => 'margin-top: 1px;',
);

if ( 3 < count( $predefined_classes ) ) {
unset( $style['max-height'] );
$style['height'] = 'height: 75px;';
$style['resize'] = 'resize: vertical;';
}

if ( null !== $do_class_field ) {
if ( $do_class_field ) {
$field .= self::do_class_field( $widget, $instance );
Expand Down Expand Up @@ -480,16 +531,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 +550,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
6 changes: 5 additions & 1 deletion readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ License: GPLv2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html
Requires at least: 3.3
Tested up to: 4.8
Stable tag: 1.5.1
Requires PHP: 5.2.4
Stable tag: 1.5.2

Add custom classes and ids plus first, last, even, odd, and numbered classes to your widgets.

Expand Down Expand Up @@ -94,6 +95,9 @@ Visit [the plugin website](http://cleverness.org/plugins/widget-css-classes/) an

== Changelog ==

= 1.5.2 =
* **Enhancement:** Make translations of core widget classes optional instead of default. [#29](https://github.com/cleverness/widget-css-classes/issues/29)

= 1.5.1 =
* **Fix:** Widget Logic `widget_content` filter compatibility. [#27](https://github.com/cleverness/widget-css-classes/issues/27)
* **Enhancement:** Make uninstall script compatible with network installations.
Expand Down
1 change: 1 addition & 0 deletions tests/test-plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ function test_settings() {
'show_evenodd' => true,
'fix_widget_params' => false,
'filter_unique' => false,
'translate_classes' => false,
);

// Trigger update.
Expand Down
7 changes: 4 additions & 3 deletions widget-css-classes.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
/**
* Plugin Name: Widget CSS Classes
* Version: 1.5.1
* Version: 1.5.2-dev
* Description: Add custom, first, last, even, odd, and numbered classes to your widgets.
* Author: C.M. Kendrick
* Author URI: http://cleverness.org
Expand All @@ -27,7 +27,7 @@ function widget_css_classes_loader() {

if ( is_admin() ) {

if ( ! defined( 'WCSSC_PLUGIN_VERSION' ) ) define( 'WCSSC_PLUGIN_VERSION', '1.5.1' );
if ( ! defined( 'WCSSC_PLUGIN_VERSION' ) ) define( 'WCSSC_PLUGIN_VERSION', '1.5.2' );
if ( ! defined( 'WCSSC_FILE' ) ) define( 'WCSSC_FILE', __FILE__ );
if ( ! defined( 'WCSSC_BASENAME' ) ) define( 'WCSSC_BASENAME', plugin_basename( __FILE__ ) );
if ( ! defined( 'WCSSC_PLUGIN_DIR' ) ) define( 'WCSSC_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
Expand All @@ -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 All @@ -66,7 +67,7 @@ function widget_css_classes_activation() {
}

if ( ! defined( 'WCSSC_BASENAME' ) ) define( 'WCSSC_BASENAME', plugin_basename( __FILE__ ) );
if ( ! defined( 'WCSSC_DB_VERSION' ) ) define( 'WCSSC_DB_VERSION', '1.5' );
if ( ! defined( 'WCSSC_DB_VERSION' ) ) define( 'WCSSC_DB_VERSION', '1.5.2' );
if ( ! defined( 'WCSSC_FILE' ) ) define( 'WCSSC_FILE', __FILE__ );
include_once 'includes/widget-css-classes-library.class.php';

Expand Down