Changeset 1786 for sites/trunk/global.wordpress.org/public_html/wp-content/mu-plugins/roles/rosetta-roles.php
- Timestamp:
- 07/26/2015 04:42:45 PM (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
sites/trunk/global.wordpress.org/public_html/wp-content/mu-plugins/roles/rosetta-roles.php
r1741 r1786 211 211 add_action( 'load-' . $this->translation_editors_page, array( $this, 'load_translation_editors_page' ) ); 212 212 add_action( 'admin_print_scripts-' . $this->translation_editors_page, array( $this, 'enqueue_scripts' ) ); 213 add_action( 'admin_footer-' . $this->translation_editors_page, array( $this, 'print_js_templates' ) ); 214 add_action( 'admin_print_styles-' . $this->translation_editors_page, array( $this, 'enqueue_styles' ) ); 213 215 } 214 216 … … 217 219 */ 218 220 public function enqueue_scripts() { 219 wp_enqueue_script( 'rosetta-roles', plugins_url( '/js/rosetta-roles.js', __FILE__ ), array( 'jquery' ), '1', true ); 221 wp_enqueue_script( 'rosetta-roles', plugins_url( '/js/rosetta-roles.js', __FILE__ ), array( 'jquery', 'wp-backbone' ), '2', true ); 222 } 223 224 /** 225 * Enqueues styles. 226 */ 227 public function enqueue_styles() { 228 wp_enqueue_style( 'rosetta-roles', plugins_url( '/css/rosetta-roles.css', __FILE__ ), array(), '2' ); 229 } 230 231 /** 232 * Prints JavaScript templates. 233 */ 234 public function print_js_templates() { 235 ?> 236 <script id="tmpl-project-checkbox" type="text/html"> 237 <# if ( ! data.checkedSubProjects ) { 238 #> 239 <label> 240 <input type="checkbox" class="input-checkbox" name="projects[]" value="{{data.id}}" 241 <# 242 if ( data.checked ) { 243 #> checked="checked"<# 244 } 245 #> 246 /> 247 {{data.name}} 248 </label> 249 <# } else { #> 250 <label> 251 <input type="radio" class="input-radio" checked="checked" /> {{data.name}} 252 </label> 253 <# } #> 254 </script> 255 <?php 220 256 } 221 257 … … 290 326 $this->notify_translation_editor_update( $user_details->ID, 'add' ); 291 327 328 $meta_key = $wpdb->get_blog_prefix() . $this->project_access_meta_key; 329 292 330 $projects = empty( $_REQUEST['projects'] ) ? '' : $_REQUEST['projects']; 293 331 if ( 'custom' === $projects ) { 332 update_user_meta( $user_details->ID, $meta_key, array() ); 294 333 $redirect = add_query_arg( 'user_id', $user_details->ID, $redirect ); 295 334 wp_redirect( add_query_arg( array( 'update' => 'user-added-custom-projects' ), $redirect ) ); … … 297 336 } 298 337 299 $meta_key = $wpdb->get_blog_prefix() . $this->project_access_meta_key;300 338 update_user_meta( $user_details->ID, $meta_key, array( 'all' ) ); 301 339 … … 393 431 $redirect = add_query_arg( 'user_id', $user_details->ID, $redirect ); 394 432 395 $all_projects = $this->get_translate_ top_level_projects();433 $all_projects = $this->get_translate_projects(); 396 434 $all_projects = wp_list_pluck( $all_projects, 'id' ); 397 435 $all_projects = array_map( 'intval', $all_projects ); … … 431 469 global $wpdb; 432 470 433 $projects = $this->get_translate_top_level_projects(); 471 $projects = $this->get_translate_projects(); 472 $project_tree = $this->get_project_tree( $projects, 0, 1 ); 473 474 // Sort the tree and remove array keys. 475 usort( $project_tree, array( $this, '_sort_name_callback' ) ); 476 foreach ( $project_tree as $key => $project ) { 477 if ( $project->sub_projects ) { 478 usort( $project->sub_projects, array( $this, '_sort_name_callback' ) ); 479 } 480 $project->sub_projects = array_values( $project->sub_projects ); 481 } 482 $project_tree = array_values( $project_tree ); 434 483 435 484 $meta_key = $wpdb->get_blog_prefix() . $this->project_access_meta_key; … … 438 487 $project_access_list = array(); 439 488 } 489 490 wp_localize_script( 'rosetta-roles', '_rosettaProjectsSettings', array( 491 'l10n' => array( 492 'searchPlaceholder' => esc_attr__( 'Search...', 'rosetta' ) 493 ), 494 'data' => $project_tree, 495 'accessList' => $project_access_list 496 ) ); 440 497 441 498 $feedback_message = $this->get_feedback_message(); … … 504 561 } 505 562 563 $projects = $this->get_translate_projects(); 564 $project_tree = $this->get_project_tree( $projects, 0, 1 ); 565 506 566 $args = array( 507 567 'user_role' => $this->translation_editor_role, 508 'projects' => $this->get_translate_top_level_projects(), 568 'projects' => $projects, 569 'project_tree' => $project_tree, 509 570 'project_access_meta_key' => $wpdb->get_blog_prefix() . $this->project_access_meta_key, 510 571 ); … … 535 596 536 597 /** 537 * Fetches all top levelprojects from translate.wordpress.org.598 * Fetches all projects from translate.wordpress.org. 538 599 * 539 600 * @return array List of projects. 540 601 */ 541 private function get_translate_ top_level_projects() {602 private function get_translate_projects() { 542 603 global $wpdb; 543 544 $cache = get_site_transient( 'translate-top-level-projects' ); 545 if ( false !== $cache) {546 return $ cache;604 static $projects = null; 605 606 if ( null !== $projects ) { 607 return $projects; 547 608 } 548 609 549 610 $_projects = $wpdb->get_results( " 550 SELECT id, name 611 SELECT id, name, parent_project_id 551 612 FROM translate_projects 552 WHERE parent_project_id IS NULL 553 ORDER BY name ASC 613 ORDER BY id ASC 554 614 " ); 555 615 … … 559 619 } 560 620 561 set_site_transient( 'translate-top-level-projects', $projects, DAY_IN_SECONDS );562 563 621 return $projects; 564 622 } 623 624 /** 625 * Transforms a flat array to a hierarchy tree. 626 * 627 * @param array $projects The projects. 628 * @param int $parent_id Optional. Parent ID. Default 0. 629 * @param int $max_depth Optional. Max depth to avoid endless recursion. Default 5. 630 * @return array The project tree. 631 */ 632 public function get_project_tree( $projects, $parent_id = 0, $max_depth = 5 ) { 633 if ( $max_depth < 0 ) { // Avoid an endless recursion. 634 return; 635 } 636 637 $tree = array(); 638 foreach ( $projects as $project ) { 639 if ( $project->parent_project_id == $parent_id ) { 640 $sub_projects = $this->get_project_tree( $projects, $project->id, $max_depth - 1 ); 641 if ( $sub_projects ) { 642 $project->sub_projects = $sub_projects; 643 } 644 645 $tree[ $project->id ] = $project; 646 } 647 } 648 return $tree; 649 } 650 651 private function _sort_name_callback( $a, $b ) { 652 return strnatcasecmp( $a->name, $b->name ); 653 } 565 654 }
Note: See TracChangeset
for help on using the changeset viewer.