%PDF- %PDF-
| Direktori : /dom405686/wp-content/plugins/pws-core/admin/handlers/ |
| Current File : /dom405686/wp-content/plugins/pws-core/admin/handlers/class-scaffold-handler.php |
<?php
namespace GD_cORe\handlers;
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* The admin-specific functionality of the plugin.
*
*
* @package GD_cORe
* @subpackage GD_cORe/admin
*/
class Scaffold extends Main {
public function __construct(){
}
/**
* Takes json site structure and sends it out to build site scaffold
*
* @since 2.8
* @return string
*
**/
public function scaffoldGenerator($siteContent){
$siteContent = json_decode($siteContent, true);
$menu_exists = wp_get_nav_menu_object('main');;
if( !$menu_exists){
$menu_id = wp_create_nav_menu( 'main' );
}else{
$menu_id = $menu_exists->term_id;
}
$pageMenuArray = [];
foreach($siteContent["pages"] as $item){
$pageInfo = $this->createPages($item, $pageMenuArray, $menu_id);
$pageMenuArray = array_unique(array_merge($pageMenuArray, $pageInfo), SORT_REGULAR);
}
exit(wp_redirect(admin_url(self::PLUGIN_MAIN)));
}
/**
* creates pages and navagation based on provided data
*
* @since 2.8
* @return (WP_POST) Array
*
**/
public function createPages($pageData, $currentPages, $menu_id, $hasParent = false){
$slug = wp_strip_all_tags(strtolower(trim(preg_replace('/[^A-Za-z0-9-]+/', '-', $pageData["title"]), '-')));
// if page is a page make it
if($pageData["navType"] === "default"){
$pageContent = array_key_exists('copy', $pageData) && $pageData['copy'] === "yes" ? "<hr /><h1>System Warning:</h1> This page has copy writing that belongs to it. Please use the copy writing as the source of truth for all copy related content.<hr /><br />" : '';
$pageContent .= array_key_exists('notes', $pageData) ? "<hr /><h1>Website Specialist Notes:</h1> " . $pageData['notes'] . "<hr /><br />" : '';
$pageContent .= array_key_exists('content', $pageData) ? "<hr /><h1>Website Content:</h1> " . $pageData['content'] . "<hr /><br />" : '';
$postObject = array(
'post_title' => wp_strip_all_tags($pageData["title"]),
'post_content' => $pageContent,
'post_status' => 'publish',
'post_type' => 'page',
'post_name' => $slug
);
// Insert the post into the database
$pageID = wp_insert_post( $postObject );
}
// add nav item
if($hasParent || array_key_exists('parentTitle', $pageData)){
$parentSlug = wp_strip_all_tags(strtolower(trim(preg_replace('/[^A-Za-z0-9-]+/', '-', $pageData["parentTitle"]), '-')));
if($pageData["navType"] === "default"){
$pageMenuItem = wp_update_nav_menu_item($menu_id, 0, array(
'menu-item-title' => wp_strip_all_tags($pageData["title"]),
'menu-item-object-id' => $pageID,
'menu-item-object' => 'page',
'menu-item-status' => 'publish',
'menu-item-type' => 'post_type',
'menu-item-parent-id' => $currentPages[$parentSlug]
));
}else{
$pageLink = array_key_exists('hyperUrl', $pageData) ? $pageData['hyperUrl'] : '#';
$pageMenuItem = wp_update_nav_menu_item($menu_id, 0, array(
'menu-item-title' => wp_strip_all_tags($pageData["title"]),
'menu-item-status' => 'publish',
'menu-item-type' => 'custom',
'menu-item-parent-id' => $currentPages[$parentSlug],
'menu-item-url' => $pageLink,
));
}
$currentPages[$slug] = $pageMenuItem;
}else{
if($pageData["navType"] === "default"){
$pageMenuItem = wp_update_nav_menu_item($menu_id, 0, array(
'menu-item-title' => wp_strip_all_tags($pageData["title"]),
'menu-item-object-id' => $pageID,
'menu-item-object' => 'page',
'menu-item-status' => 'publish',
'menu-item-type' => 'post_type',
));
}else{
$pageLink = array_key_exists('hyperUrl', $pageData) ? $pageData['hyperUrl'] : '#';
$pageMenuItem = wp_update_nav_menu_item($menu_id, 0, array(
'menu-item-title' => wp_strip_all_tags($pageData["title"]),
'menu-item-status' => 'publish',
'menu-item-type' => 'custom',
'menu-item-url' => $pageLink,
));
}
$currentPages[$slug] = $pageMenuItem;
}
if(array_key_exists('children', $pageData)){
$childrenArray = [];
foreach($pageData["children"] as $childPage){
$pageInfo = $this->createPages($childPage, $currentPages, $menu_id, true);
$currentPages = array_unique(array_merge($currentPages,$pageInfo), SORT_REGULAR);
}
}
return $currentPages;
}
}