first commit
This commit is contained in:
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
namespace WPML\ST\MO\Generate\MultiSite;
|
||||
|
||||
class Condition {
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function shouldRunWithAllSites() {
|
||||
return is_multisite() && (
|
||||
$this->hasPostBodyParam()
|
||||
|| is_super_admin()
|
||||
|| defined( 'WP_CLI' )
|
||||
);
|
||||
}
|
||||
|
||||
private function hasPostBodyParam() {
|
||||
$request_body = file_get_contents( 'php://input' );
|
||||
$data = filter_var_array( (array)json_decode( $request_body ), FILTER_SANITIZE_STRING );
|
||||
|
||||
return isset( $data['runForAllSites'] ) && $data['runForAllSites'];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
namespace WPML\ST\MO\Generate\MultiSite;
|
||||
|
||||
|
||||
class Executor {
|
||||
|
||||
const MAIN_SITE_ID = 1;
|
||||
|
||||
/**
|
||||
* @param callable $callback
|
||||
*
|
||||
* @return \WPML\Collect\Support\Collection
|
||||
*/
|
||||
public function withEach( $callback ) {
|
||||
$applyCallback = function( $siteId ) use ( $callback ) {
|
||||
switch_to_blog( $siteId );
|
||||
|
||||
return [ $siteId, $callback() ];
|
||||
};
|
||||
|
||||
$initialBlogId = get_current_blog_id();
|
||||
$result = $this->getSiteIds()->map( $applyCallback );
|
||||
switch_to_blog( $initialBlogId );
|
||||
|
||||
return $result;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \WPML\Collect\Support\Collection
|
||||
*/
|
||||
public function getSiteIds() {
|
||||
return \wpml_collect( get_sites( [ 'number' => PHP_INT_MAX ] ) )->pluck( 'id' );
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $siteId
|
||||
* @param callable $callback
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function executeWith( $siteId, callable $callback ) {
|
||||
switch_to_blog( $siteId );
|
||||
$result = $callback();
|
||||
restore_current_blog();
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user