3rd-party
4 days ago
abstracts
3 months ago
admin
2 weeks ago
emails
2 weeks ago
forms
4 days ago
helper
3 months ago
promoted-jobs
4 days ago
ui
2 weeks ago
widgets
2 weeks ago
class-access-token.php
2 years ago
class-dev-tools.php
2 years ago
class-guest-session.php
2 years ago
class-guest-user.php
2 years ago
class-job-dashboard-shortcode.php
6 months ago
class-job-listing-stats.php
2 years ago
class-job-overlay.php
2 years ago
class-stats-dashboard.php
2 years ago
class-stats-script.php
3 months ago
class-stats.php
3 months ago
class-wp-job-manager-ajax.php
2 months ago
class-wp-job-manager-api.php
6 years ago
class-wp-job-manager-blocks.php
2 weeks ago
class-wp-job-manager-cache-helper.php
3 months ago
class-wp-job-manager-category-walker.php
6 years ago
class-wp-job-manager-com-api.php
2 years ago
class-wp-job-manager-data-cleaner.php
2 years ago
class-wp-job-manager-data-exporter.php
3 months ago
class-wp-job-manager-dependency-checker.php
2 years ago
class-wp-job-manager-email-notifications.php
2 years ago
class-wp-job-manager-forms.php
5 years ago
class-wp-job-manager-geocode.php
2 weeks ago
class-wp-job-manager-install.php
2 years ago
class-wp-job-manager-post-types.php
4 days ago
class-wp-job-manager-recaptcha.php
6 months ago
class-wp-job-manager-rest-api.php
2 months ago
class-wp-job-manager-shortcodes.php
2 weeks ago
class-wp-job-manager-usage-tracking-data.php
2 years ago
class-wp-job-manager-usage-tracking.php
2 years ago
class-wp-job-manager-widget.php
2 weeks ago
class-wp-job-manager.php
3 months ago
trait-singleton.php
2 years ago
class-wp-job-manager-widget.php
241 lines
| 1 | <?php |
| 2 | /** |
| 3 | * File containing the class WP_Job_Manager_Widget. |
| 4 | * |
| 5 | * @package wp-job-manager |
| 6 | */ |
| 7 | |
| 8 | if ( ! defined( 'ABSPATH' ) ) { |
| 9 | exit; |
| 10 | } |
| 11 | |
| 12 | /** |
| 13 | * Job Manager Widget base. |
| 14 | * |
| 15 | * @since 1.0.0 |
| 16 | */ |
| 17 | class WP_Job_Manager_Widget extends WP_Widget { |
| 18 | |
| 19 | /** |
| 20 | * Widget CSS class. |
| 21 | * |
| 22 | * @var string |
| 23 | */ |
| 24 | public $widget_cssclass; |
| 25 | |
| 26 | /** |
| 27 | * Widget description. |
| 28 | * |
| 29 | * @var string |
| 30 | */ |
| 31 | public $widget_description; |
| 32 | |
| 33 | /** |
| 34 | * Widget id. |
| 35 | * |
| 36 | * @var string |
| 37 | */ |
| 38 | public $widget_id; |
| 39 | |
| 40 | /** |
| 41 | * Widget name. |
| 42 | * |
| 43 | * @var string |
| 44 | */ |
| 45 | public $widget_name; |
| 46 | |
| 47 | /** |
| 48 | * Widget settings. |
| 49 | * |
| 50 | * @var array |
| 51 | */ |
| 52 | public $settings; |
| 53 | |
| 54 | /** |
| 55 | * Constructor. |
| 56 | */ |
| 57 | public function __construct() { |
| 58 | $this->register(); |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Registers widget. |
| 63 | */ |
| 64 | public function register() { |
| 65 | $widget_ops = [ |
| 66 | 'classname' => $this->widget_cssclass, |
| 67 | 'description' => $this->widget_description, |
| 68 | 'show_instance_in_rest' => true, |
| 69 | ]; |
| 70 | |
| 71 | parent::__construct( $this->widget_id, $this->widget_name, $widget_ops ); |
| 72 | |
| 73 | add_action( 'save_post', [ $this, 'flush_widget_cache' ] ); |
| 74 | add_action( 'deleted_post', [ $this, 'flush_widget_cache' ] ); |
| 75 | add_action( 'switch_theme', [ $this, 'flush_widget_cache' ] ); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Checks for cached version of widget and outputs it if found. |
| 80 | * |
| 81 | * @param array $args |
| 82 | * @return bool |
| 83 | */ |
| 84 | public function get_cached_widget( $args ) { |
| 85 | if ( empty( $args['widget_id'] ) ) { |
| 86 | return false; |
| 87 | } |
| 88 | |
| 89 | $cache = wp_cache_get( $this->widget_id, 'widget' ); |
| 90 | |
| 91 | if ( ! is_array( $cache ) ) { |
| 92 | $cache = []; |
| 93 | } |
| 94 | |
| 95 | if ( isset( $cache[ $args['widget_id'] ] ) ) { |
| 96 | echo $cache[ $args['widget_id'] ]; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 97 | return true; |
| 98 | } |
| 99 | |
| 100 | return false; |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Caches the widget. |
| 105 | * |
| 106 | * @param array $args |
| 107 | * @param string $content |
| 108 | */ |
| 109 | public function cache_widget( $args, $content ) { |
| 110 | if ( empty( $args['widget_id'] ) ) { |
| 111 | return; |
| 112 | } |
| 113 | |
| 114 | $cache = wp_cache_get( $this->widget_id, 'widget' ); |
| 115 | |
| 116 | if ( ! is_array( $cache ) ) { |
| 117 | $cache = []; |
| 118 | } |
| 119 | |
| 120 | $cache[ $args['widget_id'] ] = $content; |
| 121 | |
| 122 | wp_cache_set( $this->widget_id, $cache, 'widget' ); |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Flushes the cache for a widget. |
| 127 | */ |
| 128 | public function flush_widget_cache() { |
| 129 | wp_cache_delete( $this->widget_id, 'widget' ); |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Updates a widget instance settings. |
| 134 | * |
| 135 | * @see WP_Widget->update |
| 136 | * @param array $new_instance |
| 137 | * @param array $old_instance |
| 138 | * @return array |
| 139 | */ |
| 140 | public function update( $new_instance, $old_instance ) { |
| 141 | $instance = $old_instance; |
| 142 | |
| 143 | if ( ! $this->settings ) { |
| 144 | return $instance; |
| 145 | } |
| 146 | |
| 147 | foreach ( $this->settings as $key => $setting ) { |
| 148 | $instance[ $key ] = isset( $new_instance[ $key ] ) ? sanitize_text_field( $new_instance[ $key ] ) : ''; |
| 149 | } |
| 150 | |
| 151 | $this->flush_widget_cache(); |
| 152 | |
| 153 | return $instance; |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Displays widget setup form. |
| 158 | * |
| 159 | * @see WP_Widget->form |
| 160 | * @param array $instance |
| 161 | * @return void |
| 162 | */ |
| 163 | public function form( $instance ) { |
| 164 | |
| 165 | if ( ! $this->settings ) { |
| 166 | return; |
| 167 | } |
| 168 | |
| 169 | foreach ( $this->settings as $key => $setting ) { |
| 170 | |
| 171 | $value = isset( $instance[ $key ] ) ? $instance[ $key ] : $setting['std']; |
| 172 | |
| 173 | switch ( $setting['type'] ) { |
| 174 | case 'text': |
| 175 | ?> |
| 176 | <p> |
| 177 | <label for="<?php echo esc_attr( $this->get_field_id( $key ) ); ?>"><?php echo esc_html( $setting['label'] ); ?></label> |
| 178 | <input class="widefat" id="<?php echo esc_attr( $this->get_field_id( $key ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( $key ) ); ?>" type="text" value="<?php echo esc_attr( $value ); ?>" /> |
| 179 | </p> |
| 180 | <?php |
| 181 | break; |
| 182 | case 'number': |
| 183 | ?> |
| 184 | <p> |
| 185 | <label for="<?php echo esc_attr( $this->get_field_id( $key ) ); ?>"><?php echo esc_html( $setting['label'] ); ?></label> |
| 186 | <input class="widefat" id="<?php echo esc_attr( $this->get_field_id( $key ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( $key ) ); ?>" type="number" step="<?php echo esc_attr( $setting['step'] ); ?>" min="<?php echo esc_attr( $setting['min'] ); ?>" max="<?php echo esc_attr( $setting['max'] ); ?>" value="<?php echo esc_attr( $value ); ?>" /> |
| 187 | </p> |
| 188 | <?php |
| 189 | break; |
| 190 | case 'select': |
| 191 | ?> |
| 192 | <p> |
| 193 | <label for="<?php echo esc_attr( $this->get_field_id( $key ) ); ?>"><?php echo esc_html( $setting['label'] ); ?></label> |
| 194 | <select class="widefat" id="<?php echo esc_attr( $this->get_field_id( $key ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( $key ) ); ?>"> |
| 195 | <?php foreach ( $setting['options'] as $option_key => $option_label ) : ?> |
| 196 | <option value="<?php echo esc_attr( $option_key ); ?>" <?php selected( $value, $option_key ); ?>><?php echo esc_html( $option_label ); ?></option> |
| 197 | <?php endforeach; ?></select> |
| 198 | </p> |
| 199 | <?php |
| 200 | break; |
| 201 | case 'checkbox': |
| 202 | ?> |
| 203 | <p> |
| 204 | <input class="checkbox" id="<?php echo esc_attr( $this->get_field_id( $key ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( $key ) ); ?>" type="checkbox" value="1" <?php checked( $value, 1 ); ?> /> |
| 205 | <label for="<?php echo esc_attr( $this->get_field_id( $key ) ); ?>"><?php echo esc_html( $setting['label'] ); ?></label> |
| 206 | </p> |
| 207 | <?php |
| 208 | break; |
| 209 | } |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | /** |
| 214 | * Gets the instance with the default values for all settings. |
| 215 | * |
| 216 | * @return array |
| 217 | */ |
| 218 | protected function get_default_instance() { |
| 219 | $defaults = []; |
| 220 | if ( ! empty( $this->settings ) ) { |
| 221 | foreach ( $this->settings as $key => $setting ) { |
| 222 | $defaults[ $key ] = null; |
| 223 | if ( isset( $setting['std'] ) ) { |
| 224 | $defaults[ $key ] = $setting['std']; |
| 225 | } |
| 226 | } |
| 227 | } |
| 228 | return $defaults; |
| 229 | } |
| 230 | |
| 231 | /** |
| 232 | * Echoes the widget content. |
| 233 | * |
| 234 | * @see WP_Widget |
| 235 | * |
| 236 | * @param array $args |
| 237 | * @param array $instance |
| 238 | */ |
| 239 | public function widget( $args, $instance ) {} |
| 240 | } |
| 241 |