<?php

/**
 * @author Harri Bell-Thomas <*****@*****.**>
 * @created January, 2014 
 * @version 1.0.0
 * @license Attribution-ShareAlike 3.0 Unported (CC BY-SA 3.0)
 * @license url : http://creativecommons.org/licenses/by-sa/3.0/
*/
/*  INCLUDE WRAPPER  */
require_once 'class.postcontroller.php';
/*  USAGE  */
$Poster = new PostController();
$Poster->set_title("My Post's Title");
$Poster->add_category(array(1, 2, 8));
$Poster->set_type("post");
$Poster->set_content("This my awesome content");
$Poster->set_author_id(1);
$Poster->set_post_slug('updated_post');
//$Poster->set_page_template( "login-infusion-page.php" );
$Poster->set_post_state("publish");
$Poster->search('title', 'Old Post');
$Poster->update();
//$Poster->create();
//$Poster->PrettyPrintAll();
$Poster->get_var('slug');
    function rbs_ajax_create_article()
    {
        if (isset($_POST['galleryid']) && (int) $_POST['galleryid'] && isset($_POST['categoryid']) && (int) $_POST['categoryid']) {
            $galleryid = intval($_POST['galleryid']);
            $categoryid = intval($_POST['categoryid']);
            $post_info = get_post($galleryid);
            if (gettype($post_info) != 'object') {
                echo '<p><strong>' . __('Post not created. Error: ', 'rbs_gallery') . '</strong><br><p>empty gallery id</p>';
                die;
            }
            $Poster = new PostController();
            $title = $post_info->post_title;
            if (isset($_POST['articletitle']) && $_POST['articletitle']) {
                $title = wp_kses_data($_POST['articletitle']);
            }
            $slug = 'post_' . $post_info->post_name;
            if (isset($_POST['articleslug']) && $_POST['articleslug']) {
                $slug = wp_kses_data($_POST['articleslug']);
            }
            $Poster->set_title($title);
            $Poster->add_category(array($categoryid));
            $Poster->set_type("post");
            $Poster->set_content('[robo-gallery id="' . $galleryid . '"]');
            $Poster->set_author_id(get_current_user_id());
            $Poster->set_post_slug($slug);
            $Poster->set_post_state("publish");
            $Poster->create();
            $posts_id = get_post_meta($galleryid, 'rbs_gallery_id', true);
            if (!$posts_id) {
                $posts_id = array();
            } else {
                $posts_id = json_decode($posts_id, true);
            }
            $postId = $Poster->PC_current_post_id;
            $posts_id[] = $postId;
            update_post_meta($galleryid, 'rbs_gallery_id', json_encode($posts_id, JSON_FORCE_OBJECT));
            if (isset($Poster->errors) && count($Poster->errors)) {
                echo '<p><strong>' . __('Post not created. Error: ', 'rbs_gallery') . '</strong><br>';
                for ($i = 0; $i < count($Poster->errors); $i++) {
                    $error = $Poster->errors[$i];
                    echo ' &nbsp;&nbsp; - ' . $error . '<br>';
                }
                echo '</p>';
            } else {
                echo '<h3>' . __('Post ', 'rbs_gallery') . '"' . $title . '"' . __(' created', 'rbs_gallery') . '</h3>';
                echo '<p>
					<a href="' . esc_url(get_edit_post_link($postId)) . '" class="button button-small" target="_blank">
						' . __('Edit', 'rbs_gallery') . '</a> 
					<a  href="' . esc_url(get_permalink($postId)) . '"  class="button button-small" target="_blank">
						' . __('Preview', 'rbs_gallery') . '</a> 
				</p>';
            }
        } else {
            echo '<p><strong>' . __('Error: input value', 'rbs_gallery') . '</strong></p>';
        }
        die;
    }