Changeset 10169 for sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-learn/inc/class-markdown-import.php
- Timestamp:
- 08/13/2020 11:43:14 PM (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-learn/inc/class-markdown-import.php
r10147 r10169 9 9 10 10 private static $lesson_plan_manifest = 'https://wptrainingteam.github.io/manifest.json'; 11 private static $input_name = 'wporg-learn-markdown-source';12 private static $meta_key = 'wporg_learn_markdown_source';13 private static $nonce_name = 'wporg-learn-markdown-source-nonce';14 private static $submit_name = 'wporg-learn-markdown-import';15 private static $supported_post_type = 'lesson-plan';16 private static $posts_per_page = 100;11 private static $input_name = 'wporg-learn-markdown-source'; 12 private static $meta_key = 'wporg_learn_markdown_source'; 13 private static $nonce_name = 'wporg-learn-markdown-source-nonce'; 14 private static $submit_name = 'wporg-learn-markdown-import'; 15 private static $supported_post_type = 'lesson-plan'; 16 private static $posts_per_page = 100; 17 17 18 18 /** … … 28 28 } 29 29 30 /** 31 * Actions taken on `wporg_learn_manifest_import` event. 32 */ 30 33 public static function action_wporg_learn_manifest_import() { 31 34 $response = wp_remote_get( self::$lesson_plan_manifest ); … … 37 40 $manifest = json_decode( wp_remote_retrieve_body( $response ), true ); 38 41 if ( ! $manifest ) { 39 return new WP_Error( 'invalid-manifest', 'Manifest did not unfurl properly.' ); ;42 return new WP_Error( 'invalid-manifest', 'Manifest did not unfurl properly.' ); 40 43 } 41 44 // Fetch all lesson plan posts for comparison 42 $q = new WP_Query( array(45 $q = new WP_Query( array( 43 46 'post_type' => self::$supported_post_type, 44 47 'post_status' => 'publish', … … 46 49 ) ); 47 50 $existing = $q->posts; 48 $created = 0;49 foreach ( $manifest as $doc ) {51 $created = 0; 52 foreach ( $manifest as $doc ) { 50 53 // Already exists 51 54 if ( wp_filter_object_list( $existing, array( 'post_name' => $doc['slug'] ) ) ) { … … 65 68 if ( isset( $manifest[ $doc['parent'] ] ) ) { 66 69 $parent_doc = $manifest[ $doc['parent'] ]; 67 $parent = self::create_post_from_manifest_doc( $parent_doc );70 $parent = self::create_post_from_manifest_doc( $parent_doc ); 68 71 if ( $parent ) { 69 72 $created++; … … 100 103 'post_name' => sanitize_title_with_dashes( $doc['slug'] ), 101 104 ); 102 $post_id = wp_insert_post( $post_data );105 $post_id = wp_insert_post( $post_data ); 103 106 if ( ! $post_id ) { 104 107 return false; … … 111 114 } 112 115 116 /** 117 * Actions taken on `wporg_learn_markdown_import` event. 118 */ 113 119 public static function action_wporg_learn_markdown_import() { 114 $q = new WP_Query( array(120 $q = new WP_Query( array( 115 121 'post_type' => self::$supported_post_type, 116 122 'post_status' => 'publish', … … 118 124 'posts_per_page' => self::$posts_per_page, 119 125 ) ); 120 $ids = $q->posts;126 $ids = $q->posts; 121 127 $success = 0; 122 foreach ( $ids as $id ) {128 foreach ( $ids as $id ) { 123 129 $ret = self::update_post_from_markdown_source( $id ); 124 130 if ( class_exists( 'WP_CLI' ) ) { … … 155 161 $response = self::update_post_from_markdown_source( $post_id ); 156 162 if ( is_wp_error( $response ) ) { 163 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped 157 164 wp_die( $response->get_error_message() ); 158 165 } … … 164 171 /** 165 172 * Add an input field for specifying Markdown source 166 */173 */ 167 174 public static function action_edit_form_after_title( $post ) { 168 175 if ( $post->post_type !== self::$supported_post_type ) { … … 177 184 placeholder="Enter a URL representing a markdown file to import" 178 185 size="50" /> 179 </label> <?php 180 if ( $markdown_source ) : 181 $update_link = add_query_arg( array( 182 self::$submit_name => 'import', 183 self::$nonce_name => wp_create_nonce( self::$input_name ), 184 ), get_edit_post_link( $post->ID, 'raw' ) ); 185 ?> 186 </label> 187 <?php 188 if ( $markdown_source ) : 189 $update_link = add_query_arg( array( 190 self::$submit_name => 'import', 191 self::$nonce_name => wp_create_nonce( self::$input_name ), 192 ), get_edit_post_link( $post->ID, 'raw' ) ); 193 ?> 186 194 <a class="button button-small button-primary" href="<?php echo esc_url( $update_link ); ?>">Import</a> 187 195 <?php endif; ?> … … 218 226 $schedules['15_minutes'] = array( 219 227 'interval' => 15 * MINUTE_IN_SECONDS, 220 'display' => '15 minutes' 228 'display' => '15 minutes', 221 229 ); 222 230 return $schedules; … … 238 246 //$markdown_source = preg_replace( '#https?://github\.com/([^/]+/[^/]+)/blob/(.+)#', 'https://raw.githubusercontent.com/$1/$2', $markdown_source ); 239 247 $markdown_source = add_query_arg( 'v', time(), $markdown_source ); 240 $response = wp_remote_get( $markdown_source );248 $response = wp_remote_get( $markdown_source ); 241 249 if ( is_wp_error( $response ) ) { 242 250 return $response; … … 251 259 $title = null; 252 260 if ( preg_match( '/^#\s(.+)/', $markdown, $matches ) ) { 253 $title = $matches[1];261 $title = $matches[1]; 254 262 $markdown = preg_replace( '/^#\s(.+)/', '', $markdown ); 255 263 } … … 257 265 // Transform to HTML and save the post 258 266 jetpack_require_lib( 'markdown' ); 259 $parser = new \WPCom_GHF_Markdown_Parser ;260 $html = $parser->transform( $markdown );261 $html = self::replace_markdown_checkboxes( $html );267 $parser = new \WPCom_GHF_Markdown_Parser(); 268 $html = $parser->transform( $markdown ); 269 $html = self::replace_markdown_checkboxes( $html ); 262 270 263 271 $post_data = array( … … 293 301 public static function replace_markdown_checkboxes( $html ) { 294 302 $empty_check_markup = '<input type="checkbox" id="" disabled="" class="task-list-item-checkbox">'; 295 $full_check_markup = '<input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked="">';303 $full_check_markup = '<input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked="">'; 296 304 297 305 // We need to allow inputs with all of our attributes for wp_filter_post_kses(). 298 306 global $allowedposttags; 299 307 300 $allowedposttags['input'] = [ 301 'type' => [], 302 'disabled' => [], 303 'checked' => [], 304 'class' => [], 305 'id' => [], 306 ]; 308 // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited 309 $allowedposttags['input'] = array( 310 'type' => array(), 311 'disabled' => array(), 312 'checked' => array(), 313 'class' => array(), 314 'id' => array(), 315 ); 307 316 308 317 $html = preg_replace( '/\[ \]/', $empty_check_markup, $html );
Note: See TracChangeset
for help on using the changeset viewer.