Changeset 8309
- Timestamp:
- 02/21/2019 06:15:15 PM (6 years ago)
- Location:
- sites/trunk/wordcamp.org/public_html/wp-content/plugins/camptix-admin-flags
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
sites/trunk/wordcamp.org/public_html/wp-content/plugins/camptix-admin-flags/addons/admin-flags.php
r8308 r8309 10 10 */ 11 11 class CampTix_Admin_Flags_Addon extends CampTix_Addon { 12 12 protected $flags = array(); 13 14 /** 15 * Constructor. 16 */ 13 17 public function __construct() { 14 18 add_action( 'camptix_init', array( $this, 'camptix_init' ) ); … … 28 32 } 29 33 30 $this->flags = array();31 34 $camptix_options = $camptix->get_options(); 32 35 33 36 // No further actions if we don't have any configured flags. 34 if ( empty( $camptix_options['camptix-admin-flags-data-parsed'] ) ) 35 return; 37 if ( empty( $camptix_options['camptix-admin-flags-data-parsed'] ) ) { 38 return; 39 } 36 40 37 41 $this->flags = (array) $camptix_options['camptix-admin-flags-data-parsed']; 38 42 39 43 if ( current_user_can( $camptix->caps['manage_attendees'] ) ) { 40 // Individual editing from Edit Attendee screen 44 // Individual editing from Edit Attendee screen. 41 45 add_action( 'save_post', array( $this, 'save_post' ), 11, 1 ); 42 46 add_action( 'camptix_attendee_submitdiv_misc', array( $this, 'publish_metabox_actions' ) ); 43 47 44 // Bulk editing from Attendees screen 48 // Bulk editing from Attendees screen. 45 49 add_filter( 'manage_tix_attendee_posts_columns', array( $this, 'add_custom_columns' ) ); 46 50 add_action( 'manage_tix_attendee_posts_custom_column', array( $this, 'render_custom_columns' ), 10, 2 ); … … 67 71 public function shortcode_attendees_atts( $out, $pairs, $atts ) { 68 72 $admin_flags = array(); 69 if ( ! empty( $atts['has_admin_flag'] ) ) 73 if ( ! empty( $atts['has_admin_flag'] ) ) { 70 74 $admin_flags = array_map( 'trim', explode( ',', $atts['has_admin_flag'] ) ); 75 } 71 76 72 77 $admin_flags_clean = array(); 73 foreach ( $this->flags as $key => $label ) 74 if ( in_array( $key, $admin_flags ) )78 foreach ( $this->flags as $key => $label ) { 79 if ( in_array( $key, $admin_flags, true ) ) { 75 80 $admin_flags_clean[] = $key; 81 } 82 } 76 83 77 84 $out['has_admin_flag'] = $admin_flags; … … 83 90 */ 84 91 public function shortcode_attendees_query( $query_args, $shortcode_args ) { 85 if ( empty( $shortcode_args['has_admin_flag'] ) ) 92 if ( empty( $shortcode_args['has_admin_flag'] ) ) { 86 93 return $query_args; 94 } 87 95 88 96 // Sanitized in self::shortcode_attendees_atts. 89 97 $flags = $shortcode_args['has_admin_flag']; 90 98 91 if ( empty( $query_args['meta_query'] ) ) 99 if ( empty( $query_args['meta_query'] ) ) { 92 100 $query_args['meta_query'] = array(); 101 } 93 102 94 103 foreach ( $flags as $flag ) { 95 104 $query_args['meta_query'][] = array( 96 'key' => 'camptix-admin-flag',97 'value' => $flag,105 'key' => 'camptix-admin-flag', 106 'value' => $flag, 98 107 'compare' => '=', 99 'type' => 'CHAR',108 'type' => 'CHAR', 100 109 ); 101 110 } … … 109 118 public function export_columns( $columns ) { 110 119 foreach ( $this->flags as $key => $label ) { 111 $column_key = sprintf( 'camptix-admin-flags-%s', $key );120 $column_key = sprintf( 'camptix-admin-flags-%s', $key ); 112 121 $columns[ $column_key ] = $label; 113 122 } … … 123 132 */ 124 133 public function export_columns_values( $value, $index, $attendee ) { 125 if ( 0 !== strpos( $index, 'camptix-admin-flags-' ) ) 134 if ( 0 !== strpos( $index, 'camptix-admin-flags-' ) ) { 126 135 return $value; 136 } 127 137 128 138 // See self::export_columns() for key format. 129 139 $key = str_replace( 'camptix-admin-flags-', '', $index ); 130 if ( ! array_key_exists( $key, $this->flags ) ) 140 if ( ! array_key_exists( $key, $this->flags ) ) { 131 141 return $value; 142 } 132 143 133 144 $attendee_flags = (array) get_post_meta( $attendee->ID, 'camptix-admin-flag' ); 134 return in_array( $key, $attendee_flags ) ? 'Yes' : 'No';145 return in_array( $key, $attendee_flags, true ) ? 'Yes' : 'No'; 135 146 } 136 147 … … 149 160 global $camptix; 150 161 151 if ( 'admin-flags' != $section ) 152 return; 162 if ( 'admin-flags' !== $section ) { 163 return; 164 } 153 165 154 166 add_settings_section( 'general', __( 'Admin Flags', 'camptix' ), array( $this, 'setup_controls_section' ), 'camptix_options' ); … … 171 183 */ 172 184 public function validate_options( $output, $input ) { 173 if ( ! isset( $input['camptix-admin-flags-data'] ) ) 185 if ( ! isset( $input['camptix-admin-flags-data'] ) ) { 174 186 return $output; 187 } 175 188 176 189 $has_error = false; 177 $flags = array();178 $data = explode( "\n", $input['camptix-admin-flags-data'] );190 $flags = array(); 191 $data = explode( "\n", $input['camptix-admin-flags-data'] ); 179 192 180 193 foreach ( $data as $line ) { 181 194 182 if ( empty( $line ) ) 195 if ( empty( $line ) ) { 183 196 continue; 184 185 // flag-key: Flag Label 197 } 198 199 // flag-key: Flag Label. 186 200 if ( ! preg_match( '#^([^:]+?):(.+)$#', $line, $matches ) ) { 187 201 $has_error = true; … … 189 203 } 190 204 191 $key = sanitize_html_class( sanitize_title_with_dashes( trim( $matches[1] ) ) );192 $label = trim( $matches[2] );205 $key = sanitize_html_class( sanitize_title_with_dashes( trim( $matches[1] ) ) ); 206 $label = trim( $matches[2] ); 193 207 $flags[ $key ] = $label; 194 208 } 195 209 196 210 $lines = array(); 197 foreach ( $flags as $key => $label ) 211 foreach ( $flags as $key => $label ) { 198 212 $lines[] = sprintf( '%s: %s', $key, $label ); 199 200 $output['camptix-admin-flags-data'] = implode( "\n", $lines ); 213 } 214 215 $output['camptix-admin-flags-data'] = implode( "\n", $lines ); 201 216 $output['camptix-admin-flags-data-parsed'] = $flags; 202 217 203 if ( $has_error ) 218 if ( $has_error ) { 204 219 add_settings_error( 'tix', 'error', __( 'Flags data has been saved, but one or more flags was invalid, so it has been stripped.', 'camptix' ), 'error' ); 220 } 205 221 206 222 return $output; … … 211 227 */ 212 228 public function save_post( $post_id ) { 213 if ( wp_is_post_revision( $post_id ) || 'tix_attendee' != get_post_type( $post_id ) ) 214 return; 215 216 if ( empty( $_POST['camptix-admin-flags-nonce'] ) || ! wp_verify_nonce( $_POST['camptix-admin-flags-nonce'], 'camptix-admin-flags-update' ) ) 217 return; 229 if ( wp_is_post_revision( $post_id ) || 'tix_attendee' !== get_post_type( $post_id ) ) { 230 return; 231 } 232 233 if ( empty( $_POST['camptix-admin-flags-nonce'] ) || ! wp_verify_nonce( $_POST['camptix-admin-flags-nonce'], 'camptix-admin-flags-update' ) ) { 234 return; 235 } 218 236 219 237 delete_post_meta( $post_id, 'camptix-admin-flag' ); 220 238 221 foreach ( $this->flags as $key => $label ) 222 if ( ! empty( $_POST['camptix-admin-flags'][ $key ] ) ) 239 foreach ( $this->flags as $key => $label ) { 240 if ( ! empty( $_POST['camptix-admin-flags'][ $key ] ) ) { 223 241 add_post_meta( $post_id, 'camptix-admin-flag', $key ); 242 } 243 } 224 244 } 225 245 … … 228 248 */ 229 249 public function publish_metabox_actions() { 230 $post = get_post();250 $post = get_post(); 231 251 $attendee_flags = (array) get_post_meta( $post->ID, 'camptix-admin-flag' ); 232 252 ?> … … 237 257 238 258 <div class="tix-pub-section-item"> 239 <input id="camptix-admin-flags-<?php echo sanitize_html_class( $key ); ?>" name="camptix-admin-flags[<?php echo esc_attr( $key ); ?>]" type="checkbox" <?php checked( in_array( $key, $attendee_flags ) ); ?> value="1" />259 <input id="camptix-admin-flags-<?php echo sanitize_html_class( $key ); ?>" name="camptix-admin-flags[<?php echo esc_attr( $key ); ?>]" type="checkbox" <?php checked( in_array( $key, $attendee_flags, true ) ); ?> value="1" /> 240 260 <label for="camptix-admin-flags-<?php echo sanitize_html_class( $key ); ?>"><?php echo esc_html( $label ); ?></label> 241 261 </div> … … 263 283 * 264 284 * @param string $column 265 * @param int $attendee_id285 * @param int $attendee_id 266 286 */ 267 287 public function render_custom_columns( $column, $attendee_id ) { … … 271 291 case 'admin-flags': 272 292 echo '<ul>'; 273 foreach ( $this->flags as $key => $label ) { 274 $enabled = in_array( $key, $attendee_flags ); 275 276 ?> 277 278 <li> 279 <span class="tix-admin-flag-label"><?php echo esc_html( $label ); ?></span>: 280 281 <a 282 href="#" 283 data-attendee-id="<?php echo esc_attr( $attendee_id ); ?>" 284 data-key="<?php echo esc_attr( $key ); ?>" 285 data-nonce="<?php echo esc_attr( wp_create_nonce( sprintf( 'tix_toggle_flag_%s_%s', $attendee_id, $key ) ) ); ?>" 286 data-command="<?php echo esc_attr( $enabled ? 'disable' : 'enable' ); ?>" 287 class="tix-toggle-flag"> 288 289 <?php if ( $enabled ) : ?> 290 <?php _e( 'Disable', 'camptix' ); ?> 291 <?php else : ?> 292 <?php _e( 'Enable', 'camptix' ); ?> 293 <?php endif; ?> 294 </a> 295 </li> 296 297 <?php 298 } 293 294 foreach ( $this->flags as $key => $label ) { 295 $enabled = in_array( $key, $attendee_flags, true ); 296 297 ?> 298 299 <li> 300 <span class="tix-admin-flag-label"> 301 <?php echo esc_html( $label ); ?> 302 </span>: 303 304 <a 305 href="#" 306 data-attendee-id="<?php echo esc_attr( $attendee_id ); ?>" 307 data-key="<?php echo esc_attr( $key ); ?>" 308 data-nonce="<?php echo esc_attr( wp_create_nonce( sprintf( 'tix_toggle_flag_%s_%s', $attendee_id, $key ) ) ); ?>" 309 data-command="<?php echo esc_attr( $enabled ? 'disable' : 'enable' ); ?>" 310 class="tix-toggle-flag"> 311 312 <?php if ( $enabled ) : ?> 313 <?php esc_html_e( 'Disable', 'camptix' ); ?> 314 <?php else : ?> 315 <?php esc_html_e( 'Enable', 'camptix' ); ?> 316 <?php endif; ?> 317 </a> 318 </li> 319 320 <?php 321 } 322 299 323 echo '</ul>'; 300 301 324 break; 302 325 } … … 334 357 335 358 foreach ( $this->flags as $flag => $label ) { 336 $count = 0;359 $count = 0; 337 360 $class_html = ''; 338 361 $url = add_query_arg( 'camptix_flag', $flag, $base_url ); 339 362 340 if ( $currently_viewed_flag && $currently_viewed_flag == $flag ) {363 if ( $currently_viewed_flag && $currently_viewed_flag === $flag ) { 341 364 $class_html = ' class="current"'; 342 365 } … … 392 415 */ 393 416 public function render_client_side_templates() { 394 if ( 'tix_attendee' != $GLOBALS['typenow'] ) {417 if ( 'tix_attendee' !== $GLOBALS['typenow'] ) { 395 418 return; 396 419 } … … 413 436 class="tix-toggle-flag"> 414 437 415 {{data.command}} <?php // todo use i18n var ?>438 {{data.command}} <?php // todo use i18n var. ?> 416 439 </a> 417 440 </script> … … 424 447 */ 425 448 public function toggle_flag() { 426 /** @var $camptix CampTix_Plugin*/449 /** @var CampTix_Plugin $camptix */ 427 450 global $camptix; 428 451 … … 441 464 $attendee = get_post( $attendee_id ); 442 465 443 if ( ! is_a( $attendee, 'WP_Post' ) || 'tix_attendee' != $attendee->post_type ) {466 if ( ! is_a( $attendee, 'WP_Post' ) || 'tix_attendee' !== $attendee->post_type ) { 444 467 wp_send_json_error( array( 'error' => 'Invalid attendee.' ) ); 445 468 } 446 469 447 if ( 'enable' == $command ) {470 if ( 'enable' === $command ) { 448 471 add_post_meta( $attendee_id, 'camptix-admin-flag', $key ); 449 } elseif ( 'disable' == $command ) {472 } elseif ( 'disable' === $command ) { 450 473 delete_post_meta( $attendee_id, 'camptix-admin-flag', $key ); 451 474 } … … 458 481 */ 459 482 public function print_javascript() { 460 if ( 'tix_attendee' != $GLOBALS['typenow'] ) {483 if ( 'tix_attendee' !== $GLOBALS['typenow'] ) { 461 484 return; 462 485 } … … 526 549 */ 527 550 public function print_css() { 528 if ( 'tix_attendee' != $GLOBALS['typenow'] ) {551 if ( 'tix_attendee' !== $GLOBALS['typenow'] ) { 529 552 return; 530 553 } -
sites/trunk/wordcamp.org/public_html/wp-content/plugins/camptix-admin-flags/camptix-admin-flags.php
r907 r8309 1 1 <?php 2 2 3 /** 3 4 * Plugin Name: CampTix - Admin Flags 4 5 * Description: An addon for CampTix that allows admins to configure and set private per-attendee flags. 5 * Version: 0.16 * Author: Konstantin Kovshenin7 * Author URI: http://kovshenin.com6 * Version: 0.1 7 * Author: Konstantin Kovshenin 8 * Author URI: http://kovshenin.com 8 9 */ 9 10 10 add_action( 'camptix_load_addons', 'camptix_admin_flags_register' ); 11 /** 12 * Register this addon with CampTix. 13 */ 11 14 function camptix_admin_flags_register() { 12 15 require_once( plugin_dir_path( __FILE__ ) . 'addons/admin-flags.php' ); 13 16 } 17 add_action( 'camptix_load_addons', 'camptix_admin_flags_register' );
Note: See TracChangeset
for help on using the changeset viewer.