Skip to content

Commit

Permalink
Issue #49 - Implement action=import to import a new post from a slave
Browse files Browse the repository at this point in the history
  • Loading branch information
bobbingwide committed Jan 24, 2020
1 parent 7d61277 commit 5956040
Show file tree
Hide file tree
Showing 2 changed files with 166 additions and 38 deletions.
82 changes: 76 additions & 6 deletions admin/class-oik-clone-admin-slave.php
Expand Up @@ -12,6 +12,8 @@ class Oik_clone_admin_slave {
private $clone_post_type;
private $posts;
private $table;
private $action; // The action to perform for a selected post
private $slave_id; // The slave ID against which to perform the action

function __construct() {
$this->slave = null;
Expand All @@ -22,23 +24,40 @@ function __construct() {
}

function do_admin_page() {

$action = $this->validate_action();
$slave_id = $this->validate_slave_id();
$slave_fields = $this->validate_slave_form_fields();
if ( $action && $slave_id && $slave_fields ) {
oik_box( null, 'slave_action', 'Processing', [$this, 'oik_clone_slave_action' ] );
}
oik_box( null, 'slave_form', "Slave", [ $this, 'oik_clone_slave_form' ] );
oik_box( null, 'slave_posts', 'Posts', [ $this, 'oik_clone_slave_posts' ] );

oik_menu_footer();

}

function oik_clone_slave_form() {
/**
* Performs the requested action against the slave.
*
* The action and slave_id should have been validated
* Now we have to find which parts of the mapping are needed to perform the action!
*
*/
function oik_clone_slave_action() {
do_action( 'oik-clone-slave-action-' . $this->action );
}

function oik_clone_slave_form() {
$slave_list = $this->oik_clone_slave_form_validate();
if ( $slave_list ) {
$slave_list = $this->validate_slave_form_fields();
}
$this->oik_clone_slave_form_display();
if ( $slave_list ) {
p( "Getting posts" );

}
return $slave_list;
}

function oik_clone_slave_form_validate() {
Expand All @@ -52,6 +71,46 @@ function oik_clone_slave_form_validate() {
return $slave_list;
}

function validate_action() {
$action = bw_array_get( $_REQUEST, 'action', null );
switch ( $action ) {
case 'import':
add_action( 'oik-clone-slave-action-import', [$this, 'oik_clone_slave_action_import'] );
break;
case 'pull':
case 'push':
die( "Why don't you die now?");
break;
default:
$action = null;

}
$this->action = $action;
return $this->action;
}

function validate_slave_id() {
$this->slave_id = null;
$slave_id = bw_array_get( $_REQUEST, 'slave_id', null );
if ( is_numeric( $slave_id ) ) {
$this->slave_id = $slave_id;
}
return $this->slave_id;
}

function oik_clone_slave_action_import() {
p( "Processing import" );
p( "Slave ID: " . $this->slave_id );
oik_require( 'admin/class-oik-clone-reconcile.php', 'oik-clone' );
$oik_clone_reconcile = new Oik_clone_reconcile();
$oik_clone_reconcile->set_slave( $this->slave );
$oik_clone_reconcile->set_slave_url( $this->slave );
$oik_clone_reconcile->set_post_type( $this->clone_post_type );
$oik_clone_reconcile->set_verbose( true );
$oik_clone_reconcile->set_dry_run( false );
$oik_clone_reconcile->import( $this->slave_id );
}

function validate_slave_form_fields() {
$this->validate_slave();
$this->validate_clone_post_type();
Expand All @@ -69,15 +128,26 @@ function validate_clone_post_type(){
$this->clone_post_type = $clone_post_type;
}

function display_slave_field() {
bw_textfield( 'slave', 80, 'Slave', $this->slave );
}

function display_post_type_field() {
bw_textfield( 'clone_post_type', 32, 'Post type', $this->clone_post_type );

}


function oik_clone_slave_form_display() {

bw_form();
stag( 'table', "form-table" );
//bw_flush();

bw_textfield( 'slave', 80, 'Slave', $this->slave );
bw_textfield( 'clone_post_type', 32, 'Post type', $this->clone_post_type );
$this->display_slave_field();
$this->display_post_type_field();



etag( "table" );
p( isubmit( "_oik_clone_slave_list", "List posts", "button-primary" ) );
Expand All @@ -87,7 +157,7 @@ function oik_clone_slave_form_display() {
}

function oik_clone_slave_posts() {
p( "posts go here");
//p( "posts go here");
oik_require( 'admin/class-oik-clone-reconcile.php', 'oik-clone' );
$oik_clone_reconcile = new Oik_clone_reconcile();
$oik_clone_reconcile->set_slave( $this->slave );
Expand Down
122 changes: 90 additions & 32 deletions admin/class-oik-clone-reconcile.php
Expand Up @@ -49,38 +49,36 @@ function __construct() {
$this->set_slave();
$this->set_slave_url();
$this->get_dry_run();
$this->get_verbose();
$this->set_verbose();
$this->set_dry_run();
$this->sanity_check();
$this->process_post_types();
*/
}

/**
* Obtain the value for the slave
*
* If not specified then die.
* If it is specified perhaps we should check it to be a valid URL
* or maybe we can determine the slave as the first from the list of slaves
* Sets the value for the slave.
*
* More importantly, we also need the slave's target URL - where we ask another server for the information that the real URL might tell us.
* This is used when importing from a local instance of the remote site. See set_slave_url().
*
* @param string $slave The URL of the slave server
*/
function set_slave( $slave=null ) {
//$slave=oik_batch_query_value_from_argv( 1, null );
if ( ! $slave ) {
echo PHP_EOL;
echo "Syntax: oikwp class-oik-clone-reconcile-batch.php slave" . PHP_EOL;
echo "e.g. oikwp class-oik-clone-reconcile-batch.php https://oik-plugins.co.uk" . PHP_EOL;
echo "or, to reconcile with a local copy of the slave at s.b/oikcouk," . PHP_EOL;
echo "oikwp class-oik-clone-reconcile-batch.php https://oik-plugins.co.uk http://s.b/oikcouk " . PHP_EOL;
die( "Try again with the right parameters" );
//p( "Choose a slave server");
}
$this->slave =$slave;
}

/**
* Sets the value for the slave server when it's on a (different) local URL.
*
* @param string $slave_url The URL of the local instance of the slave server
*/
function set_slave_url( $slave_url=null ) {
$this->slave_url = $slave_url;

}

/**
Expand All @@ -100,22 +98,23 @@ function set_master() {
bw_trace2( $this->master, "master" );
}

function get_dry_run() {
$dry_run =oik_batch_query_value_from_argv( "dry-run", "y" );
$this->dry_run=bw_validate_torf( $dry_run );
function set_post_type( $post_type ) {
$this->post_type = $post_type;
}

function set_dry_run( $dry_run = true ) {
$this->dry_run = $dry_run;
if ( $this->dry_run ) {
$this->echo( "Dry run:", 'Yes' );
} else {
$this->echo( "Dry run:", 'No' );
}
}

function get_verbose() {
$verbose =oik_batch_query_value_from_argv( "verbose", "n" );
$this->verbose=bw_validate_torf( $verbose );
function set_verbose( $verbose = false ) {
$this->verbose = $verbose;
if ( $this->verbose ) {
$this->echo( "Verbose:", 'Yes' );

} else {
$this->echo( "Verbose", 'No' );
}
Expand Down Expand Up @@ -164,7 +163,7 @@ function process_post_types() {
}

/**
* Send an AJAX request to the server
* Sends an AJAX request to the server.
*/
function process_post_type( $post_type ) {
$this->post_type = $post_type;
Expand All @@ -174,15 +173,14 @@ function process_post_type( $post_type ) {
}

/**
* Request the mapping of cloned posts
* Requests the mapping of (cloned) posts.
*
* The server is expected to reply with a JSON array consisting of the
* narrative and the mappings
* where each mapping consists of: master_ID, slave_ID and time
*
* @param string $post_type - the post type for which the mapping is requested
*
* @return array the result of the AJAX request
* @param string $post_type - the post type for which the mapping is requested.
* @return array the result of the AJAX request.
*/
function request_mapping( $post_type ) {
$url =$this->slave_url . "/wp-admin/admin-ajax.php";
Expand All @@ -203,7 +201,7 @@ function request_mapping( $post_type ) {
$result=oik_remote::bw_remote_post( $url, $args );
bw_trace2( $result );
if ( ! is_wp_error( $result ) ) {
$this->echo( "Result:", $result );
$this->echo( "Result:", 'Mappings returned' );
} else {
$this->report_wp_error( $result );
$result=null;
Expand Down Expand Up @@ -262,7 +260,7 @@ function apply_mapping( $mapping ) {
$post=null;
if ( null === $mapping->id ) {
$this->echo( "Not cloned:", $mapping->slave );
gob();

} else {
$post=get_post( $mapping->id );
}
Expand Down Expand Up @@ -551,7 +549,7 @@ function summarise( $post, $mapping, $match ) {

if ( $match ) {
$summary[]=$post->post_modified_gmt;
$summary[] = $post->post_name . '<br />' . $mapping->name;
$summary[] = $post->post_name;
$summary[]=$post->post_type;
$summary[] = $post->ID;
$summary[]= $this->master_link( $post->ID );
Expand Down Expand Up @@ -588,7 +586,8 @@ function admin_clone_url( $action, $key ) {
$admin_clone_url = admin_url( 'admin.php?page=oik_clone&amp;tab=slave' );
$args = [ 'slave' => $this->slave_url
, 'clone_post_type' => $this->post_type
, $action => $key
, 'action' => $action
, 'slave_id' => $key
];
$admin_clone_url = add_query_arg( $args, $admin_clone_url );
return $admin_clone_url;
Expand All @@ -597,12 +596,23 @@ function admin_clone_url( $action, $key ) {
function action_link( $action, $slave_id ) {

$retlink = $action;
$link = null;
switch ( $action ) {
case 'Dry import':
$link = $this->admin_clone_url( 'import', $slave_id );
$retlink = retlink( null, $link, $action );
$action = 'Import';
break;
case 'Pull':
$link = $this->admin_clone_url( 'pull', $slave_id );
break;
case 'Push':
$link = $this->admin_clone_url( 'push', $slave_id );
break;


}
if ( $link ) {
$retlink = retlink( null, $link, $action );
}
return $retlink;
}
Expand All @@ -618,6 +628,54 @@ function master_link( $post_id ) {
return retlink( null, get_permalink( $post_id ), get_the_title( $post_id ) );
}

/**
* Imports the specified post from the slave.
*
* We need to find the mapping in order to set the name and modified date.
* Can we do this by requesting the full mapping?
* Or just request one?
*
* @param ID $slave_id post ID of the slave post to import.
*/
function import( $slave_id ) {
$mapping = $this->retrieve_mapping( $slave_id );
if ( $mapping ) {
e( "Found: " . $mapping->slave );
$this->perform_import( $mapping );
} else {
e( "Could not find slave post in mapping" );
}
}

/**
* Retrieves the latest mapping for the slave ID.
*
* @param ID $slave_id Post ID on the slave.
* @return mixed|null mapping object if found.
*/
function retrieve_mapping( $slave_id ) {
$result = $this->request_mapping( $this->post_type );
$mappings = $this->extract_mappings( $result );
$mapping = $this->get_mapping( $mappings, $slave_id );
return $mapping;

}

function get_mapping( $mappings, $slave_id ) {
$slave_mapping = null;
foreach ( $mappings as $mapping ) {
if ( $mapping->slave == $slave_id ) {
$slave_mapping = $mapping;
//e( "Found $slave_id");
break;
}
}
if ( null === $slave_mapping ) {
bw_trace2( $mapping, "Slave ID not found in mapping", true, BW_TRACE_ERROR );
}
return $slave_mapping;
}



}
Expand Down

0 comments on commit 5956040

Please sign in to comment.