WordPress.org

Making WordPress.org

Changeset 654


Ignore:
Timestamp:
06/03/2014 12:56:53 AM (6 years ago)
Author:
iandunn
Message:

WordCamp Post Type: Don't publish unless all required data is entered.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordcamp.org/public_html/wp-content/plugins/wcpt/wcpt-wordcamp/wordcamp-admin.php

    r652 r654  
    1212 */
    1313class WordCamp_Admin {
     14    protected $active_admin_notices;
    1415
    1516    /**
     
    1920     */
    2021    function WordCamp_Admin () {
     22        $this->active_admin_notices = array();
    2123
    2224        // Add some general styling to the admin area
     
    3335        add_action( 'admin_menu', array( $this, 'metabox' ) );
    3436        add_action( 'save_post', array( $this, 'metabox_save' ) );
    35        
     37
    3638        // Scripts and CSS
    3739        add_action( 'admin_enqueue_scripts', array( $this, 'admin_scripts' ) );
     
    4244        add_action( 'transition_post_status', array( $this, 'add_organizer_to_central' ), 10, 3 );
    4345        add_action( 'transition_post_status', array( $this, 'notify_mes_sponsors_when_wordcamp_scheduled' ), 10, 3 );
     46        add_action( 'wp_insert_post_data',    array( $this, 'require_complete_meta_to_publish_wordcamp' ), 10, 2 );
     47
     48        // Admin notices
     49        add_action( 'admin_notices',          array( $this, 'print_admin_notices' ) );
     50        add_filter( 'redirect_post_location', array( $this, 'add_admin_notices_to_redirect_url' ), 10, 2 );
    4451    }
    4552
     
    546553        }
    547554    }
     555
     556    /**
     557     * Prevent WordCamp posts from being published until all the required fields are completed.
     558     *
     559     * @param array $post_data
     560     * @param array $post_data_raw
     561     * @return array
     562     */
     563    public function require_complete_meta_to_publish_wordcamp( $post_data, $post_data_raw ) {
     564        // The ID of the last site that was created before this rule went into effect, so that we don't apply the rule retroactively.
     565        $min_site_id = apply_filters( 'wcpt_require_complete_meta_min_site_id', '2416297' );
     566
     567        $required_fields = array(
     568            // WordCamp
     569            'Start Date (YYYY-mm-dd)',
     570            'Location',
     571            'URL',
     572            'E-mail Address',
     573            'Twitter',
     574            'WordCamp Hashtag',
     575            'Number of Anticipated Attendees',
     576            'Multi-Event Sponsor Region',
     577
     578            // Organizing Team
     579            'Organizer Name',
     580            'WordPress.org Username',
     581            'Email Address',
     582            'Mailing Address',
     583            'Sponsor Wrangler Name',
     584            'Sponsor Wrangler E-mail Address',
     585            'Budget Wrangler Name',
     586            'Budget Wrangler E-mail Address',
     587
     588            // Venue
     589            'Venue Name',
     590            'Physical Address',
     591            'Website URL',
     592        );
     593
     594        if ( WCPT_POST_TYPE_ID == $post_data['post_type'] && 'publish' == $post_data['post_status'] && absint( $_POST['post_ID'] ) > $min_site_id ) {
     595            foreach( $required_fields as $field ) {
     596                if ( empty( $_POST[ wcpt_key_to_str( $field, 'wcpt_' ) ] ) ) {
     597                    $post_data['post_status']     = 'pending';
     598                    $this->active_admin_notices[] = 1;
     599                    break;
     600                }
     601            }
     602        }
     603
     604        return $post_data;
     605    }
     606
     607    /**
     608     * Add our custom admin notice keys to the redirect URL.
     609     *
     610     * Any member can add a key to $this->active_admin_notices to signify that the corresponding message should
     611     * be shown when the redirect finished. When it does, print_admin_notices() will examine the URL and create
     612     * a notice with the message that corresponds to the key.
     613     *
     614     * @param $location
     615     * @param $post_id
     616     * @return string
     617     */
     618    public function add_admin_notices_to_redirect_url( $location, $post_id ) {
     619        if ( $this->active_admin_notices ) {
     620            $location = add_query_arg( 'wcpt_messages', implode( ',', $this->active_admin_notices ), $location );
     621        }
     622
     623        // Don't show conflicting messages like 'Post submitted.'
     624        if ( in_array( 1, $this->active_admin_notices ) && false !== strpos( $location, 'message=8' ) ) {
     625            $location = remove_query_arg( 'message', $location );
     626        }
     627
     628        return $location;
     629    }
     630
     631    /**
     632     * Create admin notices for messages that were passed in the URL.
     633     *
     634     * Any member can add a key to $this->active_admin_notices to signify that the corresponding message should
     635     * be shown when the redirect finished. add_admin_notices_to_redirect_url() adds those keys to the redirect
     636     * url, and this function examines the URL and create a notice with the message that corresponds to the key.
     637     *
     638     * $notices[key]['type'] should equal 'error' or 'updated'.
     639     */
     640    public function print_admin_notices() {
     641        global $post;
     642
     643        if ( empty( $post->post_type ) || WCPT_POST_TYPE_ID != $post->post_type ) {
     644            return;
     645        }
     646
     647        $notices = array(
     648            1 => array(
     649                'type'   => 'error',
     650                'notice' => __( 'This WordCamp cannot be published until all of its required metadata is filled in.', 'wordcamporg' ),
     651            ),
     652        );
     653
     654        if ( ! empty( $_REQUEST['wcpt_messages'] ) ) {
     655            $active_notices = explode( ',', $_REQUEST['wcpt_messages'] );
     656
     657            foreach ( $active_notices as $notice_key ) {
     658                if ( isset( $notices[ $notice_key ] ) ) {
     659                    ?>
     660
     661                    <div class="<?php echo esc_attr( $notices[ $notice_key ]['type'] ); ?>">
     662                        <p><?php echo wp_kses( $notices[ $notice_key ]['notice'], wp_kses_allowed_html( 'post' ) ); ?></p>
     663                    </div>
     664
     665                    <?php
     666                }
     667            }
     668        }
     669    }
    548670}
    549671endif; // class_exists check
Note: See TracChangeset for help on using the changeset viewer.