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