/** * */ public static function insert_posts($data) { if (!is_array($data)) { return; } foreach ($data as $post) { if (!self::post_exists($post['ext_guid'])) { $inserted_post_id = wp_insert_post($post); update_post_meta($inserted_post_id, 'original_permalink', $post['ext_permalink']); update_post_meta($inserted_post_id, 'original_guid', $post['ext_guid']); if (isset($post['post_meta'])) { foreach ($post['post_meta'] as $key => $value) { update_post_meta($inserted_post_id, $key, $value); } } $ext_embed_code = isset($post['ext_embed_code']) ? trim($post['ext_embed_code']) : ''; if ($ext_embed_code) { update_post_meta($inserted_post_id, 'embed_code', $post['ext_embed_code']); } //$ext_image = isset($post['ext_image']) && !is_array($post['ext_image']) ? trim($post['ext_image']) : ''; if (!empty($post['ext_image'])) { if (!is_array($post['ext_image'])) { update_post_meta($inserted_post_id, 'image_url', trim($post['ext_image'])); self::post_image_to_media_library($post['ext_image'], $inserted_post_id, $post['post_title'], true, $post['post_date']); } else { //[$i]['link_url'] //[$i]['image_url'] //[$i]['title'] update_post_meta($inserted_post_id, 'image_url', trim($post['ext_image'][0]['image_url'])); foreach ($post['ext_image'] as $post_image) { self::post_image_to_media_library(trim($post_image['image_url']), $inserted_post_id, $post_image['title'], true, $post['post_date']); } } } else { // possible performance hog // to do: // * activate or deactivate in settings // * check if image-url was already saved (in another article) if so, use it instead if ($post['ext_permalink'] != "") { $reader = new Opengraph\Reader(); $open_graph_content = self::my_get_remote_content($post['ext_permalink']); if ($open_graph_content) { try { $reader->parse($open_graph_content); $open_graph_data = $reader->getArrayCopy(); $image_data = isset($open_graph_data[$reader::OG_IMAGE]) ? array_pop($open_graph_data[$reader::OG_IMAGE]) : array(); $image_url = isset($image_data['og:image:url']) ? $image_data['og:image:url'] : ''; if ($image_url != "") { update_post_meta($inserted_post_id, 'image_url', $image_url); self::post_image_to_media_library($image_url, $inserted_post_id, $post['post_title'], true, $post['post_date']); } } catch (RuntimeException $e) { self::log('Remote opengraph-content not parsable:' . $open_graph_content); } } else { self::log('No ext_permalink remote content fetched'); } } } if ($post['post_format'] != "") { set_post_format($inserted_post_id, $post['post_format']); } } } }