1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179:
<?php
require_once __DIR__."/../model/Swagpath.php";
class SwagpathSyncer {
private function getIdBySlug($slug) {
global $wpdb;
if (!$slug)
return 0;
$q=$wpdb->prepare(
"SELECT ID ".
"FROM {$wpdb->prefix}posts ".
"WHERE post_name=%s ".
"AND post_type IN ('swagpath') ".
"AND post_status IN ('publish','draft')",
$slug);
$id=$wpdb->get_var($q);
if ($wpdb->last_error)
throw new Exception($wpdb->last_error);
return $id;
}
private function getSlugById($postId) {
$post=get_post($postId);
if (!$post)
return NULL;
if ($post->post_type!="swagpath")
throw new Exception("Expected type to be post or page, not: ".$post->post_type);
if ($post->post_status=="trash" || $post->post_status=="inherit")
return NULL;
return $post->post_name;
}
public function listResourceSlugs() {
$q=new WP_Query(array(
"post_type"=>"swagpath",
"post_status"=>"any",
"posts_per_page"=>-1
));
$posts=$q->get_posts();
$res=array();
foreach ($posts as $post)
if ($post->post_status=="publish" || $post->post_status=="draft")
$res[]=$post->post_name;
return $res;
}
public function updateResource($slug, $info) {
$data=$info->getData();
if ($info->isCreate()) {
$id=wp_insert_post(array(
"post_name"=>$slug,
"post_title"=>$data["post_title"],
"post_type"=>"swagpath",
"comment_status"=>$data["comment_status"]
));
}
else {
$id=$this->getIdBySlug($slug);
}
$post=get_post($id);
if (!$post)
throw new Exception("No swagpath post, strange");
$post->post_excerpt=$data["excerpt"];
$post->post_title=$data["title"];
$post->post_status=$data["status"];
$post->comment_status=$data["comment_status"];
wp_update_post($post);
update_post_meta($id,"swagifact",$data["swagifact"]);
if ($data["lessonplan"]) {
$q=new WP_Query(array(
"post_type"=>"attachment",
"name"=>$data["lessonplan"]
));
$attachmentPosts=$q->get_posts();
if (!$attachmentPosts)
throw new Exception("Lessonplan not found, slug=".$data["lessonplan"]);
$attachmentPost=$attachmentPosts[0];
update_post_meta($id,"lessonplan",$attachmentPost->ID);
}
else {
update_post_meta($id,"lessonplan","");
}
$preSlugs=$data["prerequisites"];
update_post_meta($id,"prerequisites",$preSlugs);
wp_set_object_terms($id,$data["tracks"],"swagtrack");
}
public function getResource($slug) {
$id=$this->getIdBySlug($slug);
$post=get_post($id);
if (!$post)
return NULL;
$lessonplanPost=get_post(get_post_meta($id,"lessonplan",TRUE));
if (!$post)
return NULL;
$preSlugs=get_post_meta($id,"prerequisites",TRUE);
sort($preSlugs);
$tracks=wp_get_object_terms($id,"swagtrack");
$trackSlugs=array();
foreach ($tracks as $track)
$trackSlugs[]=$track->slug;
sort($trackSlugs);
return array(
"title"=>$post->post_title,
"status"=>$post->post_status,
"swagifact"=>get_post_meta($id,"swagifact",TRUE),
"lessonplan"=>$lessonplanPost->post_name,
"excerpt"=>$post->post_excerpt,
"prerequisites"=>$preSlugs,
"tracks"=>$trackSlugs,
"comment_status"=>$post->comment_status
);
}
public function deleteResource($slug) {
$id=$this->getIdBySlug($slug);
wp_trash_post($id);
}
}
add_filter("remote-syncers",function($syncers) {
$syncers[]=new SwagpathSyncer();
return $syncers;
});