File "provider_layout.class.php"

Full path: /home/kosmetik/public_html/wp-content/plugins/unlimited-elements-for-elementor/provider/provider_layout.class.php
File size: 11.79 B
MIME-type: text/x-php
Charset: utf-8

Download   Open   Edit   Advanced Editor   Back

<?php

defined('UNLIMITED_ELEMENTS_INC') or die('Restricted access');
class UniteCreatorLayout extends UniteCreatorLayoutWork
{
    private $post;
    private $metaData;
    public function __construct()
    {
        parent::__construct();
    }
    public function initByPost($post)
    {
        UniteFunctionsUC::validateNotEmpty($post, " post object");
        $postID = $post->ID;
        $title = $post->post_title;
        $name = $post->post_name;
        $postType = $post->post_type;
        $this->post = $post;
        if (empty($this->extraParams)) {
            $this->extraParams = array();
        }
        $this->extraParams["post_type"] = $postType;
        $layoutType = "";
        $params = null;
        if ($postType == GlobalsProviderUC::POST_TYPE_LAYOUT) {
            $layoutType = GlobalsUnlimitedElements::ADDONSTYPE_ELEMENTOR_TEMPLATE;
        }
        $params = get_post_meta($postID, GlobalsProviderUC::META_KEY_LAYOUT_PARAMS, true);
        $layoutData = "";
        $catID = get_post_meta($postID, GlobalsProviderUC::META_KEY_CATID, true);
        if (empty($catID)) {
            $catID = 0;
        }
        if (!empty($params) && is_array($params)) {
            $params = $params[0];
        }
        $record = array();
        $record["ordering"] = 0;
        $record["title"] = $title;
        $record["layout_data"] = $layoutData;
        $record["layout_type"] = $layoutType;
        $record["name"] = $name;
        $record["params"] = $params;
        $record["id"] = $postID;
        $record["catid"] = $catID;
        $this->initByRecord($record);
    }
    public function initByID($id)
    {
        if ($id === "current_post") {
            $id = null;
            $post = get_post();
            if (empty($post)) {
                UniteFunctionsUC::throwError("Current post not found");
            }
            $this->id = $post->ID;
        } else {
            $id = (int) $id;
            if (empty($id)) {
                UniteFunctionsUC::throwError("Empty layout ID");
            }
            $post = @get_post($id);
            if (empty($post)) {
                UniteFunctionsUC::throwError("layout with id: {$id} not found");
            }
            $this->id = $id;
        }
        $this->initByPost($post);
    }
    public function createLayoutInDB($arrInsert, $arrParams = array())
    {
        UniteFunctionsUC::validateNotEmpty($this->objLayoutType, "layout type object");
        $postType = $this->objLayoutType->postType;
        if (empty($postType)) {
            if ($this->objLayoutType->isBasicType == true) {
                $postType = UniteFunctionsUC::getVal($arrParams, "post_type");
            } else {
                $postType = GlobalsProviderUC::POST_TYPE_LAYOUT;
            }
        }
        if (empty($postType)) {
            UniteFunctionsUC::throwError("You must specify post type for import layout");
        }
        $title = UniteFunctionsUC::getVal($arrInsert, "title");
        $name = UniteFunctionsUC::getVal($arrInsert, "name");
        if ($postType != GlobalsProviderUC::POST_TYPE_LAYOUT) {
            $name = sanitize_title($title);
        }
        $layoutData = UniteFunctionsUC::getVal($arrInsert, "layout_data");
        $catID = UniteFunctionsUC::getVal($arrInsert, "catid");
        $parentID = UniteFunctionsUC::getVal($arrParams, "parent_id");
        if (is_numeric($parentID) == false) {
            $parentID = null;
        }
        $arrPost = array();
        $arrPost["post_title"] = $title;
        $arrPost["post_name"] = $name;
        $arrPost["post_content"] = "unlimited elements layout";
        $arrPost["post_type"] = $postType;
        $arrPost["post_status"] = "publish";
        $maxOrder = UniteFunctionsWPUC::getMaxMenuOrder($postType, $parentID);
        $arrPost["menu_order"] = $maxOrder + 1;
        if (!empty($parentID)) {
            $arrPost["post_parent"] = $parentID;
        }
        $postID = wp_insert_post($arrPost);
        if (!empty($catID)) {
            add_post_meta($postID, GlobalsProviderUC::META_KEY_CATID, $catID);
        }
        return $postID;
    }
    public function updateLayoutInDB($arrUpdate)
    {
        $postID = $this->id;
        $title = UniteFunctionsUC::getVal($arrUpdate, "title");
        if ($title == "Auto Draft") {
            UniteFunctionsUC::throwError("Please change the title, 'Auto Draft' is a bad title :), <br><br> The page will be published when you save");
        }
        $arrUpdatePost = array();
        if (array_key_exists("title", $arrUpdate)) {
            $arrUpdatePost["post_title"] = $arrUpdate["title"];
        }
        $postStatus = $this->post->post_status;
        if ($postStatus == "auto-draft") {
            $arrUpdatePost["post_status"] = "draft";
            $importTitle = UniteFunctionsUC::getVal($arrUpdate, "import_title");
            if (empty($importTitle)) {
                $importTitle = $this->getNewLayoutTitle();
            }
            $arrUpdatePost["post_title"] = $importTitle;
        }
        if (isset($arrUpdatePost["post_title"])) {
            $postName = $this->post->post_name;
            if (empty($postName)) {
                $arrUpdatePost["post_name"] = sanitize_title($arrUpdatePost["post_title"]);
            }
        }
        if (!empty($arrUpdatePost)) {
            $arrUpdatePost["ID"] = $postID;
            $success = wp_update_post($arrUpdatePost);
            if ($success == 0) {
                UniteFunctionsUC::throwError("Unable to update page");
            }
        }
        if (array_key_exists("params", $arrUpdate)) {
            $params = UniteFunctionsUC::getVal($arrUpdate, "params");
            update_post_meta($postID, GlobalsProviderUC::META_KEY_LAYOUT_PARAMS, $params);
        }
        if (array_key_exists("layout_data", $arrUpdate)) {
            $layoutData = $arrUpdate["layout_data"];
            update_post_meta($postID, GlobalsProviderUC::META_KEY_BLOX_PAGE, "true");
            $updated = update_post_meta($postID, GlobalsProviderUC::META_KEY_LAYOUT_DATA, $layoutData);
            if ($updated == false) {
                $oldData = $this->getRawLayoutData();
                if ($oldData != $layoutData) {
                    UniteFunctionsUC::throwError("Unable to update page layout data");
                }
            }
            $layoutHtml = HelperUC::outputLayout($postID, true);
            $layoutHtml = UniteFunctionsUC::getPrettyHtml($layoutHtml);
            $arrUpdateContent = array();
            $arrUpdateContent["ID"] = $postID;
            $arrUpdateContent["post_content"] = $layoutHtml;
            wp_update_post($arrUpdateContent);
        }
    }
    public function duplicate()
    {
        $this->validateInited();
        $newTitle = $this->getDuplicateTitle();
        $postID = $this->id;
        $newPostID = UniteFunctionsWPUC::duplicatePost($postID, $newTitle);
        return $newPostID;
    }
    public function delete()
    {
        $this->validateInited();
        wp_delete_post($this->id, true);
        delete_metadata("post", $this->id, GlobalsProviderUC::META_KEY_BLOX_PAGE);
        delete_metadata("post", $this->id, GlobalsProviderUC::META_KEY_LAYOUT_DATA);
        delete_metadata("post", $this->id, GlobalsProviderUC::META_KEY_CATID);
        delete_metadata("post", $this->id, GlobalsProviderUC::META_KEY_LAYOUT_TYPE);
    }
    public function getParentID()
    {
        $this->validateInited();
        $parentID = $this->post->post_parent;
        return $parentID;
    }
    public function getLastRevision()
    {
        $this->validateInited();
        $arrRevisions = wp_get_post_revisions($this->id);
        if (empty($arrRevisions)) {
            return null;
        }
        $firstRevision = array_shift($arrRevisions);
        if (empty($firstRevision)) {
            UniteFunctionsUC::throwError("Failed to get template revision");
        }
        $revisionID = $firstRevision->ID;
        return $revisionID;
    }
    protected function isLayoutExistsByTitle($title, $layoutType)
    {
        $isExists = UniteFunctionsWPUC::isPostExistsByTitle($title);
        return $isExists;
    }
    private function getPostBaseName($postID)
    {
        if (empty($postID)) {
            return null;
        }
        $post = get_post($postID);
        if (empty($post)) {
            return null;
        }
        $postTitle = $post->post_title;
        $postName = $post->post_name;
        if (empty($postTitle)) {
            return $postName;
        }
        $baseName = HelperUC::convertTitleToAlias($postTitle);
        return $baseName;
    }
    protected function getNewLayoutName($title, $importParams)
    {
        $parentID = UniteFunctionsUC::getVal($importParams, "parent_id");
        $baseName = null;
        if (!empty($parentID)) {
            $baseName = $this->getPostBaseName($parentID);
        }
        $name = $this->generateName($title, null, $baseName);
        return $name;
    }
    public function getExportLayoutName()
    {
        $this->validateInited();
        $parentID = $this->post->post_parent;
        $prefix = "template";
        $layoutName = $prefix;
        if (!empty($parentID)) {
            $layoutName .= "_" . $this->getPostBaseName($parentID);
        }
        $layoutName .= "_" . $this->getPostBaseName($this->id);
        $layoutName = HelperUC::convertTitleToHandle($layoutName);
        return $layoutName;
    }
    protected function isLayoutExistsByName($name, $layoutType)
    {
        $isExists = UniteFunctionsWPUC::isPostNameExists($name);
        return $isExists;
    }
    public function generateHtmlAddonContentForLayout($postContent)
    {
        $objAddons = new UniteCreatorAddons();
        $isExists = $objAddons->isAddonExistsByName("html_editor");
        if ($isExists == false) {
            return null;
        }
        $objAddon = new UniteCreatorAddon();
        $objAddon->initByName("html_editor");
        $arrContent = array();
        $arrContent["editor"] = $postContent;
        $objAddon->setParamsValues($arrContent);
        $arrAddonContent = $objAddon->getDataForLayoutGrid();
        return $arrAddonContent;
    }
    public function checkNewPostLayoutContent()
    {
        $this->validateInited();
        if (!empty($this->metaData)) {
            return false;
        }
        $postContent = $this->post->post_content;
        $postContent = trim($postContent);
        if (empty($postContent)) {
            return false;
        }
        $arrAddonContent = $this->generateHtmlAddonContentForLayout($postContent);
        if (empty($arrAddonContent)) {
            return false;
        }
        $this->addRowWithHtmlAddon($arrAddonContent);
    }
    public function isGroup()
    {
        $this->validateInited();
        $arrChildren = UniteFunctionsWPUC::getPostChildren($this->post);
        if (!empty($arrChildren)) {
            return true;
        }
        return false;
    }
    public function getUrlEditPost()
    {
        $this->validateInited();
        $urlEdit = get_edit_post_link($this->id);
        return $urlEdit;
    }
    public function getUrlViewPost()
    {
        $this->validateInited();
        $urlView = get_permalink($this->id);
        return $urlView;
    }
    public function getPreviewImage($getThumb = false)
    {
        $this->validateInited();
        $attachmentID = UniteFunctionsWPUC::getFeaturedImageID($this->id);
        $urlPreview = null;
        if (!empty($attachmentID)) {
            if ($getThumb == true) {
                $urlPreview = UniteFunctionsWPUC::getUrlAttachmentImage($attachmentID, UniteFunctionsWPUC::THUMB_MEDIUM);
            } else {
                $urlPreview = UniteFunctionsWPUC::getUrlAttachmentImage($attachmentID);
            }
        }
        return $urlPreview;
    }
    public function getDefaultPreviewImage()
    {
        $typeName = $this->objLayoutType->typeName;
        $urlPreview = HelperUC::getDefaultPreviewImage($typeName);
        return $urlPreview;
    }
}