Last active
October 10, 2017 07:32
-
-
Save orangerdev/47602bc632917a079c1511691af68413 to your computer and use it in GitHub Desktop.
TASK 3 - WPI CLI
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace WP; | |
class CLI | |
{ | |
/** | |
* Construction | |
*/ | |
public function __construct() | |
{ | |
} | |
/** | |
* Set true/false field for all company posts | |
* @param array $args | |
* 1/0 => true|false | |
* | |
* | |
* EXAMPLES | |
* | |
* wp upgrade set 1 | |
* @return void | |
*/ | |
public function set($args) | |
{ | |
if(!isset($args[0])) : | |
\WP_CLI::error(__('Empty upgraded value','CLI')); | |
return; | |
endif; | |
$i = 0; | |
$posts_per_page = 20; | |
$offset = 0; | |
$repeat = true; | |
$boolean = (1 === $args[0]) ? TRUE : FALSE; | |
// CHUNK POSTS TO 20 | |
while($repeat) : | |
$args = [ | |
'posts_per_page' => $posts_per_page, | |
'post_type' => 'company', | |
'post_status' => 'publish', | |
'offset' => $offset | |
]; | |
$posts = get_posts($args); | |
if(is_array($posts) && 0 < count($posts)) : | |
foreach($posts as $post) : | |
update_field('upgraded',$boolean,$post->ID); | |
\WP_CLI::success(sprintf(__('Company ID %s set as %s','CLI'),$post->ID,$boolean)); | |
endforeach; | |
$i++; | |
$offset = $posts_per_page * $i; | |
else : | |
// NOT FOUND ANY POSTS | |
// STOP THE LOOP | |
$repeat = false; | |
endif; | |
endwhile; | |
} | |
} | |
new CLI; | |
\WP_CLI::add_command('upgrade' ,'\WP\CLI'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment