From 3b413de58508bb807866d74971cb7f8cfdc8a17e Mon Sep 17 00:00:00 2001 From: Jared Cobb Date: Sat, 22 Aug 2015 14:44:34 -0600 Subject: [PATCH] Add support for importing group images as featured images --- admin/class-ccb-core-cpts.php | 4 - admin/class-ccb-core-settings.php | 12 +++ admin/class-ccb-core-sync.php | 134 ++++++++++++++++++++++++++++- includes/class-ccb-core-plugin.php | 2 + 4 files changed, 146 insertions(+), 6 deletions(-) diff --git a/admin/class-ccb-core-cpts.php b/admin/class-ccb-core-cpts.php index 59e6efe..a7a0cab 100644 --- a/admin/class-ccb-core-cpts.php +++ b/admin/class-ccb-core-cpts.php @@ -235,10 +235,6 @@ protected function register_calendar() { */ public static function get_groups_custom_fields_map() { return array( - 'group_image_url' => array( - 'api_mapping' => 'image', - 'data_type' => 'string', - ), 'group_main_leader' => array( 'api_mapping' => 'main_leader', 'data_type' => 'object', diff --git a/admin/class-ccb-core-settings.php b/admin/class-ccb-core-settings.php index f1e4764..b8cd58a 100644 --- a/admin/class-ccb-core-settings.php +++ b/admin/class-ccb-core-settings.php @@ -222,6 +222,18 @@ public function get_settings_definitions() { 'field_attributes' => array( 'data-requires' => '{"groups-enabled":1}' ), 'field_tooltip' => 'This is typically where your theme will display all the groups. WordPress calls this a "slug".', ), + 'groups-import-images' => array( + 'field_title' => 'Also Import Group Images?', + 'field_render_function' => 'render_radio', + 'field_options' => array( + 'yes' => 'Yes', + 'no' => 'No' + ), + 'field_validation' => '', + 'field_default' => 'no', + 'field_attributes' => array( 'data-requires' => '{"groups-enabled":1}' ), + 'field_tooltip' => "This will download the CCB Group Image and attach it as a Featured Image.
If you don't need group images, then disabling this feature will speed up the synchronization.", + ), 'groups-advanced' => array( 'field_title' => 'Enable Advanced Settings (Optional)', 'field_render_function' => 'render_switch', diff --git a/admin/class-ccb-core-sync.php b/admin/class-ccb-core-sync.php index a23fae9..988e824 100644 --- a/admin/class-ccb-core-sync.php +++ b/admin/class-ccb-core-sync.php @@ -83,6 +83,15 @@ class CCB_Core_Sync extends CCB_Core_Plugin { */ protected $valid_services; + /** + * Whether or not to additionally import group images + * + * @since 0.9.5 + * @access protected + * @var array $valid_services + */ + protected $import_group_images; + /** * Initialize the class and set its properties. * @@ -102,6 +111,13 @@ public function __construct() { $this->enabled_apis['group_profiles'] = true; + if ( isset( $settings['groups-import-images'] ) && $settings['groups-import-images'] == 'yes' ) { + $this->import_group_images = true; + } + else { + $this->import_group_images = false; + } + } if ( isset( $settings['calendar-enabled'] ) && $settings['calendar-enabled'] == 1 ) { @@ -214,9 +230,10 @@ protected function call_ccb_api( $services = array() ) { set_time_limit(600); $full_response = array(); - // for debugging purposes, set a constant and serialize and array like so: + // for debugging purposes, set a constant and serialize an array like so: // define( 'RESPONSE_FILE', serialize( array( 'filename' => 'some_file.xml', 'service_name' => 'group_profiles' ) ) ); // file must be located in the /uploads/ccb-core/ folder + // this will prevent a real api call and will use an xml file if ( WP_DEBUG == true && defined( 'RESPONSE_FILE' ) ) { $service = unserialize( RESPONSE_FILE ); @@ -303,7 +320,7 @@ protected function call_ccb_api( $services = array() ) { $files = preg_grep( '/' . $now->format( 'Y-m-d' ) . '/', glob( trailingslashit( trailingslashit( $upload_dir['basedir'] ) . $this->plugin_name ) . '*' ), PREG_GREP_INVERT ); foreach ( $files as $file ) { if ( is_file( $file ) ) { - unlink( $file ); + @unlink( $file ); } } @@ -548,16 +565,27 @@ protected function import_cpts( $full_response ) { // delete existing custom posts $custom_posts = get_posts( array( 'post_type' => $this->plugin_name . '-groups', 'posts_per_page' => -1 ) ); foreach( $custom_posts as $custom_post ) { + + // delete the post thumbnail if it exists before deleting the post + $thumbnail_id = get_post_thumbnail_id( $custom_post->ID ); + if ( $thumbnail_id ) { + wp_delete_attachment( $thumbnail_id, true ); + } + wp_delete_post( $custom_post->ID, true); } // commit the deletes now $wpdb->query( 'COMMIT;' ); + // keep track of whether or not a default image has already been imported + $default_attachment = 0; + foreach ( $full_response['group_profiles']->response->groups->group as $group ) { // only allow publicly listed and active groups to be imported if ( $group->inactive == 'false' && $group->public_search_listed == 'true' ) { + $group_id = 0; foreach( $group->attributes() as $key => $value ) { if ( $key == 'id' ) { @@ -592,6 +620,36 @@ protected function import_cpts( $full_response ) { } } + // download and attach the group image as the featured image + if ( isset( $group->image ) && $this->import_group_images == true ) { + + $group_image_url = esc_url_raw( $group->image ); + + if ( ! empty( $group_image_url ) ) { + + // handle default images + if ( strpos( $group_image_url, 'default' ) ) { + if ( ! $default_attachment ) { + $attachment_result = $this->create_media_image( 'default', 0, $group_image_url ); + if ( $attachment_result ) { + $default_attachment = $attachment_result; + set_post_thumbnail( $post_id, $default_attachment ); + } + } + else { + set_post_thumbnail( $post_id, $default_attachment ); + } + } + else { + $attachment_result = $this->create_media_image( $group->name, $post_id, $group_image_url ); + if ( $attachment_result ) { + set_post_thumbnail( $post_id, $attachment_result ); + } + } + } + + } + } } @@ -830,4 +888,76 @@ protected function prepare_field_collection( $field_name, $collection_grouping ) return $flat_collection; } + + /** + * Downloads an image from a URL, uploads it to the Media Library, + * and then optionally attaches it to a post + * + * @param string $group_name + * @param int $post_id + * @param string $image_url + * @access protected + * @since 0.9.5 + * @return mixed Returns a media id or false on failure + */ + protected function create_media_image( $group_name, $post_id, $image_url ) { + + // fetch the image from the cdn and store temporarily + $temp_file = download_url( $image_url ); + + if ( is_wp_error( $temp_file ) ) { + return false; + } + + // attempt to detect the mimetype based on the available functions + $extension = false; + if ( function_exists( 'exif_imagetype' ) && function_exists( 'image_type_to_extension' ) ) { + // open with exif + $image_type = exif_imagetype( $temp_file ); + if ( $image_type ) { + $extension = image_type_to_extension( $image_type ); + } + } + elseif ( function_exists( 'getimagesize' ) && function_exists( 'image_type_to_extension' ) ) { + // open with gd + $file_size = getimagesize( $temp_file ); + if ( isset( $file_size[2] ) ) { + $extension = image_type_to_extension( $file_size[2] ); + } + } + elseif ( function_exists( 'finfo_open' ) ) { + // open with fileinfo + $resource = finfo_open( FILEINFO_MIME_TYPE ); + $mimetype = finfo_file( $resource, $temp_file ); + finfo_close( $resource ); + if ( $mimetype ) { + $mimetype_array = explode( '/', $mimetype ); + $extension = '.' . $mimetype_array[1]; + } + } + + if ( $extension ) { + + $filename = 'ccb-' . sanitize_file_name( strtolower( $group_name ) ) . $extension; + + $file_array = array( + 'name' => $filename, + 'tmp_name' => $temp_file, + ); + + $media_id = media_handle_sideload( $file_array, $post_id ); + @unlink( $temp_file ); + + if ( is_wp_error( $media_id ) ) { + return false; + } + + return $media_id; + + } + else { + return false; + } + } + } diff --git a/includes/class-ccb-core-plugin.php b/includes/class-ccb-core-plugin.php index e772330..3305244 100644 --- a/includes/class-ccb-core-plugin.php +++ b/includes/class-ccb-core-plugin.php @@ -79,6 +79,7 @@ public function __construct() { $this->plugin_display_name = __( 'Church Community Builder Core API', $this->plugin_name ); $this->plugin_short_display_name = __( 'CCB Core API', $this->plugin_name ); $this->version = '0.9.4'; + add_theme_support( 'post-thumbnails' ); } @@ -162,6 +163,7 @@ protected function send_non_blocking_json_response( $response ) { flush(); return true; + } /**