Skip to content

Commit e3c4041

Browse files
committed
Merge branch '0.4.9'
2 parents 24fc117 + cfa844b commit e3c4041

File tree

5 files changed

+350
-4
lines changed

5 files changed

+350
-4
lines changed

includes/customizations.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
/**
3+
* Jacobin Core Customization Functions
4+
*
5+
* @package Jacobin_Core
6+
* @subpackage Jacobin_Core\Includes
7+
* @since 0.4.9
8+
* @license GPL-2.0+
9+
*/
10+
11+
/**
12+
* Remove Image Sizes
13+
* WP generates a version for each registered image size when new media is added, this deregisters unneeded sizes.
14+
*
15+
* @since 0.4.9
16+
*
17+
* @return void
18+
*/
19+
function jacobin_core_remove_image_sizes() {
20+
remove_image_size( 'guest-author-32' );
21+
remove_image_size( 'guest-author-50' );
22+
remove_image_size( 'guest-author-64' );
23+
remove_image_size( 'guest-author-96' );
24+
remove_image_size( 'guest-author-128' );
25+
}
26+
add_action( 'init', 'jacobin_core_remove_image_sizes' );

integrations/wp-cli.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
/**
3+
* Jacobin Core WP-CLI Integration
4+
*
5+
* @package Jacobin_Core
6+
* @subpackage Jacobin_Core\Includes
7+
* @since 0.4.9
8+
* @license GPL-2.0+
9+
*/
10+
11+
/**
12+
* Run Add Featured Images Command
13+
*
14+
* Usage: `wp add-featured-images --site={id}`
15+
*
16+
* @uses jacobin_core_clean_post_images_init()
17+
* @uses WP_CLI
18+
*
19+
* @param array $args
20+
* @param array $assoc_args
21+
* @return void
22+
*/
23+
function jacobin_core_cli_add_featured_images( $args = array(), $assoc_args = array() ) {
24+
25+
$defaults = array(
26+
'site' => null
27+
);
28+
29+
$assoc_args = wp_parse_args( $assoc_args, $defaults );
30+
31+
jacobin_core_clean_post_images_init( $assoc_args['site'], $args );
32+
33+
}
34+
35+
if ( defined( 'WP_CLI' ) && WP_CLI ) {
36+
WP_CLI::add_command( 'add-featured-images', 'jacobin_core_cli_add_featured_images' );
37+
}

jacobin-core-functionality.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
* Text Domain: jacobin-core
1111
* Domain Path: /languages
1212
*
13-
* Version: 0.4.8
13+
* Version: 0.4.9
1414
*
1515
* @package Core_Functionality
1616
*/
@@ -26,6 +26,7 @@
2626
define( 'JACOBIN_CORE_DIR_URL', plugin_dir_url( __FILE__ ) );
2727

2828
require_once( 'includes/helpers.php' );
29+
require_once( 'includes/customizations.php' );
2930

3031
// Load plugin libraries
3132
require_once( 'includes/lib/class-jacobin-core-post-type.php' );
@@ -45,6 +46,8 @@
4546

4647
// Load utility files
4748
require_once( 'utils/copy-content.php' );
49+
require_once( 'utils/media-utilities.php' );
50+
require_once( 'integrations/wp-cli.php' );
4851

4952

5053

@@ -55,7 +58,7 @@
5558
* @return object Jacobin_Core
5659
*/
5760
function Jacobin_Core () {
58-
$instance = Jacobin_Core::instance( __FILE__, '0.4.8' );
61+
$instance = Jacobin_Core::instance( __FILE__, '0.4.9' );
5962

6063
return $instance;
6164
}

readme.txt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
Contributors: misfist
33
Tags: custom post type, custom taxonomy, rest api
44
Requires at least: 4.7
5-
Tested up to: 4.9.0
6-
Version: 0.4.8
5+
Tested up to: 4.9.2
6+
Version: 0.4.9
77
License: GPLv2 or later
88
License URI: http://www.gnu.org/licenses/gpl-2.0.html
99

@@ -25,6 +25,11 @@ This section describes how to install the plugin and get it working.
2525

2626
== Changelog ==
2727

28+
### 0.4.9 January 25, 2017
29+
* Added media cleanup utility
30+
* Removed unused image sizes
31+
* Added WP-CLI integration
32+
2833
### 0.4.8 January 17, 2017
2934
* Added string cleanup utility function
3035

utils/media-utilities.php

Lines changed: 275 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,275 @@
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

Comments
 (0)