function testPluggable() { add_filter("remote-syncers", function ($syncers) { $syncers[] = new TestSyncer(); return $syncers; }); RemoteSyncPlugin::instance()->syncers = array(); $syncers = RemoteSyncPlugin::instance()->getEnabledSyncers(); $this->assertEquals(3, sizeof($syncers)); $res = RemoteSyncPlugin::instance()->getApi()->ls(array("type" => "TestSyncer")); $this->assertEquals(4, sizeof($res)); $res = RemoteSyncPlugin::instance()->getApi()->get(array("type" => "TestSyncer", "slug" => "c")); $this->assertEquals("c", $res["slug"]); $this->assertEquals(15, $res["attachments"][0]["fileSize"]); }
function test_push() { RemoteSyncPlugin::instance()->install(); $postId = wp_insert_post(array('post_content' => 'content', 'post_name' => 'the-slug', 'post_title' => "Hello Post")); update_option("rs_remote_site_url", "http://example.com/"); Curl::mockResult(array()); Curl::mockResult(array()); Curl::mockResult(array()); $op = new RemoteSyncOperations(); $op->push(); wp_trash_post($postId); Curl::initMock(); Curl::mockResult(array(array("slug" => 'the-slug', 'revision' => "hello"))); Curl::mockResult(array()); Curl::mockResult(array()); $op = new RemoteSyncOperations(); $op->push(); }
function setUp() { parent::setUp(); global $wpdb; $wpdb->show_errors(); $wpdb->query("CREATE TABLE {$wpdb->prefix}h5p_contents ( " . "id INTEGER NOT NULL auto_increment, " . "slug VARCHAR(255) not null, " . "title VARCHAR(255) not null, " . "parameters VARCHAR(255) not null, " . "filtered VARCHAR(255) not null, " . "embed_type VARCHAR(255) not null, " . "disable INTEGER not null, " . "content_type VARCHAR(255) not null, " . "license VARCHAR(255) not null, " . "keywords VARCHAR(255) not null, " . "description VARCHAR(255) not null, " . "library_id INTEGER not null," . "PRIMARY KEY(id))"); if ($wpdb->last_error) { throw new Exception($wpdb->last_error); } $wpdb->query("CREATE TABLE {$wpdb->prefix}h5p_libraries ( " . "id INTEGER NOT NULL auto_increment, " . "name VARCHAR(255) not null, " . "major_version INTEGER not null, " . "minor_version INTEGER not null, " . "patch_version INTEGER not null, " . "PRIMARY KEY(id))"); if ($wpdb->last_error) { throw new Exception($wpdb->last_error); } $wpdb->query("CREATE TABLE {$wpdb->prefix}h5p_contents_libraries ( " . "content_id INTEGER NOT NULL, " . "library_id INTEGER NOT NULL, " . "dependency_type VARCHAR(255) not null, " . "weight INTEGER NOT NULL, " . "drop_css INTEGER NOT NULL, " . "PRIMARY KEY(content_id,library_id,dependency_type))"); if ($wpdb->last_error) { throw new Exception($wpdb->last_error); } RemoteSyncPlugin::instance()->syncers = NULL; RemoteSyncPlugin::instance()->install(); }
function test_ls() { RemoteSyncPlugin::instance()->syncers = NULL; RemoteSyncPlugin::instance()->install(); $api = new RemoteSyncApi(); $res = $api->ls(array("type" => "post")); $this->assertCount(0, $res); $id1 = wp_insert_post(array("post_title" => "post one", "post_name" => "post-one")); $id2 = wp_insert_post(array("post_title" => "post two", "post_parent" => $id1, "post_name" => "post-two")); $id3 = wp_insert_post(array("post_title" => "post three", "post_parent" => $id2, "post_name" => "post-three")); $res = $api->ls(array("type" => "post")); $this->assertCount(3, $res); $this->assertEquals("post-one", $res[0]["slug"]); $this->assertEquals("post-two", $res[1]["slug"]); $this->assertEquals("post-three", $res[2]["slug"]); $post1 = get_post($id1); $post1->post_parent = NULL; wp_update_post($post1); $post2 = get_post($id2); $post2->post_parent = NULL; wp_update_post($post2); $post3 = get_post($id3); $post3->post_parent = NULL; wp_update_post($post3); $post1 = get_post($id1); $post1->post_parent = $id2; wp_update_post($post1); $post2 = get_post($id2); $post2->post_parent = $id3; wp_update_post($post2); $post3 = get_post($id3); $post3->post_parent = NULL; wp_update_post($post3); $res = $api->ls(array("type" => "post")); $this->assertCount(3, $res); $this->assertEquals("post-three", $res[0]["slug"]); $this->assertEquals("post-two", $res[1]["slug"]); $this->assertEquals("post-one", $res[2]["slug"]); }
/** * Fetch all remote resources for type. */ public static function fetchAllForType($type) { $infos = RemoteSyncPlugin::instance()->remoteCall("ls")->addPostField("type", $type)->exec(); $remoteResources = array(); foreach ($infos as $info) { /*$logger=RemoteSyncPlugin::instance()->getLogger(); if ($logger) $logger->log(print_r($info,TRUE));*/ $remoteResource = new RemoteResource($type, $info["slug"], $info["revision"]); $remoteResources[] = $remoteResource; } return $remoteResources; }
/** * Activation hook. */ function rs_activate() { RemoteSyncPlugin::instance()->install(); }
/** * Push. */ public function push() { $syncers = RemoteSyncPlugin::instance()->getEnabledSyncers(); foreach ($syncers as $syncer) { $this->log("Push: " . $syncer->getType()); $syncResources = SyncResource::findAllForType($syncer->getType(), SyncResource::POPULATE_LOCAL | SyncResource::POPULATE_REMOTE); foreach ($syncResources as $syncResource) { switch ($syncResource->getState()) { case SyncResource::NEW_REMOTE: case SyncResource::UPDATED_REMOTE: case SyncResource::DELETED_REMOTE: $this->log($syncResource->getSlug() . ": Remotely changed, please pull."); break; case SyncResource::NEW_LOCAL: $syncResource->createRemoteResource(); $syncResource->save(); $this->log(" " . $syncResource->getSlug() . ": Created remote."); break; case SyncResource::UPDATED_LOCAL: $syncResource->updateRemoteResource(); $syncResource->save(); $this->log(" " . $syncResource->getSlug() . ": Updated remote."); break; case SyncResource::DELETED_LOCAL: $syncResource->deleteRemoteResource(); $syncResource->delete(); $this->log(" " . $syncResource->getSlug() . ": Deleted remote."); break; case SyncResource::CONFLICT: $this->log(" " . $syncResource->getSlug() . ": Conflict, skipping."); break; case SyncResource::UP_TO_DATE: $syncResource->save(); break; case SyncResource::GARBAGE: //echo "garbage!!!\n"; $syncResource->delete(); break; } } } }
function test_state() { update_option("rs_remote_site_url", "helloworld"); RemoteSyncPlugin::instance()->syncers = array(new SRTestSyncer("testType")); RemoteSyncPlugin::instance()->install(); $syncer = RemoteSyncPlugin::instance()->getSyncerByType("testType"); $data = $syncer->getResource("slug1"); $rev = md5(json_encode($data)); Curl::mockResult(array(array("slug" => "onlyremote", "revision" => "05a1ad082ad35cad7aac7b18e232feb3"), array("slug" => "slug1", "revision" => $rev))); $syncResources = SyncResource::findAllForType("testType", SyncResource::POPULATE_REMOTE | SyncResource::POPULATE_LOCAL); $a = array(); foreach ($syncResources as $syncResource) { $a[$syncResource->getSlug()] = $syncResource; } //echo "l: ".sizeof($syncResources); $syncResource = $syncResources[0]; $data = $syncResource->getData(); $rev = $syncResource->getLocalRevision(); $this->assertEquals($a["onlyremote"]->getState(), SyncResource::NEW_REMOTE); $this->assertEquals($a["onlylocal"]->getState(), SyncResource::NEW_LOCAL); $this->assertEquals($a["slug1"]->getState(), SyncResource::UP_TO_DATE); }
<?php require_once __DIR__ . "/src/utils/WpUtil.php"; require_once __DIR__ . "/src/plugin/RemoteSyncPlugin.php"; use remotesync\WpUtil; require_once WpUtil::getWpLoadPath(); require_once ABSPATH . 'wp-admin/includes/plugin.php'; header('Content-Type: application/json'); if (!$_REQUEST) { http_response_code(500); echo json_encode(array("error" => TRUE, "message" => "Got no request data, you might need to increase post_max_size and/or upload_max_filesize. " . "post_max_size=" . ini_get("post_max_size") . " upload_max_filesize=" . ini_get("upload_max_filesize"))); exit; } $plugin = new RemoteSyncPlugin(); $plugin->getApi()->handleApiCall($_REQUEST["action"], $_REQUEST);
<?php require_once __DIR__ . "/src/utils/WpUtil.php"; require_once __DIR__ . "/src/plugin/RemoteSyncPlugin.php"; use remotesync\WpUtil; require_once WpUtil::getWpLoadPath(); require_once ABSPATH . 'wp-admin/includes/plugin.php'; $logger = new EventStream(); $logger->start(); $instance = RemoteSyncPlugin::instance(); $instance->setLogger($logger); $instance->getOperations()->handleOperation($_REQUEST["action"]); $logger->done();
function test_doApiCall() { RemoteSyncPlugin::instance()->install(); $id = wp_insert_post(array("post_title" => "hello", "post_type" => "post", "post_content" => "something", "post_name" => "the-name")); update_option('rs_access_key', "test"); $api = new RemoteSyncApi(); // test listing with the right key $ls_res1 = array(); $args = array("key" => "test", "type" => "post", "version" => 2); $ls_res1 = $api->doApiCall("ls", $args); //print_r($ls_res1); //test listing with the wrong key $ls_res2 = array(); $args = array("key" => "Wrong Key", "type" => "post"); $ls_res2 = $api->doApiCall("ls", $args); //print_r($ls_res2); // test deleting with wrong key $del_res1 = array(); $args = array("key" => "wrong key", "type" => "post", "slug" => $ls_res1[0]["slug"]); $del_res1 = $api->doApiCall("del", $args); //print_r($del_res1); // test deleting with right key $del_res1 = array(); $args = array("key" => "test", "type" => "post", "slug" => $ls_res1[0]["slug"], "version" => 2); $del_res1 = $api->doApiCall("del", $args); //print_r($del_res1); }
/** * Handle the Api Response */ public function doApiCall($call, $params) { $res = array(); if (!(array_key_exists("key", $params) && $params["key"] === get_option("rs_access_key", ""))) { $res += array("Error" => "Operation NOT permitted!!\nEither you have not set the access key or the access key does not match the remote access key."); return $res; } else { if (!array_key_exists("version", $params) || $params["version"] != RemoteSyncPlugin::instance()->getProtocolVersion()) { throw new Exception("Your local version of wp-remote-sync is not compatible " . "with the version installed on this remote server."); } else { $res = call_user_func(array($this, $call), $params); return $res; } } }
/** * Add local attachments to call for upload. */ private function addAttachmentsToRemoteCall($call) { $logger = RemoteSyncPlugin::instance()->getLogger(); foreach ($this->getAttachments() as $attachment) { if (!$this->isRemoteAttachmentCurrent($attachment)) { $logger->log("Uploading: " . $attachment->getFileName()); $encodedName = urlencode($attachment->getFileName()); $encodedName = str_replace('.', '%2E', $encodedName); $encodedName = str_replace('-', '%2D', $encodedName); $call->addFileUpload($encodedName, $this->getAttachmentDirectory() . "/" . $attachment->getFileName()); } else { $logger->log("Skipping: " . $attachment->getFileName()); } } }