diff --git a/readme.txt b/readme.txt index 9c526dc..810ca4c 100644 --- a/readme.txt +++ b/readme.txt @@ -1,11 +1,11 @@ === Widget Subtitles === Contributors: keraweb -Donate link: https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=YGPLMLU7XQ9E8&lc=NL&item_name=Widget%20Subtitles&item_number=JWPP%2dWS¤cy_code=EUR&bn=PP%2dDonationsBF%3abtn_donateCC_LG%2egif%3aNonHostedGuest +Donate link: https://www.keraweb.nl/donate.php?for=widget-subtitles Tags: widget, widget subtitle, subtitle, subtitles, sub title, sidebar Requires at least: 3.0 Tested up to: 4.9 Requires PHP: 5.2.4 -Stable tag: 1.1.4.1 +Stable tag: 1.2 License: GPLv2 or later License URI: http://www.gnu.org/licenses/gpl-2.0.html @@ -15,59 +15,14 @@ Add a customizable subtitle to your widgets This plugin adds a subtitle input field to all your widgets. You can also change the location of the subtitle and even use filters to change the subtitle output. -= Filter: `widget_subtitles_element` = -Allows you to change the HTML element for the subtitle. -Since 1.0 - -* **Default:** `span` -* **Parameters:** - * `string` Default element. - * `string` Widget ID, widget name + instance number. - * `string` Sidebar ID where this widget is located. (since 1.1) - * `array` All widget data. (since 1.1) - * `WP_Widget` The widget type class. (since 1.1.3) -* **Return:** `string` A valid HTML element. - -= Filter: `widget_subtitles_classes` = -Allow filter for subtitle classes to overwrite, remove or add classes. -Since 1.0 - -* **Default:** `array( 'widget-subtitle', 'widgetsubtitle', 'subtitle-{LOCATION}' );` *Where {LOCATION} stands for your selected location*. -* **Parameters:** - * `string` Default element. - * `string` Widget ID, widget name + instance number. - * `string` Sidebar ID where this widget is located. (since 1.1) - * `array` All widget data. (since 1.1) - * `WP_Widget` The widget type class. (since 1.1.3) -* **Return:** `array` An array of CSS classes. - -= Filter: `widget_subtitles_default_location` = -Sets the default location for subtitles. -Since 1.1.2 - -* **Default:** `after-inside` -* **Parameters:** `string` The default subtitle location. -* **Return:** `string` Options: `after-inside`, `after-outside`, `before-inside`, `before-outside`. - -= Filter: `widget_subtitles_edit_location_capability` = -Change the capability required to modify subtitle locations. -Since 1.1.2 - -* **Default:** `edit_theme_options` -* **Parameters:** `string` The default capability. -* **Return:** `string` The new capability. - -= Filter: `widget_subtitles_available_locations` = -Overwrites the available locations for a widget. -NOTE: You can currently only remove locations. New locations are not possible (yet). -Since 1.1.3 - -* **Default:** `after-inside`, `after-outside`, `before-inside`, `before-outside`. -* **Parameters:** - * `array` The array of available locations. - * `WP_Widget` The widget type class. - * `array` The widget instance. -* **Return:** `array` Filtered list of available locations for this widget. += Filters = +* [`widget_subtitles_element`](https://github.com/JoryHogeveen/widget-subtitles/wiki#filter-widget_subtitles_element) +* [`widget_subtitles_classes`](https://github.com/JoryHogeveen/widget-subtitles/wiki#filter-widget_subtitles_classes) +* [`widget_subtitles_default_location`](https://github.com/JoryHogeveen/widget-subtitles/wiki#filter-widget_subtitles_default_location) +* [`widget_subtitles_edit_location_capability`](https://github.com/JoryHogeveen/widget-subtitles/wiki#filter-widget_subtitles_edit_location_capability) +* [`widget_subtitles_available_locations`](https://github.com/JoryHogeveen/widget-subtitles/wiki#filter-widget_subtitles_available_locations) +* [`widget_subtitles_add_subtitle`](https://github.com/JoryHogeveen/widget-subtitles/wiki#filter-widget_subtitles_add_subtitle) +* [`widget_subtitle`](https://github.com/JoryHogeveen/widget-subtitles/wiki#filter-widget_subtitle) You can use these filters inside your theme functions.php file or in a plugin. @@ -87,6 +42,16 @@ Or search for "Widget Subtitles" via your plugins menu. == Changelog == += 1.2 = + +* **Feature:** New filter: `widget_subtitle` to change the subtitle for a widget. Similar to WP's `widget_title`. +* **Feature:** New filter: `widget_subtitles_add_subtitle` to allow custom subtitle location handlers. +* **Enhancement:** Extended filter: `widget_subtitles_available_locations` now allows custom locations. +* **Enhancement:** Make use of `wp_get_sidebars_widgets()` instead of a global to get the related sidebar ID from a widget instance. +* **Documentation:** Created a wiki on GitHub. + +Detailed info: [PR on GitHub](https://github.com/JoryHogeveen/widget-subtitles/pull/11) + = 1.1.4.1 = * **Fix:** PHP notice. diff --git a/tests/test-plugin.php b/tests/test-plugin.php index 53e747e..f1d81d9 100644 --- a/tests/test-plugin.php +++ b/tests/test-plugin.php @@ -16,13 +16,11 @@ function test_plugin_activated() { // Check for PHP errors function test_general() { - $ws = ws_widget_subtitles(); - $ws->get_links(); + ws_widget_subtitles()->get_links(); } // Check add_subtitle() method. function test_add_subtitle() { - $ws = ws_widget_subtitles(); $tests = array( array( @@ -118,28 +116,73 @@ function test_add_subtitle() { ), 'location' => 'after-outside', // default ), + array( + 'start' => array( + 'after_title' => '', + 'before_title' => '
', + ), + 'data' => '
SUBTITLE
', + 'result' => array( + 'after_title' => '
', + 'before_title' => '
SUBTITLE
test ', + ), + 'location' => 'custom', // default + ), ); + add_filter( 'widget_subtitles_add_subtitle', array( $this, 'filter_widget_subtitles_add_subtitle' ), 10, 3 ); + foreach ( $tests as $test ) { - $this->assertEquals( $test['result'], $ws->add_subtitle( $test['start'], $test['data'], $test['location'] ) ); + $this->assertEquals( + $test['result'], + ws_widget_subtitles()->add_subtitle( $test['start'], $test['data'], $test['location'] ) + ); } } - // Check widget_update_callback() method - function test_widget_update_callback() { - $ws = ws_widget_subtitles(); + function filter_widget_subtitles_add_subtitle( $params, $subtitle, $subtitle_location ) { + if ( 'custom' === $subtitle_location ) { + $params['before_title'] = $params['before_title'] . $subtitle . ' test '; + } + return $params; + } + + // Check get_subtitle_classes() method. + function test_get_subtitle_classes() { $tests = array( array( - 'start' => array(), - 'data' => array( - 'subtitle' => 'test', - 'subtitle_location' => '1', // Not valid. + 'result' => array( + 'widgetsubtitle', + 'widget-subtitle', + 'subtitle-after-outside', + 'subtitle-after', + 'subtitle-outside', ), + 'location' => 'after-outside', // default + ), + array( 'result' => array( - 'subtitle' => 'test', + 'widgetsubtitle', + 'widget-subtitle', + 'subtitle-custom', ), + 'location' => 'custom', // default ), + ); + + foreach ( $tests as $test ) { + $this->assertEquals( + $test['result'], + ws_widget_subtitles()->get_subtitle_classes( $test['location'] ) + ); + } + } + + // Check filter_widget_update_callback() method + function test_widget_update_callback() { + + $tests = array( array( 'start' => array(), 'data' => array( @@ -201,7 +244,10 @@ function test_widget_update_callback() { // Run tests foreach ( $tests as $test ) { - $this->assertEquals( $test['result'], $ws->widget_update_callback( $test['start'], $test['data'] ) ); + $this->assertEquals( + $test['result'], + ws_widget_subtitles()->filter_widget_update_callback( $test['start'], $test['data'] ) + ); } } } diff --git a/widget-subtitles.php b/widget-subtitles.php index f56e559..a0390c6 100644 --- a/widget-subtitles.php +++ b/widget-subtitles.php @@ -3,7 +3,7 @@ * @author Jory Hogeveen * @package Widget_Subtitles * @since 0.1.0 - * @version 1.1.4.1 + * @version 1.2.0 * @licence GPL-2.0+ * @link https://github.com/JoryHogeveen/widget-subtitles * @@ -11,7 +11,7 @@ * Plugin Name: Widget Subtitles * Plugin URI: https://wordpress.org/plugins/widget-subtitles/ * Description: Add a customizable subtitle to your widgets - * Version: 1.1.4.1 + * Version: 1.2 * Author: Jory Hogeveen * Author URI: http://www.keraweb.nl * Text Domain: widget-subtitles @@ -50,7 +50,7 @@ * @author Jory Hogeveen * @package Widget_Subtitles * @since 0.1.0 - * @version 1.1.4.1 + * @version 1.2.0 */ final class WS_Widget_Subtitles { @@ -166,16 +166,12 @@ public function init() { * @param string $subtitle_location The subtitle location (default: 'after-inside'). * @return string Options: 'after-inside', 'after-outside', 'before-inside', 'before-outside'. */ - $default = apply_filters( 'widget_subtitles_default_location', $this->default_location ); - $default = explode( '-', $default ); + $this->default_location = apply_filters( 'widget_subtitles_default_location', $this->default_location ); + $default = explode( '-', $this->default_location ); foreach ( $default as $key => $value ) { if ( isset( $loc[ $value ] ) && 2 === count( $default ) ) { $default[ $key ] = $loc[ $value ]; - continue; - } else { - $default = array( $loc['after'], $loc['inside'] ); - break; } } @@ -194,9 +190,9 @@ public function init() { ); add_action( 'init', array( $this, 'load_plugin_textdomain' ) ); - add_action( 'in_widget_form', array( $this, 'in_widget_form' ), 9, 3 ); - add_filter( 'widget_update_callback', array( $this, 'widget_update_callback' ), 10, 4 ); - add_filter( 'dynamic_sidebar_params', array( $this, 'dynamic_sidebar_params' ) ); + add_action( 'in_widget_form', array( $this, 'action_in_widget_form' ), 9, 3 ); + add_filter( 'widget_update_callback', array( $this, 'filter_widget_update_callback' ), 10, 4 ); + add_filter( 'dynamic_sidebar_params', array( $this, 'filter_dynamic_sidebar_params' ) ); // Add links to plugins page. add_action( 'plugin_row_meta', array( $this, 'action_plugin_row_meta' ), 10, 2 ); @@ -215,6 +211,7 @@ public function load_plugin_textdomain() { * Add a subtitle input field into the form. * * @since 0.1.0 + * @since 1.2.0 Add `action_` prefix. * @access public * * @param \WP_Widget $widget @@ -222,7 +219,7 @@ public function load_plugin_textdomain() { * @param array $instance * @return null */ - public function in_widget_form( $widget, $return, $instance ) { + public function action_in_widget_form( $widget, $return, $instance ) { $instance = wp_parse_args( (array) $instance, @@ -240,19 +237,12 @@ public function in_widget_form( $widget, $return, $instance ) {

- get_available_subtitle_locations( $widget, $instance ); - } - - if ( 1 < count( $locations ) ) { - ?> +