|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Jacobin Core Media Utilities |
| 4 | + * |
| 5 | + * @package Jacobin_Core |
| 6 | + * @subpackage Jacobin_Core\Includes |
| 7 | + * @since 0.4.9 |
| 8 | + * @license GPL-2.0+ |
| 9 | + */ |
| 10 | + |
| 11 | +/** |
| 12 | + * Clean Up Post Media |
| 13 | + * Removes first image in post content, adds as featured image (if none exists) and deletes first image tag |
| 14 | + * |
| 15 | + * @since 0.4.9 |
| 16 | + * |
| 17 | + * @uses jacobin_core_posts_without_featured_image() |
| 18 | + * @uses jacobin_core_find_media_in_post_content() |
| 19 | + * @uses jacobin_core_add_attachment_post() |
| 20 | + * @uses attachment_url_to_postid() |
| 21 | + * @uses set_post_thumbnail() |
| 22 | + * |
| 23 | + * @param int $site |
| 24 | + * @param array $args |
| 25 | + * @return void |
| 26 | + */ |
| 27 | +function jacobin_core_clean_post_images_init( $site = null, $args = array() ) { |
| 28 | + |
| 29 | + if( is_multisite() && $site ) { |
| 30 | + switch_to_blog( $site ); |
| 31 | + } |
| 32 | + |
| 33 | + // If posts |
| 34 | + if( $posts = jacobin_core_posts_without_featured_image( $args ) ) { |
| 35 | + |
| 36 | + $total = count( $posts ); |
| 37 | + $count = 0; |
| 38 | + $no_image = 0; |
| 39 | + |
| 40 | + if ( defined( 'WP_CLI' ) && WP_CLI ) { |
| 41 | + WP_CLI::log( "\n----------\nPosts without featured image: {$total}\n----------\n" ); |
| 42 | + } else { |
| 43 | + echo "\n----------\n"; |
| 44 | + echo "Posts without featured image: {$total}"; |
| 45 | + echo "\n----------\n"; |
| 46 | + } |
| 47 | + |
| 48 | + foreach( $posts as $post_id ) { |
| 49 | + |
| 50 | + $count++; |
| 51 | + |
| 52 | + if ( defined( 'WP_CLI' ) && WP_CLI ) { |
| 53 | + WP_CLI::log( "\n----------\nPost ID: {$post_id} - {$count} of {$total}\n" ); |
| 54 | + } else { |
| 55 | + echo "\n----------\n"; |
| 56 | + echo "Post ID: {$post_id} - {$count} of {$total}\n"; |
| 57 | + } |
| 58 | + |
| 59 | + //If image tag |
| 60 | + if( $url = jacobin_core_find_media_in_post_content( $post_id ) ) { |
| 61 | + |
| 62 | + if ( defined( 'WP_CLI' ) && WP_CLI ) { |
| 63 | + WP_CLI::success( "Image found: {$url}\n" ); |
| 64 | + } else { |
| 65 | + echo "Image found: {$url}\n"; |
| 66 | + } |
| 67 | + |
| 68 | + //If attachment id found |
| 69 | + if( $attachment_id = attachment_url_to_postid( $url ) ) { |
| 70 | + |
| 71 | + if ( defined( 'WP_CLI' ) && WP_CLI ) { |
| 72 | + WP_CLI::success( "Attachment ID found: {$attachment_id}\n" ); |
| 73 | + } else { |
| 74 | + echo "Attachment ID found: {$attachment_id}\n"; |
| 75 | + } |
| 76 | + |
| 77 | + set_post_thumbnail( $post_id, $attachment_id ); |
| 78 | + |
| 79 | + if ( defined( 'WP_CLI' ) && WP_CLI ) { |
| 80 | + WP_CLI::success( "Featured image assigned: {$attachment_id}\n" ); |
| 81 | + } else { |
| 82 | + echo "Featured image assigned: {$attachment_id}\n"; |
| 83 | + } |
| 84 | + |
| 85 | + } else { |
| 86 | + |
| 87 | + if ( defined( 'WP_CLI' ) && WP_CLI ) { |
| 88 | + WP_CLI::log( "No attachment ID found. Creating Post...\n" ); |
| 89 | + } else { |
| 90 | + echo "No attachment ID found. Creating Post...\n"; |
| 91 | + } |
| 92 | + |
| 93 | + jacobin_core_add_attachment_post( $url, $post_id ); |
| 94 | + |
| 95 | + } |
| 96 | + |
| 97 | + |
| 98 | + } else { |
| 99 | + |
| 100 | + if ( defined( 'WP_CLI' ) && WP_CLI ) { |
| 101 | + WP_CLI::warning( "Skipping: No image found in post.\n" ); |
| 102 | + } else { |
| 103 | + echo "Skipping: No image found in post.\n"; |
| 104 | + } |
| 105 | + |
| 106 | + $no_image++; |
| 107 | + |
| 108 | + continue; |
| 109 | + |
| 110 | + } |
| 111 | + |
| 112 | + } |
| 113 | + |
| 114 | + if ( defined( 'WP_CLI' ) && WP_CLI ) { |
| 115 | + WP_CLI::log( "\n----------\nPosts processed: {$count} - Posts without images: {$no_image}\n----------\n" ); |
| 116 | + } else { |
| 117 | + echo "\n----------\nPosts processed: {$count} - Posts without images: {$no_image}\n----------\n"; |
| 118 | + } |
| 119 | + |
| 120 | + } else { |
| 121 | + |
| 122 | + wp_die( 'No posts without featured images were found.' ); |
| 123 | + |
| 124 | + } |
| 125 | + |
| 126 | + if( is_multisite() && $site ) { |
| 127 | + restore_current_blog(); |
| 128 | + } |
| 129 | + |
| 130 | +} |
| 131 | + |
| 132 | +/** |
| 133 | + * Find the Posts with No Featured Image |
| 134 | + * Limits search to post ID for |
| 135 | + * |
| 136 | + * @param array $args |
| 137 | + * @return mixed array of post ids || false |
| 138 | + */ |
| 139 | +function jacobin_core_posts_without_featured_image( $args = array() ) { |
| 140 | + |
| 141 | + $defaults = array( |
| 142 | + 'post_type' => 'post', |
| 143 | + 'posts_per_page' => -1, |
| 144 | + 'fields' => 'ids', |
| 145 | + 'meta_query' => array( |
| 146 | + array( |
| 147 | + 'key' => '_thumbnail_id', |
| 148 | + 'value' => '?', |
| 149 | + 'compare' => 'NOT EXISTS' |
| 150 | + ) |
| 151 | + ), |
| 152 | + ); |
| 153 | + |
| 154 | + $args = wp_parse_args( $args, $defaults ); |
| 155 | + |
| 156 | + $query = new WP_Query( $args ); |
| 157 | + |
| 158 | + if( $query->have_posts() ) { |
| 159 | + |
| 160 | + while( $query->have_posts() ) { |
| 161 | + |
| 162 | + $total = count( $query->posts ); |
| 163 | + |
| 164 | + return $query->posts; |
| 165 | + |
| 166 | + } |
| 167 | + |
| 168 | + } |
| 169 | + |
| 170 | + return false; |
| 171 | + |
| 172 | +} |
| 173 | + |
| 174 | +/** |
| 175 | + * Find First Media Item in Post Content |
| 176 | + * |
| 177 | + * @uses DOMDocument class |
| 178 | + * @link http://php.net/manual/en/class.domdocument.php |
| 179 | + * |
| 180 | + * @param obj $post |
| 181 | + * @return string $url |
| 182 | + */ |
| 183 | +function jacobin_core_find_media_in_post_content( $post_id ) { |
| 184 | + $content = get_post_field( 'post_content', $post_id ); |
| 185 | + |
| 186 | + if( class_exists( 'DomDocument' ) ) { |
| 187 | + // Set error level |
| 188 | + $internalErrors = libxml_use_internal_errors( true ); |
| 189 | + |
| 190 | + $dom = new DomDocument(); |
| 191 | + $dom->loadHTML( mb_convert_encoding( $content, 'HTML-ENTITIES', 'UTF-8' ) ); |
| 192 | + |
| 193 | + $images = $dom->getElementsByTagName( 'img' ); |
| 194 | + |
| 195 | + if( empty( $images ) ) { |
| 196 | + |
| 197 | + return false; |
| 198 | + } |
| 199 | + |
| 200 | + $image = $images[0]; |
| 201 | + |
| 202 | + if( empty( $image ) ) { |
| 203 | + |
| 204 | + return false; |
| 205 | + } |
| 206 | + |
| 207 | + $url = $image->getAttribute('src'); |
| 208 | + |
| 209 | + // Reset error level |
| 210 | + libxml_use_internal_errors( $internalErrors ); |
| 211 | + |
| 212 | + return $url; |
| 213 | + } |
| 214 | + |
| 215 | + else { |
| 216 | + |
| 217 | + wp_die( "Finding an image tag requires DomDocument" ); |
| 218 | + |
| 219 | + } |
| 220 | + |
| 221 | +} |
| 222 | + |
| 223 | +/** |
| 224 | + * Create Attachment Post |
| 225 | + * |
| 226 | + * @param string $url |
| 227 | + * @param int $parent_id |
| 228 | + */ |
| 229 | +function jacobin_core_add_attachment_post( $url, $parent_id ) { |
| 230 | + $wp_upload_dir = wp_upload_dir(); |
| 231 | + |
| 232 | + $base_path = implode( '/', array_slice( explode( '/', $url ), -3, 3, true ) ); |
| 233 | + $filename = trailingslashit( $wp_upload_dir['basedir'] ) . $base_path; |
| 234 | + |
| 235 | + if( file_exists( $filename ) ) { |
| 236 | + |
| 237 | + // Make sure that this file is included, as wp_generate_attachment_metadata() depends on it. |
| 238 | + require_once( ABSPATH . 'wp-admin/includes/image.php' ); |
| 239 | + |
| 240 | + $filetype = wp_check_filetype( basename( $filename ), null ); |
| 241 | + |
| 242 | + $attachment = array( |
| 243 | + 'guid' => $wp_upload_dir['url'] . '/' . basename( $filename ), |
| 244 | + 'post_mime_type' => $filetype['type'], |
| 245 | + 'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $filename ) ), |
| 246 | + 'post_content' => '', |
| 247 | + 'post_status' => 'inherit' |
| 248 | + ); |
| 249 | + |
| 250 | + $attachment_id = wp_insert_attachment( $attachment, $filename, $parent_id ); |
| 251 | + |
| 252 | + $attachment_data = wp_generate_attachment_metadata( $attachment_id, $filename ); |
| 253 | + wp_update_attachment_metadata( $attachment_id, $attachment_data ); |
| 254 | + |
| 255 | + set_post_thumbnail( $parent_id, $attachment_id ); |
| 256 | + |
| 257 | + if ( defined( 'WP_CLI' ) && WP_CLI ) { |
| 258 | + WP_CLI::success( "Media post created: {$url} - {$attachment_id}\nFeatured image assigned: {$attachment_id}\n" ); |
| 259 | + } else { |
| 260 | + echo "Media post created: {$url} - {$attachment_id}\n"; |
| 261 | + echo "Featured image assigned: {$attachment_id}\n"; |
| 262 | + } |
| 263 | + |
| 264 | + } else { |
| 265 | + |
| 266 | + if ( defined( 'WP_CLI' ) && WP_CLI ) { |
| 267 | + WP_CLI::warning( "Media Post not created - File doesn't exist: {$filename}.\n" ); |
| 268 | + } else { |
| 269 | + echo "Media Post not created - File doesn't exist: {$filename}.\n"; |
| 270 | + } |
| 271 | + |
| 272 | + return; |
| 273 | + |
| 274 | + } |
| 275 | +} |
0 commit comments