get_setting('aspgspintax_install_date') === false)
$ret = $this->add_setting('aspgspintax_install_date', date('Y-m-d h:i:s'));
if($this->get_setting('aspgspintax_rating_div') === false)
$ret = $this->add_setting('aspgspintax_rating_div', 'no');
}
}
$GLOBALS['AdvancedSpintaxPostGenerator'] = new AdvancedSpintaxPostGenerator();
//based on https://gist.github.com/irazasyed/11256369
class aspgspintax_spintax
{
public function process($text)
{
return preg_replace_callback(
'/\{(((?>[^\{\}]+)|(?R))*)\}/x',
array($this, 'replace'),
$text
);
}
public function replace($text)
{
$text = $this->process($text[1]);
$parts = explode('|', $text);
return $parts[array_rand($parts)];
}
}
function aspgspintax_isnone($v) {
return !isset($v) || empty($v) || $v === "" || $v === NULL;
}
function aspgspintax_checkperms() {
// TODO: Add additional user checks here
return current_user_can('publish_posts');
}
function aspgspintax_create_post() {
check_ajax_referer($GLOBALS['aspgspintax_nonce'], 'security'); //will literally die if there's no nonce
global $wpdb; //database
if(!current_user_can('publish_posts')) {
wp_die(); //no perms.
return; //just in case
}
if(!aspgspintax_checkperms()) {
wp_die(); //no perms.
return; //just in case
}
// Default response
$response = array(
'message' => esc_html__('Success!', 'aspgspintax'),
'success' => 1,
'ID' => 0,
);
$spintax = new aspgspintax_spintax();
//$seed = $GLOBALS['post']->ID.'-'.$GLOBALS['aspgspintax_seed'].'-'.'aspg'; //create a psuedorandom seed to keep the same content on updates
$seed = microtime();
$template_id = intval($_POST['template_id']);
$title = "";
$content = "";
$tags = "";
$categories = "";
$post_type = "";
$slug = "";
$date = "";
$status = "";
if(aspgspintax_isnone($template_id)) {
// Sent to wp_insert_post which uses sanitize_post()
$content = $_POST['content']; // WPCS: sanitization ok.
$post_type = $_POST['post_type']; // WPCS: sanitization ok.
$title = $_POST['title']; // WPCS: sanitization ok.
$status = $_POST['status']; // WPCS: sanitization ok.
$tags = $_POST['tags']; // WPCS: sanitization ok.
$categories = $_POST['categories']; // WPCS: sanitization ok.
$slug = $_POST['slug']; // WPCS: sanitization ok.
if(aspgspintax_isnone($status))
$status = 'draft';
if(aspgspintax_isnone($post_type))
$post_type = 'post';
srand((int)md5($seed));
$tags = $spintax->process($tags);
srand((int)md5($seed));
$categories = $spintax->process($categories);
srand((int)md5($seed));
$slug = $spintax->process($slug);
$tags = array_map('trim', explode(',', $tags));
$categories = array_map('trim', explode(',', $categories));
}
else {
//TODO: Allow saving/loading/scheduling templates
//TODO: Featured image
}
if(aspgspintax_isnone($content)) {
$response['message'] = esc_html__('Error, no post content!', 'aspgspintax');
$response['success'] = 0;
goto aspgspintax_main_sub3;
}
if($date === '' || !isset($date)) {
$date = date("Y-m-d H:i:s", time());
}
else {
$date = date("Y-m-d H:i:s", intval($date)); //int
}
if(true) {
$my_post = array(
'post_type' => $post_type,
'post_title' => $title,
'post_content' => $content,
'post_status' => $status,
'post_category' => $categories,
'tags_input' => $tags,
'post_date' => $date,
'post_slug' => $slug,
);
srand((int)md5($seed));
$my_post['post_title'] = $spintax->process($my_post['post_title']);
$my_post['post_content'] = $spintax->process($my_post['post_content']);
$id = wp_insert_post($my_post);
if($id == 0) {
$response['message'] = esc_html__('Failed to create post. Unknown error.', 'aspgspintax');
$response['success'] = 0;
}
else {
$response['ID'] = $id;
}
goto aspgspintax_main_sub3;
}
aspgspintax_main_sub3: { // Eh, screw good practice. How bad can it be?
wp_send_json($response);
wp_die();
}
}
add_action( 'wp_ajax_aspgspintax_create_post', 'aspgspintax_create_post' );
// Add settings page link on left
function aspgspintax_action_links( $links ) {
$links[] = ''.esc_html__('Generate Posts','aspgspintax').'';
$links[] = ''.esc_html__('DesignSmoke','aspgspintax').'';
return $links;
}
add_filter( 'plugin_action_links_' . plugin_basename(__FILE__), 'aspgspintax_action_links' );