Skip to content
This repository has been archived by the owner on Oct 31, 2022. It is now read-only.

Commit

Permalink
Add support for importing group images as featured images
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredcobb committed Aug 22, 2015
1 parent bae8c66 commit 3b413de
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 6 deletions.
4 changes: 0 additions & 4 deletions admin/class-ccb-core-cpts.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
12 changes: 12 additions & 0 deletions admin/class-ccb-core-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 <em>all</em> 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.<br>If you don't need group images, then disabling this feature will speed up the synchronization.",
),
'groups-advanced' => array(
'field_title' => 'Enable Advanced Settings <em>(Optional)</em>',
'field_render_function' => 'render_switch',
Expand Down
134 changes: 132 additions & 2 deletions admin/class-ccb-core-sync.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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 ) {

Expand Down Expand Up @@ -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 );
Expand Down Expand Up @@ -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 );
}
}

Expand Down Expand Up @@ -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' ) {
Expand Down Expand Up @@ -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 );
}
}
}

}

}

}
Expand Down Expand Up @@ -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;
}
}

}
2 changes: 2 additions & 0 deletions includes/class-ccb-core-plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' );

}

Expand Down Expand Up @@ -162,6 +163,7 @@ protected function send_non_blocking_json_response( $response ) {
flush();

return true;

}

/**
Expand Down

0 comments on commit 3b413de

Please sign in to comment.