Making WordPress.org

Changeset 7637


Ignore:
Timestamp:
08/29/2018 11:19:29 AM (8 years ago)
Author:
vedjain
Message:

WordCamp: Added meetup.com API sync to meetup posts.

This is part 1 of adding meetup API sync with meetup admin page. This commit syncs some of the group details like members count, location etc. Currently, information sync for list of co-organizers etc is yet to be implemented.

Location:
sites/trunk/wordcamp.org/public_html/wp-content/plugins/wcpt
Files:
3 edited

Legend:

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

    r7629 r7637  
    1616abstract class Event_Admin {
    1717
     18        public $active_admin_notices;
     19
    1820        /**
    1921         * Event_Admin constructor.
    2022         */
    2123        public function __construct() {
     24
     25                $this->active_admin_notices = array();
    2226
    2327                add_action( 'add_meta_boxes', array( $this, 'metabox' ) );
    … …  
    5155
    5256                add_action( 'transition_post_status', array( $this, 'log_status_changes' ), 10, 3 );
     57
     58                add_filter( 'redirect_post_location', array( $this, 'add_admin_notices_to_redirect_url' ), 10, 2 );
     59
     60                // Admin notices
     61                add_action( 'admin_notices', array( $this, 'print_admin_notices' ) );
    5362        }
    5463
    … …  
    411420
    412421                $this->validate_and_add_note( $post_id );
     422
     423        }
     424
     425        /**
     426         * Add our custom admin notice keys to the redirect URL.
     427         *
     428         * Any member can add a key to $this->active_admin_notices to signify that the corresponding message should
     429         * be shown when the redirect finished. When it does, print_admin_notices() will examine the URL and create
     430         * a notice with the message that corresponds to the key.
     431         *
     432         * @param string $location
     433         * @param int    $post_id
     434         *
     435         * @return string
     436         */
     437        public function add_admin_notices_to_redirect_url( $location, $post_id ) {
     438                if ( $this->active_admin_notices ) {
     439                        $location = add_query_arg( 'wcpt_messages', implode( ',', $this->active_admin_notices ), $location );
     440                }
     441
     442                // Don't show conflicting messages like 'Post submitted.'
     443                if ( in_array( 1, $this->active_admin_notices ) && false !== strpos( $location, 'message=8' ) ) {
     444                        $location = remove_query_arg( 'message', $location );
     445                }
     446
     447                return $location;
     448        }
     449
     450        /**
     451         * Return admin notices for messages that were passed in the URL.
     452         */
     453        abstract public function get_admin_notices();
     454
     455        /**
     456         * Create admin notices for messages that were passed in the URL.
     457         *
     458         * Any member can add a key to $this->active_admin_notices to signify that the corresponding message should
     459         * be shown when the redirect finished. add_admin_notices_to_redirect_url() adds those keys to the redirect
     460         * url, and this function examines the URL and create a notice with the message that corresponds to the key.
     461         *
     462         * $notices[key]['type'] should equal 'error' or 'updated'.
     463         */
     464        public function print_admin_notices() {
     465
     466                global $post;
     467                $screen = get_current_screen();
     468
     469
     470                if ( empty( $post->post_type ) || $this->get_event_type() != $post->post_type || 'post' !== $screen->base ) {
     471                        return;
     472                }
     473
     474                $notices = $this->get_admin_notices();
     475
     476                if ( ! empty( $_REQUEST['wcpt_messages'] ) ) {
     477                        $active_notices = explode( ',', $_REQUEST['wcpt_messages'] );
     478
     479                        foreach ( $active_notices as $notice_key ) {
     480                                if ( isset( $notices[ $notice_key ] ) ) {
     481                                        ?>
     482
     483                                        <div class="<?php echo esc_attr( $notices[ $notice_key ]['type'] ); ?>">
     484                                                <p><?php echo wp_kses( $notices[ $notice_key ]['notice'], wp_kses_allowed_html( 'post' ) ); ?></p>
     485                                        </div>
     486
     487                                        <?php
     488                                }
     489                        }
     490                }
    413491
    414492        }
  • sites/trunk/wordcamp.org/public_html/wp-content/plugins/wcpt/wcpt-meetup/class-meetup-admin.php

    r7635 r7637  
    2222                public function __construct() {
    2323                        parent::__construct();
     24
     25                        add_action( 'wcpt_metabox_save_done', array( $this, 'maybe_update_meetup_data' ) );
    2426                }
    2527
    … …  
    9395
    9496                /**
    95                  * TODO: Implement
    96                  *
     97                 * Return read only meta fields.
     98                 */
     99                public static function get_protected_fields() {
     100                        return array(
     101
     102                                // These fields are update by meetup API and will be overwritten even if manually changed.
     103                                'Meetup Location (From meetup.com)',
     104                                'Meetup members count',
     105                                'Meetup group created on',
     106                                'Number of past meetups',
     107                                'Last meetup on',
     108                                'Last meetup RSVP count',
     109                        );
     110                }
     111
     112                /**
     113                 * Checks if a field is read only.
    97114                 * @param string $key Name of the field.
    98115                 *
    … …  
    100117                 */
    101118                public static function is_protected_field( $key ) {
    102                         return false;
     119                        return in_array( $key, self::get_protected_fields() );
    103120                }
    104121
    … …  
    189206                        );
    190207
     208                        add_meta_box(
     209                                'wcpt_meetup_metadata',
     210                                __( 'Meetup.com API sync', 'wcpt' ),
     211                                array( $this, 'wcpt_meetup_sync' ),
     212                                Meetup_Application::POST_TYPE,
     213                                'side',
     214                                'high'
     215                        );
     216
    191217                }
    192218
    … …  
    247273                        global $post_id;
    248274
    249                         self::display_meta_boxes( array(), $meta_keys, array(), $post_id, array() );
     275                        self::display_meta_boxes( array(), $meta_keys, array(), $post_id, self::get_protected_fields() );
     276                }
     277
     278                public function wcpt_meetup_sync() {
     279                        global $post_id;
     280                        $meta_key = 'Last meetup.com API sync';
     281                        $last_synced_on = get_post_meta( $post_id, $meta_key, true );
     282                        $element_name = 'sync_with_meetup_api';
     283
     284                        if ( empty( $last_synced_on ) ) {
     285                                $last_synced_on = 'Never';
     286                        } else {
     287                                $last_synced_on = date( "Y-m-d",  substr( $last_synced_on, 0, 10 ) );
     288                        }
     289                        ?>
     290                        <div class="wcb submitbox">
     291                                <div class="misc-pub-section">
     292                                        <label>Last sync: <?php echo $last_synced_on ?></label>
     293                                </div>
     294                                <div class="misc-pub-section">
     295                                        <label>
     296                                                <input type="checkbox" name="<?php echo $element_name ?>" >
     297                                                Sync Now
     298                                        </label>
     299                                </div>
     300                        </div>
     301                        <?php
     302                }
     303                /**
     304                 * Updates meetup fields using meetup.com API only if Sync now checkbox is checked.
     305                 *
     306                 * @param int   $post_id
     307                 * @param array $original_meta_values
     308                 */
     309                public function maybe_update_meetup_data( $post_id ){
     310                        if ( $this->get_event_type() !== get_post_type() ) {
     311                                return;
     312                        }
     313
     314                        $should_sync = $_POST[ 'sync_with_meetup_api' ];
     315                        if ( ! $should_sync ) {
     316                                return;
     317                        }
     318
     319                        $result = self::update_meetup_data( $post_id );
     320
     321                        if ( is_wp_error( $result ) ) {
     322                                $this->active_admin_notices[] = $result->get_error_code();
     323                                return;
     324                        }
     325
     326                        update_post_meta( $post_id, 'Last meetup.com API sync', time() );
     327
     328                }
     329
     330                /**
     331                 * Update meetup fields using meetup.com API
     332                 *
     333                 * @param $post_id
     334                 *
     335                 * @return array|WP_Error
     336                 */
     337                public static function update_meetup_data( $post_id ) {
     338
     339                        $meetup_url = get_post_meta( $post_id, 'Meetup URL', true );
     340
     341                        $parsed_url = wp_parse_url( $meetup_url, -1 );
     342
     343                        if( ! $parsed_url ) {
     344                                return new WP_Error( 'invalid-url', __('Provided Meetup URL is not a valid URL.', 'wordcamporg' ) );
     345                        }
     346                        $url_path_segments = explode( '/', rtrim( $parsed_url['path'], '/' ) );
     347                        $slug = array_pop( $url_path_segments );
     348                        $mtp_client = new \WordCamp\Utilities\Meetup_Client();
     349
     350                        $group_details = $mtp_client->get_group_details(
     351                                $slug,
     352                                array(
     353                                        'fields' => 'past_event_count,last_event',
     354                                )
     355                        );
     356
     357                        if ( is_wp_error( $group_details ) ) {
     358                                return $group_details;
     359                        }
     360
     361                        if ( isset( $group_details['errors'] ) ) {
     362                                return new WP_Error( 'invalid-response', __( 'Received invalid response from meetup api.', 'wordcamporg' ) );
     363                        }
     364
     365                        update_post_meta( $post_id, 'Meetup Location (From meetup.com)', $group_details['localized_location'] );
     366                        update_post_meta( $post_id, 'Meetup members count', $group_details['members'] );
     367                        update_post_meta( $post_id, 'Meetup group created on', $group_details['created'] / 1000 );
     368                        update_post_meta( $post_id, 'Number of past meetups', $group_details['past_event_count'] );
     369
     370                        if ( isset( $group_details['last_event'] ) && is_array( $group_details['last_event'] ) ) {
     371                                update_post_meta( $post_id, 'Last meetup on', $group_details['last_event']['time'] / 1000 );
     372                                update_post_meta( $post_id, 'Last meetup RSVP count', $group_details['last_event']['yes_rsvp_count'] );
     373                        }
     374                }
     375
     376                /**
     377                 * List of admin notices.
     378                 *
     379                 * @return array
     380                 */
     381                public function get_admin_notices() {
     382
     383                        return array(
     384                                'invalid-url'        => array(
     385                                        'type'   => 'notice',
     386                                        'notice' => __( 'Invalid meetup.com URL. Meetup fields are not updated.', 'wordcamporg' ),
     387                                ),
     388                                'invalid-response'   => array(
     389                                        'type'   => 'notice',
     390                                        'notice' => __( 'Received invalid response from Meetup API. Please make sure Meetup URL is correct, or try again after some time.', 'wordcamporg' )
     391                                ),
     392                                'group_error'        => array(
     393                                        'type'   => 'notice',
     394                                        'notice' => __( 'Received invalid response from Meetup API. Please make sure Meetup URL is correct, or try again after some time.', 'wordcamporg' )
     395                                ),
     396                                'http_response_code' => array(
     397                                        'type'   => 'notice',
     398                                        'notice' => __( 'Received invalid response code from Meetup API. Please make sure Meetup URL is correct, or try again after some time.', 'wordcamporg' )
     399                                ),
     400                        );
     401
    250402                }
    251403
    … …  
    260412
    261413                        $info_keys = array(
    262                                 'Meetup URL'      => 'text',
    263                                 'HelpScout link'  => 'text',
    264                                 'Meetup Location' => 'text',
     414                                'Meetup URL'                        => 'text',
     415                                'Meetup Location (From meetup.com)' => 'text',
     416                                'Meetup members count'              => 'text',
     417                                'Meetup group created on'           => 'date',
     418                                'Number of past meetups'            => 'text',
     419                                'Last meetup on'                    => 'date',
     420                                'Last meetup RSVP count'            => 'text',
     421                                'HelpScout link'                    => 'text',
     422                                'Meetup Location'                   => 'text',
    265423                        );
    266424
    … …  
    291449                        );
    292450
     451                        $metadata_keys = array(
     452                                'Last meetup.com API sync' => 'date',
     453                        );
     454
    293455                        $swag_keys = array(
    294456                                'Swag notes' => 'textarea',
    … …  
    307469                                case 'swag':
    308470                                        $data = $swag_keys;
     471                                        break;
     472                                case 'metadata':
     473                                        $data = $metadata_keys;
    309474                                        break;
    310475                                case 'all':
    … …  
    314479                                                $application_keys,
    315480                                                $organizer_keys,
    316                                                 $swag_keys
     481                                                $swag_keys,
     482                                                $metadata_keys
    317483                                        );
    318484                        }
  • sites/trunk/wordcamp.org/public_html/wp-content/plugins/wcpt/wcpt-wordcamp/wordcamp-admin.php

    r7629 r7637  
    1717         */
    1818        class WordCamp_Admin extends Event_Admin {
    19                 protected $active_admin_notices;
    2019
    2120                /**
    … …  
    2524
    2625                        parent::__construct();
    27 
    28                         $this->active_admin_notices = array();
    2926
    3027                        // Add some general styling to the admin area
    … …  
    4744                                ), 11, 2
    4845                        ); // after enforce_post_status
    49 
    50                         // Admin notices
    51                         add_action( 'admin_notices', array( $this, 'print_admin_notices' ) );
    52 
    53                         add_filter( 'redirect_post_location', array( $this, 'add_admin_notices_to_redirect_url' ), 10, 2 );
    5446
    5547                        // Cron jobs
    … …  
    863855
    864856                /**
    865                  * Add our custom admin notice keys to the redirect URL.
    866                  *
    867                  * Any member can add a key to $this->active_admin_notices to signify that the corresponding message should
    868                  * be shown when the redirect finished. When it does, print_admin_notices() will examine the URL and create
    869                  * a notice with the message that corresponds to the key.
    870                  *
    871                  * @param $location
    872                  * @param $post_id
    873                  * @return string
    874                  */
    875                 public function add_admin_notices_to_redirect_url( $location, $post_id ) {
    876                         if ( $this->active_admin_notices ) {
    877                                 $location = add_query_arg( 'wcpt_messages', implode( ',', $this->active_admin_notices ), $location );
    878                         }
    879 
    880                         // Don't show conflicting messages like 'Post submitted.'
    881                         if ( in_array( 1, $this->active_admin_notices ) && false !== strpos( $location, 'message=8' ) ) {
    882                                 $location = remove_query_arg( 'message', $location );
    883                         }
    884 
    885                         return $location;
    886                 }
    887 
    888                 /**
    889                  * Create admin notices for messages that were passed in the URL.
    890                  *
    891                  * Any member can add a key to $this->active_admin_notices to signify that the corresponding message should
    892                  * be shown when the redirect finished. add_admin_notices_to_redirect_url() adds those keys to the redirect
    893                  * url, and this function examines the URL and create a notice with the message that corresponds to the key.
    894                  *
    895                  * $notices[key]['type'] should equal 'error' or 'updated'.
    896                  */
    897                 public function print_admin_notices() {
     857                 * Return admin notices for messages that were passed in the URL.
     858                 */
     859                public function get_admin_notices() {
     860
    898861                        global $post;
     862
    899863                        $screen = get_current_screen();
    900864
    901                         if ( empty( $post->post_type ) || WCPT_POST_TYPE_ID != $post->post_type || 'post' !== $screen->base ) {
    902                                 return;
    903                         }
    904 
    905                         $notices = array(
     865
     866                        if ( empty( $post->post_type ) || $this->get_event_type() != $post->post_type || 'post' !== $screen->base ) {
     867                                return array();
     868                        }
     869
     870                        // Show this error permanently, not just after updating.
     871                        if ( ! empty( $post->{'Physical Address'} ) && empty( get_post_meta( $post->ID, '_venue_coordinates', true ) ) ) {
     872                                $_REQUEST['wcpt_messages'] = empty( $_REQUEST['wcpt_messages'] ) ? '4' : $_REQUEST['wcpt_messages'] . ',4';
     873                        }
     874
     875                        return array(
    906876                                1 => array(
    907877                                        'type'   => 'error',
    … …  
    926896                        );
    927897
    928                         // Show this error permenantly, not just after updating.
    929                         if ( ! empty( $post->{'Physical Address'} ) && empty( get_post_meta( $post->ID, '_venue_coordinates', true ) ) ) {
    930                                 $_REQUEST['wcpt_messages'] = empty( $_REQUEST['wcpt_messages'] ) ? '4' : $_REQUEST['wcpt_messages'] . ',4';
    931                         }
    932 
    933                         if ( ! empty( $_REQUEST['wcpt_messages'] ) ) {
    934                                 $active_notices = explode( ',', $_REQUEST['wcpt_messages'] );
    935 
    936                                 foreach ( $active_notices as $notice_key ) {
    937                                         if ( isset( $notices[ $notice_key ] ) ) {
    938                                                 ?>
    939 
    940                                         <div class="<?php echo esc_attr( $notices[ $notice_key ]['type'] ); ?>">
    941                                                 <p><?php echo wp_kses( $notices[ $notice_key ]['notice'], wp_kses_allowed_html( 'post' ) ); ?></p>
    942                                         </div>
    943 
    944                                                 <?php
    945                                         }
    946                                 }
    947                         }
    948898                }
    949899
Note: See TracChangeset for help on using the changeset viewer.