Making WordPress.org

Ticket #1792: class-controls.php

File class-controls.php, 6.4 KB (added by joelcj91, 10 years ago)

Removed unwanted loop and fixed the duplication issue.

Line 
1<?php
2namespace WordPressdotorg\Plugin_Directory\Admin\Metabox;
3
4/**
5 * The Plugin Controls / Publish metabox.
6 *
7 * @package WordPressdotorg\Plugin_Directory\Admin\Metabox
8 */
9class Controls {
10
11 /**
12 * Displays the Publish metabox for plugins.
13 * The HTML here matches what Core uses.
14 */
15 static function display() {
16 echo '<div class="submitbox" id="submitpost">';
17 echo '<div id="misc-publishing-actions">';
18 self::display_post_status();
19
20 if ( 'publish' === get_post_status() ) {
21 self::display_tested_up_to();
22 }
23 echo '</div>';
24
25 echo '<div id="major-publishing-actions"><div id="publishing-action">';
26 echo '<span class="spinner"></span>';
27 printf( '<input type="submit" name="save_changes" id="publish" class="button button-primary button-large" value="%s">', __( 'Save Changes', 'wporg-plugins' ) );
28 echo '</div><div class="clear"></div></div>';
29 echo '</div>';
30 }
31
32 /**
33 * Displays the Plugin Status control in the Publish metabox.
34 */
35 protected static function display_post_status() {
36 $post = get_post();
37
38 // Bail if the current user can't review plugins.
39 if ( ! current_user_can( 'plugin_approve', $post ) && ! current_user_can( 'plugin_review', $post ) ) {
40 return;
41 }
42
43 $statuses = array( 'draft', 'pending' );
44 if ( current_user_can( 'plugin_approve', $post ) ) {
45 if ( in_array( $post->post_status, array( 'draft', 'pending', 'rejected', 'approved' ) ) ) {
46 $statuses = array_merge( $statuses, array( 'approved', 'rejected' ) );
47 } else {
48 $statuses = array( 'publish', 'disabled', 'closed' );
49 }
50 }
51 ?>
52 <div class="misc-pub-section misc-pub-plugin-status">
53 <label for="post_status"><?php _e( 'Status:', 'wporg-plugins' ); ?></label>
54 <strong id="plugin-status-display"><?php echo esc_html( get_post_status_object( $post->post_status )->label ); ?></strong>
55 <button type="button" class="button-link edit-plugin-status hide-if-no-js">
56 <span aria-hidden="true"><?php _e( 'Edit', 'wporg-plugins' ); ?></span>
57 <span class="screen-reader-text"><?php _e( 'Edit plugin status', 'wporg-plugins' ); ?></span>
58 </button>
59
60 <div id="plugin-status-select" class="plugin-control-select hide-if-js">
61 <input type="hidden" name="hidden_post_status" id="hidden-post-status" value="<?php echo esc_attr( $post->post_status ); ?>">
62 <label class="screen-reader-text" for="plugin-status"><?php _e( 'Plugin status', 'wporg-plugins' ); ?></label>
63 <select name="post_status" id="plugin-status">
64 <?php
65 foreach ( $statuses as $statii ) {
66 $status_object = get_post_status_object( $statii );
67 printf(
68 '<option value="%s" %s>%s</option>',
69 esc_attr( $statii ),
70 selected( $post->post_status, $statii, false ),
71 esc_html( $status_object->label )
72 );
73 }
74 ?>
75 </select>
76 <button type="button" class="save-plugin-status hide-if-no-js button"><?php _e( 'OK', 'wporg-plugins' ); ?></button>
77 <button type="button" class="cancel-plugin-status hide-if-no-js button-link"><?php _e( 'Cancel', 'wporg-plugins' ); ?></button>
78 </div>
79
80 </div><!-- .misc-pub-section --><?php
81 }
82
83 /**
84 * Displays the Tested Up To control in the Publish metabox.
85 */
86 protected static function display_tested_up_to() {
87 $post = get_post();
88 $tested_up_to = (string) get_post_meta( $post->ID, 'tested', true );
89 $versions = self::get_tested_up_to_versions( $tested_up_to );
90 $tested_up_to = $versions['tested_up_to'];
91 $unknown_string = _x( 'Unknown', 'unknown version', 'wporg-plugins' );
92 ?>
93 <div class="misc-pub-section misc-pub-tested">
94 <label for="tested_with"><?php _e( 'Tested With:', 'wporg-plugins' ); ?></label>
95 <strong id="tested-with-display"><?php echo ( $tested_up_to ? sprintf( 'WordPress %s', $tested_up_to ) : $unknown_string ); ?></strong>
96 <button type="button" class="button-link edit-tested-with hide-if-no-js">
97 <span aria-hidden="true"><?php _e( 'Edit', 'wporg-plugins' ); ?></span>
98 <span class="screen-reader-text"><?php _e( 'Edit tested with version', 'wporg-plugins' ); ?></span>
99 </button>
100
101 <div id="tested-with-select" class="plugin-control-select hide-if-js">
102 <input type="hidden" name="hidden_tested_with" id="hidden-tested-with" value="<?php echo esc_attr( $tested_up_to ); ?>">
103 <label class="screen-reader-text" for="tested-with"><?php _e( 'Version of WordPress it was tested with', 'wporg-plugins' ); ?></label>
104 <select name="tested_with" id="tested-with">
105 <?php
106 foreach ( $versions['versions'] as $ver ) {
107 printf(
108 '<option value="%s" %s>%s</option>',
109 esc_attr( $ver ),
110 selected( $tested_up_to, $ver, true ),
111 esc_html( $ver ? sprintf( 'WordPress %s', $ver ) : $unknown_string )
112 );
113 }
114 ?>
115 </select>
116 <button type="button" class="save-tested-with hide-if-no-js button"><?php _e( 'OK', 'wporg-plugins' ); ?></button>
117 <button type="button" class="cancel-tested-with hide-if-no-js button-link"><?php _e( 'Cancel', 'wporg-plugins' ); ?></button>
118 </div>
119
120 </div><!-- .misc-pub-section --><?php
121 }
122
123 /**
124 * Fetch all versions which an author can set their plugin as tested with.
125 *
126 * This returns the latest release in the previous 4 branches, trunk, and
127 * the current version the plugin is marked as tested with.
128 *
129 * @param string $tested_up_to The version which the plugin is currently specified as compatible to.
130 *
131 * @return array An array containing 'versions' an array of versions for display, and 'tested_up_to'
132 * the sanitized/most recent version of the $tested_up_to parameter.
133 */
134 protected static function get_tested_up_to_versions( $tested_up_to ) {
135 global $wp_version;
136 // Fetch all "compatible" versions, this array is in the form of [ '4.4.2' => [ '4.4.1', '4.4' ], ...]
137 if ( function_exists( 'wporg_get_version_equivalents' ) ) {
138 // This function is a global WordPress.org function.
139 $all_versions = wporg_get_version_equivalents();
140 } else {
141 $all_versions = array( (string)(float) $wp_version => array( $wp_version ) );
142 }
143
144 $versions = array_slice( array_keys( $all_versions ), 0, 4 );
145 // WordPress.org runs trunk, this keeps the highest version selectable as trunk
146 $versions[5] = preg_replace( '!-\d{4,}$!', '', $wp_version );
147
148 // If the version specified isn't going to display, insert it into the list.
149 if (!in_array($tested_up_to, $versions)) {
150 $versions[4] = $tested_up_to;
151 ksort($versions);
152 }
153
154 return compact( 'versions', 'tested_up_to' );
155 }
156}
157