Making WordPress.org


Ignore:
Timestamp:
08/13/2020 11:43:14 PM (6 years ago)
Author:
coreymckrill
Message:

WordPress.org Learn: Sync with GitHub

https://github.com/WordPress/learn/compare/1accd3db38a60230689bdc157f9e82081d35d163...38e7793fd20434d72ca898988d017ba2009fb677

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  
    99
    1010    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;
    1717
    1818    /**
     
    2828    }
    2929
     30    /**
     31     * Actions taken on `wporg_learn_manifest_import` event.
     32     */
    3033    public static function action_wporg_learn_manifest_import() {
    3134        $response = wp_remote_get( self::$lesson_plan_manifest );
     
    3740        $manifest = json_decode( wp_remote_retrieve_body( $response ), true );
    3841        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.' );
    4043        }
    4144        // Fetch all lesson plan posts for comparison
    42         $q = new WP_Query( array(
     45        $q        = new WP_Query( array(
    4346            'post_type'      => self::$supported_post_type,
    4447            'post_status'    => 'publish',
     
    4649        ) );
    4750        $existing = $q->posts;
    48         $created = 0;
    49         foreach( $manifest as $doc ) {
     51        $created  = 0;
     52        foreach ( $manifest as $doc ) {
    5053            // Already exists
    5154            if ( wp_filter_object_list( $existing, array( 'post_name' => $doc['slug'] ) ) ) {
     
    6568                    if ( isset( $manifest[ $doc['parent'] ] ) ) {
    6669                        $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 );
    6871                        if ( $parent ) {
    6972                            $created++;
     
    100103            'post_name'   => sanitize_title_with_dashes( $doc['slug'] ),
    101104        );
    102         $post_id = wp_insert_post( $post_data );
     105        $post_id   = wp_insert_post( $post_data );
    103106        if ( ! $post_id ) {
    104107            return false;
     
    111114    }
    112115
     116    /**
     117     * Actions taken on `wporg_learn_markdown_import` event.
     118     */
    113119    public static function action_wporg_learn_markdown_import() {
    114         $q = new WP_Query( array(
     120        $q       = new WP_Query( array(
    115121            'post_type'      => self::$supported_post_type,
    116122            'post_status'    => 'publish',
     
    118124            'posts_per_page' => self::$posts_per_page,
    119125        ) );
    120         $ids = $q->posts;
     126        $ids     = $q->posts;
    121127        $success = 0;
    122         foreach( $ids as $id ) {
     128        foreach ( $ids as $id ) {
    123129            $ret = self::update_post_from_markdown_source( $id );
    124130            if ( class_exists( 'WP_CLI' ) ) {
     
    155161        $response = self::update_post_from_markdown_source( $post_id );
    156162        if ( is_wp_error( $response ) ) {
     163            // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
    157164            wp_die( $response->get_error_message() );
    158165        }
     
    164171    /**
    165172     * Add an input field for specifying Markdown source
    166      */
     173     */
    167174    public static function action_edit_form_after_title( $post ) {
    168175        if ( $post->post_type !== self::$supported_post_type ) {
     
    177184            placeholder="Enter a URL representing a markdown file to import"
    178185            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            ?>
    186194                <a class="button button-small button-primary" href="<?php echo esc_url( $update_link ); ?>">Import</a>
    187195            <?php endif; ?>
     
    218226        $schedules['15_minutes'] = array(
    219227            'interval' => 15 * MINUTE_IN_SECONDS,
    220             'display'  => '15 minutes'
     228            'display'  => '15 minutes',
    221229        );
    222230        return $schedules;
     
    238246        //$markdown_source = preg_replace( '#https?://github\.com/([^/]+/[^/]+)/blob/(.+)#', 'https://raw.githubusercontent.com/$1/$2', $markdown_source );
    239247        $markdown_source = add_query_arg( 'v', time(), $markdown_source );
    240         $response = wp_remote_get( $markdown_source );
     248        $response        = wp_remote_get( $markdown_source );
    241249        if ( is_wp_error( $response ) ) {
    242250            return $response;
     
    251259        $title = null;
    252260        if ( preg_match( '/^#\s(.+)/', $markdown, $matches ) ) {
    253             $title = $matches[1];
     261            $title    = $matches[1];
    254262            $markdown = preg_replace( '/^#\s(.+)/', '', $markdown );
    255263        }
     
    257265        // Transform to HTML and save the post
    258266        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 );
    262270
    263271        $post_data = array(
     
    293301    public static function replace_markdown_checkboxes( $html ) {
    294302        $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="">';
    296304
    297305        // We need to allow inputs with all of our attributes for wp_filter_post_kses().
    298306        global $allowedposttags;
    299307
    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        );
    307316
    308317        $html = preg_replace( '/\[ \]/', $empty_check_markup, $html );
Note: See TracChangeset for help on using the changeset viewer.