-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPageManager.php
executable file
·86 lines (73 loc) · 3.4 KB
/
PageManager.php
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
<?php
namespace TestPlugin {
use TestPlugin\PageInfo;
/**
* A class used to managed pages created by this plugin
* @package TestPlugin
*/
class PageManager {
private $pluginName = "";
/**
* The generic placeholder text to be saved as the plugin page's post content. This uses the special placeholder "{pluginName}" to "inject" the plugin name dynamically.
*/
private static $genericPageContent = "<h1>Plugin Generated Page<h1> <div>Page content generated by the plugin \"{pluginName}\". If you're seeing this, the plugin isn't activated/installed or the plugin's template file wasn't loaded.</div>";
private $pageFolder = "";
public function __construct($folder = "", $pluginName = "") {
$this->pageFolder = $folder;
$this->pluginName = $pluginName;
}
/**
* Given PageInfo, this verifies a plugin page exists. If this page doesn't exist, it'll be created.
* <b> This method CANNOT be used before the "init" WordPress Hook </b>
*
* @param PageInfo $pageToCheck The page to verify exists (and creates it if it doesn't)
*/
public function ensureBasicPluginPageExists(PageInfo $pageToCheck) {
$pagePost = get_page_by_title($pageToCheck->pageTitle);
if (($pagePost === NULL) || ($pagePost === FALSE)) {//doesn't exist, so create it
PageManager::createPluginPage($pageToCheck);
} else {//already exists
// the plugin may have been previously active and the page may just be trashed
if($pagePost->post_status != 'publish') {
$pagePost->post_status = 'publish';
wp_update_post($pagePost);
}
}
}
/**
* Given PageInfo, this creates a plugin page.
* <b> This method CANNOT be used before the "init" WordPress Hook </b>
*
* @param PageInfo $pageToCreate The page to create
* @return int The id of the plugin page's post ID
*/
public function createPluginPage(PageInfo $pageToCreate):int {
global $wp_rewrite;
// Create post object
$p = array();
$newPageGuid = implode("/",[site_url(),$pageToCreate->getPostName(),""]);
$p['post_title'] = $pageToCreate->pageTitle;
$p['post_name'] = $pageToCreate->getPostName();
$p['post_content'] = str_replace('{pluginName}',$this->pluginName,PageManager::$genericPageContent);
$p['post_status'] = 'publish';
$p['post_type'] = 'page';
$p['comment_status'] = 'closed';
$p['ping_status'] = 'closed';
$p['post_category'] = array(1); // the default :'Uncategorised'
// Insert the post
$the_page_id = wp_insert_post($p);
return $the_page_id;
}
/**
* Given PageInfo, this <b>deletes</b> a plugin page.
*
* @param PageInfo $page The page to delete
*/
public function deletePluginPage(PageInfo $page) {
$pagePost = get_page_by_title($page->pageTitle);
if (($pagePost !== NULL) && ($pagePost !== FALSE)) {
wp_delete_post($pagePost->ID, true);
}
}
}
}