Making WordPress.org

Ticket #986: 986.3.diff

File 986.3.diff, 9.1 KB (added by iandunn, 11 years ago)

Various minor tweaks to 986.2.diff

  • anon-upload-template.php

     
    6161
    6262        .video-upload p {
    6363                margin: 16px 0;
     64                overflow: auto;
    6465        }
    6566
    6667        .video-upload h3 {
     
    214215                case 13:
    215216                        $message = "Error: please leave the first field empty. (It helps us know you're not a spammer.)";
    216217                        break;
     218                case 14:
     219                        $message = "Error: please enter a valid WordPress.org username for the producer, or leave the field empty.";
     220                        break;
    217221        }
    218222        $message = '<div class="error"><p>' . $message . '</p></div>';
    219223} elseif ( !empty($_REQUEST['success']) ) {
     
    314318        </div>
    315319
    316320        <p>
    317                 <label for="wptv_video_producer"><?php esc_html_e( 'Video producer' ); ?></label>
    318                 <input type="text" id="wptv_video_producer" name="wptv_video_producer" value="" />
     321                <label for="wptv_producer_username"><?php esc_html_e( 'Producer WordPress.org Username' ); ?></label>
     322                <input type="text" id="wptv_producer_username" name="wptv_producer_username" value="" />
    319323        </p>
    320324        <p>
    321325                <label for="wptv_speakers"><?php esc_html_e( 'Speakers' ); ?></label>
  • functions.php

     
    164164                global $post;
    165165
    166166                $slides_url = get_post_meta( $post->ID, '_wptv_slides_url', true );
     167                $producer_username = get_post_meta( $post->ID, '_wptv_producer_username', true );
    167168                wp_nonce_field( 'edit-video-info', 'video_info_metabox_nonce' );
    168169
    169170                ?>
     
    173174                        <input type="text" class="widefat" id="wptv-slides-url" name="_wptv_slides_url" value="<?php echo esc_url( $slides_url ); ?>" />
    174175                </p>
    175176
     177                <p>
     178                        <label for="wptv-producer-username">Producer's WordPress.org Username</label>
     179                        <input type="text" class="widefat" id="wptv-producer-username" name="_wptv_producer_username" value="<?php echo esc_attr( $producer_username ); ?>" />
     180                </p>
    176181                <?php
    177182        }
    178183
     
    198203                } else {
    199204                        delete_post_meta( $post_id, '_wptv_slides_url' );
    200205                }
     206
     207                $producer_username = sanitize_text_field( $_POST['_wptv_producer_username'] );
     208
     209                if ( wporg_username_exists( $producer_username ) ) {
     210                        update_post_meta( $post_id, '_wptv_producer_username', $producer_username );
     211                } else {
     212                        delete_post_meta( $post_id, '_wptv_producer_username' );
     213                }
    201214        }
    202215
    203216        /**
     
    829842        return $excerpt;
    830843}
    831844add_filter( 'get_the_excerpt', 'wptv_excerpt_slides' );
     845
     846/**
     847 * Checks if the given username exists on WordPress.org
     848 *
     849 * grav-redirect.php will redirect to a Gravatar image URL. If the WordPress.org username exists, the `d` parameter
     850 * will be 'retro', and if it doesn't it'll be 'mm'.
     851 *
     852 * @param string $username
     853 *
     854 * @return bool
     855 */
     856function wporg_username_exists( $username ) {
     857        $username_exists = false;
     858        $validator_url   = add_query_arg( 'user', $username, 'https://wordpress.org/grav-redirect.php' );
     859        $response        = wp_remote_retrieve_headers( wp_remote_get( $validator_url, array( 'redirection' => 0 ) ) );
     860
     861        if ( array_key_exists( 'location', $response ) ) {
     862                if ( false === strpos( $response['location'], 'd=mm' ) ) {
     863                        $username_exists = true;
     864                }
     865        }
     866
     867        return $username_exists;
     868}
     869
     870// todo temp
     871function test_wporg_username_exists() {
     872        $valid_usernames   = array( 'iandunn', 'iandunn-CapitalTest', 'Ian Dunn Space Capital Test', 'kovshenin', '.EXTRATER.', '_Skyn3t_' );
     873        $invalid_usernames = array( '', 252342, 'sfjj2fasdfaef', '#@(@%%@#&*(*&(', '.;.;.;' );
     874
     875        foreach ( $valid_usernames as $username ) {
     876                printf( "<p>Valid username %s was marked as %s</p>", $username, wporg_username_exists( $username ) ? 'valid' : 'invalid' );
     877        }
     878
     879        foreach ( $invalid_usernames as $username ) {
     880                printf( "<p>Invalid username %s was marked as %s</p>", $username, wporg_username_exists( $username ) ? 'valid' : 'invalid' );
     881        }
     882}
     883//test_wporg_username_exists();
  • plugins/wordpresstv-anon-upload/anon-upload.php

     
    4747        function validate() {
    4848                $text_fields = array(
    4949                        'wptv_video_title',
    50                         'wptv_video_producer',
     50                        'wptv_producer_username',
    5151                        'wptv_speakers',
    5252                        'wptv_event',
    5353                        'wptv_slides_url'
     
    6262                        return $this->error( 13 );
    6363                }
    6464
     65                if ( empty( $_POST['wptv_producer_username'] ) || ! wporg_username_exists( $_POST['wptv_producer_username'] ) ) {
     66                        return $this->error( 14 );
     67                }
     68
    6569                if ( ! is_user_logged_in() ) {
    6670                        if ( empty( $_POST['wptv_uploaded_by'] ) ) {
    6771                                return $this->error( 10 );
     
    259263                }
    260264
    261265                $video_title    = $this->sanitize_text( $_posted['wptv_video_title'] );
    262                 $video_producer = $this->sanitize_text( $_posted['wptv_video_producer'] );
     266                $producer_username = $this->sanitize_text( $_posted['wptv_producer_username'] );
    263267                $speakers       = $this->sanitize_text( $_posted['wptv_speakers'] );
    264268                $event          = $this->sanitize_text( $_posted['wptv_event'] );
    265269                $description    = $this->sanitize_text( $_posted['wptv_video_description'], false );
     
    266270                $language       = $this->sanitize_text( $_posted['wptv_language'] );
    267271                $slides         = $this->sanitize_text( $_posted['wptv_slides_url'] );
    268272                $ip             = $_SERVER['REMOTE_ADDR'];
     273                // todo re-align after this commit
    269274
    270275                $categories = '';
    271276                if ( ! empty( $_posted['post_category'] ) && is_array( $_posted['post_category'] ) ) {
     
    282287                        'submitted_by'    => $anon_author,
    283288                        'submitted_email' => $anon_author_email,
    284289                        'title'           => $video_title,
    285                         'producer'        => $video_producer,
     290                        'producer_username' => $producer_username,
    286291                        'speakers'        => $speakers,
    287292                        'event'           => $event,
    288293                        'language'        => $language,
     
    290295                        'description'     => $description,
    291296                        'slides'          => $slides,
    292297                        'ip'              => $ip,
     298                        // todo re-align after this commit
    293299                );
    294300
    295301                $post_meta['video_guid'] = $video_data->guid;
     
    324330                $embed_args['blog_id'] = get_current_blog_id();
    325331                $embed_args['post_id'] = $meta['attachment_id'];
    326332
    327                 // Add slides index to meta (necessary for posts that were uploaded before the field was added)
    328                 if ( ! array_key_exists( 'slides', $meta ) ) {
    329                         $meta['slides'] = '';
     333                // Add missing indexes to meta (necessary for posts that were uploaded before the fields were added)
     334                $new_fields = array( 'slides', 'producer_username' );       // todo username should be taxonomy like producer name, rather than meta
     335                foreach ( $new_fields as $field ) {
     336                        if ( ! array_key_exists( $field, $meta ) ) {
     337                                $meta[ $field ] = '';
     338                        }
    330339                }
    331340
    332341                ?>
     
    460469                                        </div>
    461470
    462471                                        <div class="row">
    463                                                 <p class="label">Producer:</p>
     472                                                <p class="label">Producer WordPress.org Username:</p>
     473
    464474                                                <p class="data">
    465                                                         <input type="text" value="<?php echo esc_attr( $meta['producer'] ); ?>"/>
    466                                                         <a class="button-secondary anon-approve" href="#new-tag-producer">Approve</a>
     475                                                        <input type="text" value="<?php echo esc_attr( $meta['producer_username'] ); ?>"/>
     476                                                        <a class="button-secondary anon-approve" href="#wptv-producer-username">Approve</a>
    467477                                                </p>
    468478                                        </div>
    469479
     
    528538                                                if (id.indexOf('#new-tag-') != -1) {
    529539                                                        el.val(target.siblings('input[type="text"]').val());
    530540                                                        el.siblings('.tagadd').click();
     541                                                } else if ('#title' == id || '#wptv-producer-username' == id) {
     542                                                        el.val(target.siblings('input[type="text"]').val());
    531543                                                } else if ('#title' == id  || '#wptv-slides-url' == id) {
    532544                                                        el.val(target.siblings('input[type="text"]').val());
    533545                                                } else if (id == '#excerpt') {
  • sidebar-single.php

     
    8181                                                printf( '<a href="%s">Subtitle this video &rarr;</a>', esc_url( add_query_arg( 'video', $video->post_id, home_url( 'subtitle/' ) ) ) );
    8282                                        }
    8383                                }
     84
     85                        /*
     86                         * Credit video producer with link to their WordPress.org profile
     87                         *
     88                         * In most cases we'll either have the producer name, or the username, but not both.
     89                         */
     90                        $producer_username = get_post_meta( get_the_ID(), '_wptv_producer_username', true );
     91
     92                        if ( $producer_name = get_the_terms( get_the_ID(), 'producer' ) ) {
     93                                if ( ! is_wp_error( $producer_name ) && ! empty( $producer_name[0]->name ) ) {
     94                                        $producer_name = $producer_name[0]->name;
     95                                } else {
     96                                        $producer_name = false;
     97                                }
     98                        }
     99
    84100                        ?>
     101
     102                        <?php if ( $producer_name || $producer_username ) : ?>
     103                                <h5>Producer</h5>
     104
     105                                <div class="video-producer">
     106                                        <?php if ( $producer_username ) : ?>
     107
     108                                                <a href="<?php echo esc_url( 'https://profiles.wordpress.org/' . rawurlencode( $producer_username ) ); ?>">
     109                                                        <?php echo esc_html( $producer_username ); ?>
     110                                                </a>
     111
     112                                        <?php elseif ( $producer_name ) : ?>
     113
     114                                                <?php echo esc_html( $producer_name ); ?>
     115
     116                                        <?php endif; ?>
     117                                </div>
     118                        <?php endif; ?>
     119
    85120</div><!-- .secondary-content -->