| 1 |
<?php |
| 2 |
/* |
| 3 |
Plugin Name: Widget Context |
| 4 |
Plugin URI: http://wordpress.org/extend/plugins/widget-context/ |
| 5 |
Description: Show or hide widgets depending on the section of the site that is being viewed. |
| 6 |
Version: 1.0.1 |
| 7 |
Author: Kaspars Dambis |
| 8 |
Author URI: http://kaspars.net |
| 9 |
Text Domain: widget-context |
| 10 |
*/ |
| 11 |
|
| 12 |
// Go! |
| 13 |
widget_context::instance(); |
| 14 |
|
| 15 |
class widget_context { |
| 16 |
|
| 17 |
private static $instance; |
| 18 |
private $sidebars_widgets; |
| 19 |
private $options_name = 'widget_logic_options'; // Context settings for widgets (visibility, etc) |
| 20 |
private $settings_name = 'widget_context_settings'; // Widget Context global settings |
| 21 |
|
| 22 |
private $core_modules = array( |
| 23 |
'word-count/word-count.php', |
| 24 |
'custom-post-types-taxonomies/custom-cpt-tax.php' |
| 25 |
); |
| 26 |
|
| 27 |
var $context_options = array(); // Store visibility settings |
| 28 |
var $context_settings = array(); // Store admin settings |
| 29 |
var $contexts = array(); |
| 30 |
var $plugin_path; |
| 31 |
|
| 32 |
|
| 33 |
static function instance() { |
| 34 |
|
| 35 |
if ( ! self::$instance ) |
| 36 |
self::$instance = new self(); |
| 37 |
|
| 38 |
return self::$instance; |
| 39 |
|
| 40 |
} |
| 41 |
|
| 42 |
|
| 43 |
private function widget_context() { |
| 44 |
|
| 45 |
// Define available widget contexts |
| 46 |
add_action( 'init', array( $this, 'define_widget_contexts' ), 5 ); |
| 47 |
|
| 48 |
// Load plugin settings and show/hide widgets by altering the |
| 49 |
// $sidebars_widgets global variable |
| 50 |
add_action( 'init', array( $this, 'init_widget_context' ) ); |
| 51 |
|
| 52 |
// Enable localization |
| 53 |
add_action( 'plugins_loaded', array( $this, 'init_l10n' ) ); |
| 54 |
|
| 55 |
// Append Widget Context settings to widget controls |
| 56 |
add_action( 'in_widget_form', array( $this, 'widget_context_controls' ), 10, 3 ); |
| 57 |
|
| 58 |
// Add admin menu for config |
| 59 |
add_action( 'admin_enqueue_scripts', array( $this, 'admin_scripts' ) ); |
| 60 |
|
| 61 |
// Style things based on admin color scheme |
| 62 |
add_action( 'admin_footer', array( $this, 'admin_styles_inline' ) ); |
| 63 |
|
| 64 |
// Save widget context settings, when in admin area |
| 65 |
add_action( 'sidebar_admin_setup', array( $this, 'save_widget_context_settings' ) ); |
| 66 |
|
| 67 |
// Fix legacy context option naming |
| 68 |
add_filter( 'widget_context_options', array( $this, 'fix_legacy_options' ) ); |
| 69 |
|
| 70 |
// Register admin settings menu |
| 71 |
add_action( 'admin_menu', array( $this, 'widget_context_settings_menu' ) ); |
| 72 |
|
| 73 |
// Register admin settings |
| 74 |
add_action( 'admin_init', array( $this, 'widget_context_settings_init' ) ); |
| 75 |
|
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
function define_widget_contexts() { |
| 80 |
|
| 81 |
// Initialize core modules |
| 82 |
$include_path = plugin_dir_path( __FILE__ ) . '/modules'; |
| 83 |
|
| 84 |
foreach ( $this->core_modules as $module ) { |
| 85 |
include sprintf( '%s/%s', $include_path, $module ); |
| 86 |
} |
| 87 |
|
| 88 |
// Default context |
| 89 |
$default_contexts = array( |
| 90 |
'incexc' => array( |
| 91 |
'label' => __( 'Widget Context', 'widget-context' ), |
| 92 |
'description' => __( 'Set the default logic to show or hide.', 'widget-context' ), |
| 93 |
'weight' => -100, |
| 94 |
'type' => 'core', |
| 95 |
), |
| 96 |
'location' => array( |
| 97 |
'label' => __( 'Global Sections', 'widget-context' ), |
| 98 |
'description' => __( 'Based on standard WordPress template tags.', 'widget-context' ), |
| 99 |
'weight' => 10 |
| 100 |
), |
| 101 |
'url' => array( |
| 102 |
'label' => __( 'Target by URL', 'widget-context' ), |
| 103 |
'description' => __( 'Based on URL patterns.', 'widget-context' ), |
| 104 |
'weight' => 20 |
| 105 |
), |
| 106 |
'admin_notes' => array( |
| 107 |
'label' => __( 'Notes (invisible to public)', 'widget-context' ), |
| 108 |
'description' => __( 'Enables private notes on widget context settings.'), |
| 109 |
'weight' => 90 |
| 110 |
) |
| 111 |
); |
| 112 |
|
| 113 |
// Add default context controls and checks |
| 114 |
foreach ( $default_contexts as $context_name => $context_desc ) { |
| 115 |
|
| 116 |
add_filter( 'widget_context_control-' . $context_name, array( $this, 'control_' . $context_name ), 10, 2 ); |
| 117 |
add_filter( 'widget_context_check-' . $context_name, array( $this, 'context_check_' . $context_name ), 10, 2 ); |
| 118 |
|
| 119 |
} |
| 120 |
|
| 121 |
// Enable other plugins and themes to specify their own contexts |
| 122 |
$this->contexts = apply_filters( 'widget_contexts', $default_contexts ); |
| 123 |
|
| 124 |
// Sort contexts by their weight |
| 125 |
uasort( $this->contexts, array( $this, 'sort_context_by_weight' ) ); |
| 126 |
|
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
function sort_context_by_weight( $a, $b ) { |
| 131 |
|
| 132 |
if ( ! isset( $a['weight'] ) ) |
| 133 |
$a['weight'] = 10; |
| 134 |
|
| 135 |
if ( ! isset( $b['weight'] ) ) |
| 136 |
$b['weight'] = 10; |
| 137 |
|
| 138 |
return ( $a['weight'] < $b['weight'] ) ? -1 : 1; |
| 139 |
|
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
function init_widget_context() { |
| 144 |
|
| 145 |
$this->context_options = apply_filters( |
| 146 |
'widget_context_options', |
| 147 |
(array) get_option( $this->options_name, array() ) |
| 148 |
); |
| 149 |
|
| 150 |
$this->context_settings = wp_parse_args( |
| 151 |
(array) get_option( $this->settings_name, array() ), |
| 152 |
array( |
| 153 |
'contexts' => array() |
| 154 |
) |
| 155 |
); |
| 156 |
|
| 157 |
// Hide/show widgets for is_active_sidebar() to work |
| 158 |
add_filter( 'sidebars_widgets', array( $this, 'maybe_unset_widgets_by_context' ), 10 ); |
| 159 |
|
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
function init_l10n() { |
| 164 |
|
| 165 |
load_plugin_textdomain( 'widget-context', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' ); |
| 166 |
|
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
function admin_scripts() { |
| 171 |
|
| 172 |
wp_enqueue_style( |
| 173 |
'widget-context-css', |
| 174 |
plugins_url( 'css/admin.css', plugin_basename( __FILE__ ) ) |
| 175 |
); |
| 176 |
|
| 177 |
wp_enqueue_script( |
| 178 |
'widget-context-js', |
| 179 |
plugins_url( 'js/widget-context.js', plugin_basename( __FILE__ ) ), |
| 180 |
array( 'jquery' ) |
| 181 |
); |
| 182 |
|
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
function admin_styles_inline() { |
| 187 |
|
| 188 |
global $_wp_admin_css_colors; |
| 189 |
|
| 190 |
$color_scheme = get_user_option( 'admin_color' ); |
| 191 |
|
| 192 |
if ( isset( $_wp_admin_css_colors[ $color_scheme ] ) ) { |
| 193 |
|
| 194 |
printf( |
| 195 |
'<style type="text/css"> |
| 196 |
.widget-context .widget-context-header h3:before { color:%1$s; } |
| 197 |
</style>', |
| 198 |
$_wp_admin_css_colors[ $color_scheme ]->colors[3] |
| 199 |
); |
| 200 |
|
| 201 |
} |
| 202 |
|
| 203 |
} |
| 204 |
|
| 205 |
|
| 206 |
function widget_context_controls( $object, $return, $instance ) { |
| 207 |
|
| 208 |
echo $this->display_widget_context( $object->id ); |
| 209 |
|
| 210 |
} |
| 211 |
|
| 212 |
|
| 213 |
function save_widget_context_settings() { |
| 214 |
|
| 215 |
if ( ! current_user_can( 'edit_theme_options' ) || empty( $_POST ) || ! isset( $_POST['wl'] ) ) |
| 216 |
return; |
| 217 |
|
| 218 |
// Delete a widget |
| 219 |
if ( isset( $_POST['delete_widget'] ) && isset( $_POST['the-widget-id'] ) ) |
| 220 |
unset( $this->context_options[ $_POST['the-widget-id'] ] ); |
| 221 |
|
| 222 |
// Add / Update |
| 223 |
$this->context_options = array_merge( $this->context_options, $_POST['wl'] ); |
| 224 |
|
| 225 |
$sidebars_widgets = wp_get_sidebars_widgets(); |
| 226 |
$all_widget_ids = array(); |
| 227 |
|
| 228 |
// Get a lits of all widget IDs |
| 229 |
foreach ( $sidebars_widgets as $widget_area => $widgets ) |
| 230 |
foreach ( $widgets as $widget_order => $widget_id ) |
| 231 |
$all_widget_ids[] = $widget_id; |
| 232 |
|
| 233 |
// Remove non-existant widget contexts from the settings |
| 234 |
foreach ( $this->context_options as $widget_id => $widget_context ) |
| 235 |
if ( ! in_array( $widget_id, $all_widget_ids ) ) |
| 236 |
unset( $this->context_options[ $widget_id ] ); |
| 237 |
|
| 238 |
update_option( $this->options_name, $this->context_options ); |
| 239 |
|
| 240 |
} |
| 241 |
|
| 242 |
|
| 243 |
function maybe_unset_widgets_by_context( $sidebars_widgets ) { |
| 244 |
|
| 245 |
// Don't run this on the backend |
| 246 |
if ( is_admin() ) |
| 247 |
return $sidebars_widgets; |
| 248 |
|
| 249 |
// Return from cache if we have done the context checks already |
| 250 |
if ( ! empty( $this->sidebars_widgets ) ) |
| 251 |
return $this->sidebars_widgets; |
| 252 |
|
| 253 |
foreach( $sidebars_widgets as $widget_area => $widget_list ) { |
| 254 |
|
| 255 |
if ( $widget_area == 'wp_inactive_widgets' || empty( $widget_list ) ) |
| 256 |
continue; |
| 257 |
|
| 258 |
foreach( $widget_list as $pos => $widget_id ) { |
| 259 |
|
| 260 |
if ( ! $this->check_widget_visibility( $widget_id ) ) |
| 261 |
unset( $sidebars_widgets[ $widget_area ][ $pos ] ); |
| 262 |
|
| 263 |
} |
| 264 |
|
| 265 |
} |
| 266 |
|
| 267 |
// Store in class cache |
| 268 |
$this->sidebars_widgets = $sidebars_widgets; |
| 269 |
|
| 270 |
return $sidebars_widgets; |
| 271 |
|
| 272 |
} |
| 273 |
|
| 274 |
|
| 275 |
function check_widget_visibility( $widget_id ) { |
| 276 |
|
| 277 |
// Check if this widget even has context set |
| 278 |
if ( ! isset( $this->context_options[ $widget_id ] ) ) |
| 279 |
return true; |
| 280 |
|
| 281 |
$matches = array(); |
| 282 |
|
| 283 |
foreach ( $this->contexts as $context_id => $context_settings ) { |
| 284 |
|
| 285 |
// This context check has been disabled in the plugin settings |
| 286 |
if ( isset( $this->context_settings['contexts'][ $context_id ] ) && ! $this->context_settings['contexts'][ $context_id ] ) |
| 287 |
continue; |
| 288 |
|
| 289 |
// Make sure that context settings for this widget are defined |
| 290 |
if ( ! isset( $this->context_options[ $widget_id ][ $context_id ] ) ) |
| 291 |
$widget_context_args = array(); |
| 292 |
else |
| 293 |
$widget_context_args = $this->context_options[ $widget_id ][ $context_id ]; |
| 294 |
|
| 295 |
$matches[ $context_id ] = apply_filters( |
| 296 |
'widget_context_check-' . $context_id, |
| 297 |
null, |
| 298 |
$widget_context_args |
| 299 |
); |
| 300 |
|
| 301 |
} |
| 302 |
|
| 303 |
// Get the match rule for this widget (show/hide/selected/notselected) |
| 304 |
$match_rule = $this->context_options[ $widget_id ][ 'incexc' ][ 'condition' ]; |
| 305 |
|
| 306 |
// Force show or hide the widget! |
| 307 |
if ( $match_rule == 'show' ) |
| 308 |
return true; |
| 309 |
elseif ( $match_rule == 'hide' ) |
| 310 |
return false; |
| 311 |
|
| 312 |
if ( $match_rule == 'selected' ) |
| 313 |
$inc = true; |
| 314 |
else |
| 315 |
$inc = false; |
| 316 |
|
| 317 |
if ( $inc && in_array( true, $matches ) ) |
| 318 |
return true; |
| 319 |
elseif ( ! $inc && ! in_array( true, $matches ) ) |
| 320 |
return true; |
| 321 |
else |
| 322 |
return false; |
| 323 |
|
| 324 |
} |
| 325 |
|
| 326 |
|
| 327 |
/** |
| 328 |
* Default context checks |
| 329 |
*/ |
| 330 |
|
| 331 |
function context_check_incexc( $check, $settings ) { |
| 332 |
|
| 333 |
return $check; |
| 334 |
|
| 335 |
} |
| 336 |
|
| 337 |
|
| 338 |
function context_check_location( $check, $settings ) { |
| 339 |
|
| 340 |
$status = array( |
| 341 |
'is_front_page' => is_front_page(), |
| 342 |
'is_home' => is_home(), |
| 343 |
'is_singular' => is_singular(), |
| 344 |
'is_single' => is_singular( 'post' ), |
| 345 |
'is_page' => ( is_page() && ! is_front_page() ), |
| 346 |
'is_attachment' => is_attachment(), |
| 347 |
'is_search' => is_search(), |
| 348 |
'is_404' => is_404(), |
| 349 |
'is_archive' => is_archive(), |
| 350 |
'is_date' => is_date(), |
| 351 |
'is_day' => is_day(), |
| 352 |
'is_month' => is_month(), |
| 353 |
'is_year' => is_year(), |
| 354 |
'is_category' => is_category(), |
| 355 |
'is_tag' => is_tag(), |
| 356 |
'is_author' => is_author() |
| 357 |
); |
| 358 |
|
| 359 |
$matched = array_intersect_assoc( $settings, $status ); |
| 360 |
|
| 361 |
if ( ! empty( $matched ) ) |
| 362 |
return true; |
| 363 |
|
| 364 |
return $check; |
| 365 |
|
| 366 |
} |
| 367 |
|
| 368 |
|
| 369 |
function context_check_url( $check, $settings ) { |
| 370 |
|
| 371 |
$urls = trim( $settings['urls'] ); |
| 372 |
|
| 373 |
if ( empty( $urls ) ) |
| 374 |
return $check; |
| 375 |
|
| 376 |
if ( $this->match_path( $urls ) ) |
| 377 |
return true; |
| 378 |
|
| 379 |
return $check; |
| 380 |
|
| 381 |
} |
| 382 |
|
| 383 |
|
| 384 |
// Thanks to Drupal: http://api.drupal.org/api/function/drupal_match_path/6 |
| 385 |
function match_path( $patterns ) { |
| 386 |
|
| 387 |
global $wp; |
| 388 |
|
| 389 |
$patterns_safe = array(); |
| 390 |
|
| 391 |
// Get the request URI from WP |
| 392 |
$url_request = $wp->request; |
| 393 |
|
| 394 |
// Append the query string |
| 395 |
if ( ! empty( $_SERVER['QUERY_STRING'] ) ) |
| 396 |
$url_request .= '?' . $_SERVER['QUERY_STRING']; |
| 397 |
|
| 398 |
foreach ( explode( "\n", $patterns ) as $pattern ) |
| 399 |
$patterns_safe[] = trim( trim( $pattern ), '/' ); // Trim trailing and leading slashes |
| 400 |
|
| 401 |
// Remove empty URL patterns |
| 402 |
$patterns_safe = array_filter( $patterns_safe ); |
| 403 |
|
| 404 |
$regexps = '/^('. preg_replace( array( '/(\r\n|\n| )+/', '/\\\\\*/' ), array( '|', '.*' ), preg_quote( implode( "\n", array_filter( $patterns_safe, 'trim' ) ), '/' ) ) .')$/'; |
| 405 |
|
| 406 |
return preg_match( $regexps, $url_request ); |
| 407 |
|
| 408 |
} |
| 409 |
|
| 410 |
|
| 411 |
// Dummy function |
| 412 |
function context_check_admin_notes( $check, $widget_id ) {} |
| 413 |
|
| 414 |
|
| 415 |
// Dummy function |
| 416 |
function context_check_general( $check, $widget_id ) {} |
| 417 |
|
| 418 |
|
| 419 |
/* |
| 420 |
Widget Controls |
| 421 |
*/ |
| 422 |
|
| 423 |
function display_widget_context( $widget_id = null ) { |
| 424 |
|
| 425 |
$controls = array(); |
| 426 |
$controls_disabled = array(); |
| 427 |
$controls_core = array(); |
| 428 |
|
| 429 |
foreach ( $this->contexts as $context_name => $context_settings ) { |
| 430 |
|
| 431 |
$context_classes = array( |
| 432 |
'context-group', |
| 433 |
sprintf( 'context-group-%s', esc_attr( $context_name ) ) |
| 434 |
); |
| 435 |
|
| 436 |
// Hide this context from the admin UX. We can't remove them |
| 437 |
// because settings will get lost if this page is submitted. |
| 438 |
if ( isset( $this->context_settings['contexts'][ $context_name ] ) && ! $this->context_settings['contexts'][ $context_name ] ) { |
| 439 |
$context_classes[] = 'context-inactive'; |
| 440 |
$controls_disabled[] = $context_name; |
| 441 |
} |
| 442 |
|
| 443 |
// Store core controls |
| 444 |
if ( isset( $context_settings['type'] ) && 'core' == $context_settings['type'] ) { |
| 445 |
$controls_core[] = $context_name; |
| 446 |
} |
| 447 |
|
| 448 |
$control_args = array( |
| 449 |
'name' => $context_name, |
| 450 |
'input_prefix' => 'wl' . $this->get_field_name( array( $widget_id, $context_name ) ), |
| 451 |
'settings' => $this->get_field_value( array( $widget_id, $context_name ) ), |
| 452 |
'widget_id' => $widget_id |
| 453 |
); |
| 454 |
|
| 455 |
$context_controls = apply_filters( 'widget_context_control-' . $context_name, $control_args ); |
| 456 |
$context_classes = apply_filters( 'widget_context_classes-' . $context_name, $context_classes, $control_args ); |
| 457 |
|
| 458 |
if ( ! empty( $context_controls ) && is_string( $context_controls ) ) { |
| 459 |
|
| 460 |
$controls[ $context_name ] = sprintf( |
| 461 |
'<div class="%s"> |
| 462 |
<h4 class="context-toggle">%s</h4> |
| 463 |
<div class="context-group-wrap"> |
| 464 |
%s |
| 465 |
</div> |
| 466 |
</div>', |
| 467 |
esc_attr( implode( ' ', $context_classes ) ), |
| 468 |
esc_html( $context_settings['label'] ), |
| 469 |
$context_controls |
| 470 |
); |
| 471 |
|
| 472 |
} |
| 473 |
|
| 474 |
} |
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
// Non-core controls that should be visible if enabled |
| 479 |
$controls_not_core = array_diff( array_keys( $controls ), $controls_core ); |
| 480 |
|
| 481 |
// Check if any non-core context controls have been enabled |
| 482 |
$has_controls = array_diff( $controls_not_core, $controls_disabled ); |
| 483 |
|
| 484 |
if ( empty( $controls ) || empty( $has_controls ) ) { |
| 485 |
|
| 486 |
if ( current_user_can( 'edit_theme_options' ) ) { |
| 487 |
|
| 488 |
$controls = array( sprintf( |
| 489 |
'<p class="error">%s</p>', |
| 490 |
sprintf( |
| 491 |
__( 'No widget controls enabled. You can enable them in <a href="%s">Widget Context settings</a>.', 'widget-context' ), |
| 492 |
admin_url( 'options-general.php?page=widget_context_settings' ) |
| 493 |
) |
| 494 |
) ); |
| 495 |
|
| 496 |
} else { |
| 497 |
|
| 498 |
$controls = array( sprintf( |
| 499 |
'<p class="error">%s</p>', |
| 500 |
__( 'No widget controls enabled.', 'widget-context' ) |
| 501 |
) ); |
| 502 |
|
| 503 |
} |
| 504 |
|
| 505 |
} |
| 506 |
|
| 507 |
if ( current_user_can( 'edit_theme_options' ) ) { |
| 508 |
|
| 509 |
$controls[] = sprintf( |
| 510 |
'<p class="widget-context-settings-link"><a href="%s">%s</a></p>', |
| 511 |
admin_url( 'options-general.php?page=widget_context_settings' ), |
| 512 |
__( 'Widget Context Settings', 'widget-context' ) |
| 513 |
); |
| 514 |
|
| 515 |
} |
| 516 |
|
| 517 |
return sprintf( |
| 518 |
'<div class="widget-context"> |
| 519 |
<div class="widget-context-header"> |
| 520 |
<h3>%s</h3> |
| 521 |
<!-- <a href="#widget-context-%s" class="toggle-contexts hide-if-no-js"> |
| 522 |
<span class="expand">%s</span> |
| 523 |
<span class="collapse">%s</span> |
| 524 |
</a> --> |
| 525 |
</div> |
| 526 |
<div class="widget-context-inside" id="widget-context-%s" data-widget-id="%s"> |
| 527 |
%s |
| 528 |
</div> |
| 529 |
</div>', |
| 530 |
__( 'Widget Context', 'widget-context' ), |
| 531 |
esc_attr( $widget_id ), |
| 532 |
// Toggle buttons |
| 533 |
__( 'Expand', 'widget-context' ), |
| 534 |
__( 'Collapse', 'widget-context' ), |
| 535 |
// Inslide classes |
| 536 |
esc_attr( $widget_id ), |
| 537 |
esc_attr( $widget_id ), |
| 538 |
// Controls |
| 539 |
implode( '', $controls ) |
| 540 |
); |
| 541 |
|
| 542 |
} |
| 543 |
|
| 544 |
|
| 545 |
function control_incexc( $control_args ) { |
| 546 |
|
| 547 |
$options = array( |
| 548 |
'show' => __( 'Show widget everywhere', 'widget-context' ), |
| 549 |
'selected' => __( 'Show widget on selected', 'widget-context' ), |
| 550 |
'notselected' => __( 'Hide widget on selected', 'widget-context' ), |
| 551 |
'hide' => __( 'Hide widget everywhere', 'widget-context' ) |
| 552 |
); |
| 553 |
|
| 554 |
return $this->make_simple_dropdown( $control_args, 'condition', $options ); |
| 555 |
|
| 556 |
} |
| 557 |
|
| 558 |
|
| 559 |
function control_location( $control_args ) { |
| 560 |
|
| 561 |
$options = array( |
| 562 |
'is_front_page' => __( 'Front page', 'widget-context' ), |
| 563 |
'is_home' => __( 'Blog page', 'widget-context' ), |
| 564 |
'is_singular' => __( 'All posts, pages and custom post types', 'widget-context' ), |
| 565 |
'is_single' => __( 'All posts', 'widget-context' ), |
| 566 |
'is_page' => __( 'All pages', 'widget-context' ), |
| 567 |
'is_attachment' => __( 'All attachments', 'widget-context' ), |
| 568 |
'is_search' => __( 'Search results', 'widget-context' ), |
| 569 |
'is_404' => __( '404 error page', 'widget-context' ), |
| 570 |
'is_archive' => __( 'All archives', 'widget-context' ), |
| 571 |
'is_date' => __( 'All date archives', 'widget-context' ), |
| 572 |
'is_day' => __( 'Daily archives', 'widget-context' ), |
| 573 |
'is_month' => __( 'Monthly archives', 'widget-context' ), |
| 574 |
'is_year' => __( 'Yearly archives', 'widget-context' ), |
| 575 |
'is_category' => __( 'All category archives', 'widget-context' ), |
| 576 |
'is_tag' => __( 'All tag archives', 'widget-context' ), |
| 577 |
'is_author' => __( 'All author archives', 'widget-context' ) |
| 578 |
); |
| 579 |
|
| 580 |
foreach ( $options as $option => $label ) |
| 581 |
$out[] = $this->make_simple_checkbox( $control_args, $option, $label ); |
| 582 |
|
| 583 |
return implode( '', $out ); |
| 584 |
|
| 585 |
} |
| 586 |
|
| 587 |
|
| 588 |
function control_url( $control_args ) { |
| 589 |
|
| 590 |
return sprintf( |
| 591 |
'<div>%s</div> |
| 592 |
<p class="help">%s</p>', |
| 593 |
$this->make_simple_textarea( $control_args, 'urls' ), |
| 594 |
__( 'Enter one location fragment per line. Use <strong>*</strong> character as a wildcard. Example: <code>category/peace/*</code> to target all posts in category <em>peace</em>.', 'widget-context' ) |
| 595 |
); |
| 596 |
|
| 597 |
} |
| 598 |
|
| 599 |
|
| 600 |
function control_admin_notes( $control_args ) { |
| 601 |
|
| 602 |
return sprintf( |
| 603 |
'<div>%s</div>', |
| 604 |
$this->make_simple_textarea( $control_args, 'notes' ) |
| 605 |
); |
| 606 |
|
| 607 |
} |
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
/** |
| 612 |
* Widget control helpers |
| 613 |
*/ |
| 614 |
|
| 615 |
|
| 616 |
function make_simple_checkbox( $control_args, $option, $label ) { |
| 617 |
|
| 618 |
if ( isset( $control_args['settings'][ $option ] ) && $control_args['settings'][ $option ] ) |
| 619 |
$value = true; |
| 620 |
else |
| 621 |
$value = false; |
| 622 |
|
| 623 |
return sprintf( |
| 624 |
'<label class="wc-field-checkbox-%s" data-widget-id="%s"> |
| 625 |
<input type="hidden" value="0" name="%s[%s]" /> |
| 626 |
<input type="checkbox" value="1" name="%s[%s]" %s /> %s |
| 627 |
</label>', |
| 628 |
$this->get_field_classname( $option ), |
| 629 |
esc_attr( $control_args['widget_id'] ), |
| 630 |
// Input hidden |
| 631 |
$control_args['input_prefix'], |
| 632 |
esc_attr( $option ), |
| 633 |
// Input value |
| 634 |
$control_args['input_prefix'], |
| 635 |
esc_attr( $option ), |
| 636 |
checked( $value, true, false ), |
| 637 |
// Label |
| 638 |
esc_html( $label ) |
| 639 |
); |
| 640 |
|
| 641 |
} |
| 642 |
|
| 643 |
|
| 644 |
function make_simple_textarea( $control_args, $option, $label = null ) { |
| 645 |
|
| 646 |
if ( isset( $control_args['settings'][ $option ] ) ) |
| 647 |
$value = esc_textarea( $control_args['settings'][ $option ] ); |
| 648 |
else |
| 649 |
$value = ''; |
| 650 |
|
| 651 |
return sprintf( |
| 652 |
'<label class="wc-field-textarea-%s" data-widget-id="%s"> |
| 653 |
<strong>%s</strong> |
| 654 |
<textarea name="%s[%s]">%s</textarea> |
| 655 |
</label>', |
| 656 |
$this->get_field_classname( $option ), |
| 657 |
esc_attr( $control_args['widget_id'] ), |
| 658 |
// Label |
| 659 |
esc_html( $label ), |
| 660 |
// Input |
| 661 |
$control_args['input_prefix'], |
| 662 |
$option, |
| 663 |
$value |
| 664 |
); |
| 665 |
|
| 666 |
} |
| 667 |
|
| 668 |
|
| 669 |
function make_simple_textfield( $control_args, $option, $label_before = null, $label_after = null) { |
| 670 |
|
| 671 |
if ( isset( $control_args['settings'][ $option ] ) ) |
| 672 |
$value = esc_attr( $control_args['settings'][ $option ] ); |
| 673 |
else |
| 674 |
$value = false; |
| 675 |
|
| 676 |
return sprintf( |
| 677 |
'<label class="wc-field-text-%s" data-widget-id="%s"> |
| 678 |
%s |
| 679 |
<input type="text" name="%s[%s]" value="%s" /> |
| 680 |
%s |
| 681 |
</label>', |
| 682 |
$this->get_field_classname( $option ), |
| 683 |
esc_attr( $control_args['widget_id'] ), |
| 684 |
// Before |
| 685 |
$label_before, |
| 686 |
// Input |
| 687 |
$control_args['input_prefix'], |
| 688 |
$option, |
| 689 |
esc_attr( $value ), |
| 690 |
// After |
| 691 |
esc_html( $label_after ) |
| 692 |
); |
| 693 |
|
| 694 |
} |
| 695 |
|
| 696 |
|
| 697 |
function make_simple_dropdown( $control_args, $option, $selection = array(), $label_before = null, $label_after = null ) { |
| 698 |
|
| 699 |
$options = array(); |
| 700 |
|
| 701 |
if ( isset( $control_args['settings'][ $option ] ) ) |
| 702 |
$value = $control_args['settings'][ $option ]; |
| 703 |
else |
| 704 |
$value = false; |
| 705 |
|
| 706 |
if ( empty( $selection ) ) |
| 707 |
$options[] = sprintf( |
| 708 |
'<option value="">%s</option>', |
| 709 |
esc_html__( 'No options available', 'widget-context' ) |
| 710 |
); |
| 711 |
|
| 712 |
foreach ( $selection as $sid => $svalue ) |
| 713 |
$options[] = sprintf( |
| 714 |
'<option value="%s" %s>%s</option>', |
| 715 |
esc_attr( $sid ), |
| 716 |
selected( $value, $sid, false ), |
| 717 |
esc_html( $svalue ) |
| 718 |
); |
| 719 |
|
| 720 |
return sprintf( |
| 721 |
'<label class="wc-field-select-%s" data-widget-id="%s"> |
| 722 |
%s |
| 723 |
<select name="%s[%s]"> |
| 724 |
%s |
| 725 |
</select> |
| 726 |
%s |
| 727 |
</label>', |
| 728 |
$this->get_field_classname( $option ), |
| 729 |
esc_attr( $control_args['widget_id'] ), |
| 730 |
// Before |
| 731 |
$label_before, |
| 732 |
// Input |
| 733 |
$control_args['input_prefix'], |
| 734 |
$option, |
| 735 |
implode( '', $options ), |
| 736 |
// After |
| 737 |
$label_after |
| 738 |
); |
| 739 |
|
| 740 |
} |
| 741 |
|
| 742 |
|
| 743 |
/** |
| 744 |
* Returns [part1][part2][partN] from array( 'part1', 'part2', 'part3' ) |
| 745 |
* @param array $parts i.e. array( 'part1', 'part2', 'part3' ) |
| 746 |
* @return string i.e. [part1][part2][partN] |
| 747 |
*/ |
| 748 |
function get_field_name( $parts ) { |
| 749 |
|
| 750 |
return esc_attr( sprintf( '[%s]', implode( '][', $parts ) ) ); |
| 751 |
|
| 752 |
} |
| 753 |
|
| 754 |
function get_field_classname( $name ) { |
| 755 |
|
| 756 |
if ( is_array( $name ) ) |
| 757 |
$name = end( $name ); |
| 758 |
|
| 759 |
return sanitize_html_class( str_replace( '_', '-', $name ) ); |
| 760 |
|
| 761 |
} |
| 762 |
|
| 763 |
|
| 764 |
/** |
| 765 |
* Given option keys return its value |
| 766 |
* @param array $parts i.e. array( 'part1', 'part2', 'part3' ) |
| 767 |
* @param array $options i.e. array( 'part1' => array( 'part2' => array( 'part3' => 'VALUE' ) ) ) |
| 768 |
* @return string Returns option value |
| 769 |
*/ |
| 770 |
function get_field_value( $parts, $options = null ) { |
| 771 |
|
| 772 |
if ( $options == null ) |
| 773 |
$options = $this->context_options; |
| 774 |
|
| 775 |
$value = false; |
| 776 |
|
| 777 |
if ( empty( $parts ) || ! is_array( $parts ) ) |
| 778 |
return false; |
| 779 |
|
| 780 |
$part = array_shift( $parts ); |
| 781 |
|
| 782 |
if ( ! empty( $parts ) && isset( $options[ $part ] ) && is_array( $options[ $part ] ) ) |
| 783 |
$value = $this->get_field_value( $parts, $options[ $part ] ); |
| 784 |
elseif ( isset( $options[ $part ] ) ) |
| 785 |
return $options[ $part ]; |
| 786 |
|
| 787 |
return $value; |
| 788 |
|
| 789 |
} |
| 790 |
|
| 791 |
|
| 792 |
function fix_legacy_options( $options ) { |
| 793 |
|
| 794 |
if ( empty( $options ) || ! is_array( $options ) ) |
| 795 |
return $options; |
| 796 |
|
| 797 |
foreach ( $options as $widget_id => $option ) { |
| 798 |
|
| 799 |
// This doesn't have an include/exclude rule defined |
| 800 |
if ( ! isset( $option['incexc'] ) ) |
| 801 |
unset( $options[ $widget_id ] ); |
| 802 |
|
| 803 |
// We moved from [incexc] = 1/0 to [incexc][condition] = 1/0 |
| 804 |
if ( isset( $option['incexc'] ) && ! is_array( $option['incexc'] ) ) |
| 805 |
$options[ $widget_id ]['incexc'] = array( 'condition' => $option['incexc'] ); |
| 806 |
|
| 807 |
// Move notes from "general" group to "admin_notes" |
| 808 |
if ( isset( $option['general']['notes'] ) ) { |
| 809 |
$options[ $widget_id ]['admin_notes']['notes'] = $option['general']['notes']; |
| 810 |
unset( $option['general']['notes'] ); |
| 811 |
} |
| 812 |
|
| 813 |
// We moved word count out of location context group |
| 814 |
if ( isset( $option['location']['check_wordcount'] ) ) |
| 815 |
$options[ $widget_id ]['word_count'] = array( |
| 816 |
'check_wordcount' => true, |
| 817 |
'check_wordcount_type' => $option['location']['check_wordcount_type'], |
| 818 |
'word_count' => $option['location']['word_count'] |
| 819 |
); |
| 820 |
|
| 821 |
} |
| 822 |
|
| 823 |
return $options; |
| 824 |
|
| 825 |
} |
| 826 |
|
| 827 |
|
| 828 |
|
| 829 |
/** |
| 830 |
* Admin Settings |
| 831 |
*/ |
| 832 |
|
| 833 |
|
| 834 |
function widget_context_settings_menu() { |
| 835 |
|
| 836 |
add_options_page( |
| 837 |
__( 'Widget Context Settings', 'widget-context' ), |
| 838 |
__( 'Widget Context', 'widget-context' ), |
| 839 |
'manage_options', |
| 840 |
$this->settings_name, |
| 841 |
array( $this, 'widget_context_admin_view' ) |
| 842 |
); |
| 843 |
|
| 844 |
} |
| 845 |
|
| 846 |
|
| 847 |
function widget_context_settings_init() { |
| 848 |
|
| 849 |
register_setting( $this->settings_name, $this->settings_name ); |
| 850 |
|
| 851 |
} |
| 852 |
|
| 853 |
|
| 854 |
function widget_context_admin_view() { |
| 855 |
|
| 856 |
$context_controls = array(); |
| 857 |
|
| 858 |
foreach ( $this->contexts as $context_id => $context_args ) { |
| 859 |
|
| 860 |
// Hide core modules from being disabled |
| 861 |
if ( isset( $context_args['type'] ) && $context_args['type'] == 'core' ) |
| 862 |
continue; |
| 863 |
|
| 864 |
if ( ! empty( $context_args['description'] ) ) |
| 865 |
$context_description = sprintf( |
| 866 |
'<p class="context-desc">%s</p>', |
| 867 |
esc_html( $context_args['description'] ) |
| 868 |
); |
| 869 |
else |
| 870 |
$context_description = null; |
| 871 |
|
| 872 |
// Enable new modules by default |
| 873 |
if ( ! isset( $this->context_settings['contexts'][ $context_id ] ) ) |
| 874 |
$this->context_settings['contexts'][ $context_id ] = 1; |
| 875 |
|
| 876 |
$context_controls[] = sprintf( |
| 877 |
'<li class="context-%s"> |
| 878 |
<label> |
| 879 |
<input type="hidden" name="%s[contexts][%s]" value="0" /> |
| 880 |
<input type="checkbox" name="%s[contexts][%s]" value="1" %s /> %s |
| 881 |
</label> |
| 882 |
%s |
| 883 |
</li>', |
| 884 |
esc_attr( $context_id ), |
| 885 |
$this->settings_name, |
| 886 |
esc_attr( $context_id ), |
| 887 |
$this->settings_name, |
| 888 |
esc_attr( $context_id ), |
| 889 |
checked( $this->context_settings['contexts'][ $context_id ], 1, false ), |
| 890 |
esc_html( $context_args['label'] ), |
| 891 |
$context_description |
| 892 |
); |
| 893 |
|
| 894 |
} |
| 895 |
|
| 896 |
?> |
| 897 |
<div class="wrap wrap-widget-context"> |
| 898 |
<h2><?php esc_html_e( 'Widget Context Settings', 'widget-context' ); ?></h2> |
| 899 |
|
| 900 |
<div class="widget-context-settings-wrap"> |
| 901 |
|
| 902 |
<div class="widget-context-form"> |
| 903 |
<form method="post" action="options.php"> |
| 904 |
<?php |
| 905 |
settings_fields( $this->settings_name ); |
| 906 |
do_settings_sections( $this->settings_name ); |
| 907 |
?> |
| 908 |
|
| 909 |
<?php |
| 910 |
printf( |
| 911 |
'<div class="settings-section settings-section-modules"> |
| 912 |
<h3>%s</h3> |
| 913 |
<ul>%s</ul> |
| 914 |
</div>', |
| 915 |
esc_html__( 'Enabled Context Modules', 'widget-context' ), |
| 916 |
implode( '', $context_controls ) |
| 917 |
); |
| 918 |
?> |
| 919 |
|
| 920 |
<?php |
| 921 |
submit_button(); |
| 922 |
?> |
| 923 |
</form> |
| 924 |
</div> |
| 925 |
|
| 926 |
<div class="widget-context-sidebar"> |
| 927 |
<div class="wc-sidebar-in"> |
| 928 |
|
| 929 |
<div class="wc-sidebar-section wc-sidebar-credits"> |
| 930 |
<p> |
| 931 |
<img src="http://gravatar.com/avatar/661eb21385c25c01ad64ab9e13b37331/?s=60" alt="Kaspars Dambis" width="60" height="60" /> |
| 932 |
<?php printf( esc_html__( 'Widget Context is created and maintained by %s.' ), '<a href="http://kaspars.net">Kaspars Dambis</a>' ); ?> |
| 933 |
</p> |
| 934 |
</div> |
| 935 |
|
| 936 |
<div class="wc-sidebar-section wc-sidebar-newsletter"> |
| 937 |
<h3><?php esc_html_e( 'News & Updates' ); ?></h3> |
| 938 |
<p><?php esc_html_e( 'Subscribe to receive news & updates about the plugin.' ); ?></p> |
| 939 |
<form action="//osc.us2.list-manage.com/subscribe/post?u=e8d173fc54c0fc4286a2b52e8&id=8afe96c5a3" method="post" target="_blank"> |
| 940 |
<?php $user = wp_get_current_user(); ?> |
| 941 |
<p><label><?php _e( 'Your Name', 'widget-context' ); ?>: <input type="text" name="NAME" value="<?php echo esc_attr( sprintf( '%s %s', $user->first_name, $user->last_name ) ) ?>" /></label></p> |
| 942 |
<p><label><?php _e( 'Your Email', 'widget-context' ); ?>: <input type="text" name="EMAIL" value="<?php echo esc_attr( $user->user_email ); ?>" /></label></p> |
| 943 |
<p><input class="button" name="subscribe" type="submit" value="<?php _e( 'Subscribe', 'widget-context' ); ?>" /></p> |
| 944 |
</form> |
| 945 |
</div> |
| 946 |
|
| 947 |
</div> |
| 948 |
</div> |
| 949 |
|
| 950 |
</div> |
| 951 |
</div> |
| 952 |
<?php |
| 953 |
|
| 954 |
} |
| 955 |
|
| 956 |
|
| 957 |
} |
| 958 |
|
| 959 |
|
| 960 |
|