options = get_option('_ai_assistant');
if( ! isset( $this->options->token ) ){
$this->options = new stdClass();
$this->options->token = null;
}
add_filter('https_ssl_verify', '__return_false');
add_action('init', [$this, 'init'] );
add_action('plugins_loaded', [$this, 'langs']);
add_action('admin_menu', [$this, 'menu']);
add_action('wp_footer', [$this, 'front'] );
add_action('admin_enqueue_scripts', [$this, 'scripts'] );
add_filter('mce_external_plugins', [$this, 'add_button']);
add_filter('mce_buttons', [$this, 'button_init'], 999);
add_action('edit_form_after_title', [$this, 'metabox'] );
add_action('wp_ajax_saveContent', [$this, 'saveContent']);
add_action('wp_ajax_clearContent', [$this, 'clearContent']);
add_action('wp_ajax_aiassist_sign', [$this, 'sign']);
add_action('wp_ajax_aiassist_getStat', [$this, 'getStat']);
add_action('wp_ajax_aiassist_buy', [$this, 'buy']);
add_action('wp_ajax_saveStep', [$this, 'saveStep']);
add_action('wp_ajax_saveTranslateImagesPromts', [$this, 'saveTranslateImagesPromts']);
add_action('wp_ajax_loadImage', [$this, 'loadImage']);
add_action('wp_ajax_saveKey', [$this, 'saveKey']);
add_action('wp_ajax_getBonus', [$this, 'getBonus']);
add_action('wp_ajax_rewriteOptions', [$this, 'rewriteOptions']);
add_action('wp_ajax_autoGenOptions', [$this, 'autoGenOptions']);
add_action('wp_ajax_assistcron', [$this, 'cron']);
add_action('wp_ajax_nopriv_assistcron', [$this, 'cron']);
add_action('wp_ajax_initRewrite', [$this, 'initRewrite']);
add_action('wp_ajax_startRewrite', [$this, 'startRewrite']);
add_action('wp_ajax_clearRewrite', [$this, 'clearRewrite']);
add_action('wp_ajax_stopRewrite', [$this, 'stopRewrite']);
add_action('wp_ajax_postRestore', [$this, 'postRestore']);
add_action('wp_ajax_stopArticlesGen', [$this, 'stopArticlesGen']);
add_action('wp_ajax_clearArticlesGen', [$this, 'clearArticlesGen']);
add_action('wp_ajax_initArticlesGen', [$this, 'initArticlesGen']);
add_action('wp_ajax_startArticlesGen', [$this, 'startArticlesGen']);
add_action('wp_ajax_removeQueueArticle', [$this, 'removeQueueArticle']);
add_action('wp_ajax_replaceImagesStart', [$this, 'replaceImagesStart']);
add_action('wp_ajax_replaceImagesStop', [$this, 'replaceImagesStop']);
add_action('wp_ajax_replaceImagesReset', [$this, 'replaceImagesReset']);
add_action('wp_ajax_replaceImagesRemove', [$this, 'replaceImagesRemove']);
add_action('wp_ajax_replaceImagesRestore', [$this, 'replaceImagesRestore']);
add_action('activated_plugin', [$this, 'active'], 10, 2 );
add_action('deactivate_plugin', [$this, 'inactive']);
add_action('admin_notices', [$this, 'notice']);
add_action('admin_init', [$this, 'removeNotice'] );
}
public function langs(){
load_theme_textdomain('wp-ai-assistant', plugin_dir_path( __FILE__ ) .'/langs' );
}
public function getDefaultLangId(){
$lang = preg_replace('/_[A-Z]+/', '', get_locale() );
if( $this->info->promts->code && is_numeric( $lang_id = array_search( $lang, $this->info->promts->code ) ) )
return $lang_id;
if( $lang != 'ru' )
return 1;
return 0;
}
public function menu(){
add_menu_page('AI WP Writer', 'AI WP Writer', 'level_10', 'wpai-assistant', [$this, 'options'], 'dashicons-businessperson', 777);
}
public function notice(){
$screen = get_current_screen();
if( @$this->options->token ){
switch( @$screen->id ){
case 'plugins':
case 'dashboard':
case 'toplevel_page_wpai-assistant':
include dirname(__FILE__) . '/tpl/notice.php';
break;
}
}
}
public function removeNotice() {
global $wp_filter;
if( ! isset( $wp_filter['admin_notices'] ) || ! ( $wp_filter['admin_notices'] instanceof WP_Hook ) || ( ! isset( $_GET['page'] ) || $_GET['page'] != 'wpai-assistant' ) )
return;
$hook = new WP_Hook();
foreach( $wp_filter['admin_notices']->callbacks as $priority => $callbacks ){
foreach( $callbacks as $callback_id => $callback ){
if( is_array( $callback['function'] ) && is_object( $callback['function'][0] ) && get_class( $callback['function'][0] ) === get_class( $this ) )
$hook->add_filter( $priority, $callback['function'], $priority, $callback['accepted_args'] );
}
}
$wp_filter['admin_notices'] = $hook;
}
public function cron(){
global $wpdb;
$data = [ 'rewrites' => [], 'articles' => [] ];
$wpdb->query('SET innodb_lock_wait_timeout=1');
$wpdb->query('START TRANSACTION');
$hold = $wpdb->query('SELECT * FROM `'. $wpdb->options .'` WHERE `option_name`="aiWriterCronCheck" FOR UPDATE');
if( $hold === false )
return $data;
$check = (int) get_option('aiWriterCronCheck');
if( $check < time() - 60 ){
$data['rewrites'] = $this->aiRewrite();
$data['articles'] = $this->aiArticlesAutoGen();
$data['images'] = $this->replaceImages();
update_option('aiWriterCronCheck', time() );
} else {
$data['rewrites'] = get_option('aiRewritesData');
$data['articles'] = get_option('aiArticlesAutoGenData');
$data['images'] = get_option('aiImagesData');
}
$wpdb->query('COMMIT');
if( is_user_logged_in() )
wp_die( json_encode( $data ) );
}
private function maskKey( $key, $first = 4, $last = 5 ){
if( $key == '' )
return null;
return esc_attr( substr( $key, 0, $first ) .' '. str_repeat('•', max(4, strlen( $key ) - $first - $last ) ) .' '. substr( $key, -$last ) );
}
public function init(){
if( isset( $_POST['save'] ) && $this->checkNonce() && current_user_can('manage_options') ){
$this->options = new stdClass();
$this->options->cron = isset( $_POST['cron'] );
if( ! $options = get_option('_ai_assistant') )
$options = (object) [];
$this->options->token = @$options->token;
if( @$_POST['token'] != '' && preg_match('/^[A-Za-z0-9]{64}$/i', $_POST['token']) )
$this->options->token = sanitize_text_field( $_POST['token'] );
if( @$_POST['token'] != '' ){
$this->activation( sanitize_text_field( $this->options->token ), $this->options->cron );
$this->setInfo();
}
if( @$_POST['token'] == '' )
unset( $this->options->token );
update_option('_ai_assistant', $this->options );
update_option('_ai_system_prompt', isset( $_POST['system_prompt'] ) ? 1 : 0 );
}
}
public function options(){
$system_prompt = get_option('_ai_system_prompt');
if( $system_prompt === false )
$system_prompt = true;
if( ! $images = get_option('aiImagesData') )
$images = [ 'start' => false, 'attachments' => [] ];
if( ! $rewrites = get_option('aiRewritesData') )
$rewrites = [];
if( ! $autoGen = get_option('aiArticlesAutoGenData') )
$autoGen = [];
if( ! $cats = get_categories( [ 'hide_empty' => 0 ] ) )
$cats = [];
if( ! $users = get_users( [ 'fields' => [ 'ID', 'user_email', 'display_name' ] ] ) )
$users = [];
include dirname(__FILE__) . '/tpl/options.php';
}
public function replaceImagesReset(){
if( ! $this->checkNonce() || ! current_user_can('manage_options') )
return;
update_option('aiImagesData', [ 'start' => false, 'attachments' => [] ] );
}
public function replaceImagesStop(){
if( ! $this->checkNonce() || ! current_user_can('manage_options') )
return;
$data = get_option('aiImagesData');
$data['start'] = false;
update_option('aiImagesData', $data);
}
public function replaceImagesRemove(){
if( ! $this->checkNonce() || ! current_user_can('manage_options') )
return;
$data = get_option('aiImagesData');
if( $data['attachments'] ){
foreach( $data['attachments'] as $attach ){
if( isset( $attach['attach_id'] ) && isset( $attach['replace_id'] ) )
wp_delete_attachment( (int) $attach['attach_id'], true );
}
update_option('aiImagesData', [ 'start' => false, 'attachments' => [] ] );
wp_die('{"removing":"true"}');
}
wp_die('{"removing":"false"}');
}
public function replaceImagesRestore(){
if( ! $this->checkNonce() || ! current_user_can('manage_options') )
return;
$data = get_option('aiImagesData');
if( $data['attachments'] ){
$attachments = [];
foreach( $data['attachments'] as $attach )
$attachments[ $attach['post_id'] ][] = $attach;
if( $attachments ){
foreach( $attachments as $post_id => $attachs ){
if( $post = get_post( (int) $post_id ) ){
foreach( $attachs as $attach ){
if( isset( $attach['thumbnail'] ) )
update_post_meta( (int) $attach['post_id'], '_thumbnail_id', (int) $attach['attach_id'] );
$post->post_content = str_replace( $attach['replace_url'], $attach['url'], $post->post_content );
$post->post_content = str_replace( 'wp-image-'. (int) $attach['replace_id'], 'wp-image-'. (int) $attach['attach_id'], $post->post_content );
$this->replaceImageToElementor( $attach['post_id'], $attach['replace_id'], $attach['attach_id'], $attach['url'] );
wp_delete_attachment( (int) $attach['replace_id'], true );
}
wp_update_post( [ 'ID' => (int) $post_id, 'post_content' => $post->post_content ] );
}
}
update_option('aiImagesData', [ 'start' => false, 'attachments' => [] ] );
}
}
}
public function replaceImagesStart(){
global $wpdb;
if( ! $this->checkNonce() || ! current_user_can('manage_options') )
return;
$data = get_option('aiImagesData');
$posts_ids = [];
$data['start'] = true;
if( isset( $_POST['imageModel'] ) )
$data['imageModel'] = sanitize_text_field( $_POST['imageModel'] );
if( $_POST['cat'] ){
$sql = 'SELECT
p.`ID`
FROM
'. $wpdb->posts .' p
INNER JOIN '. $wpdb->term_relationships .' tr ON (p.ID=tr.object_id)
INNER JOIN '. $wpdb->term_taxonomy .' tt ON (tr.term_taxonomy_id=tt.term_taxonomy_id)
WHERE
tt.`taxonomy`="category"
AND
tt.`term_id`=%d
AND
( p.`post_status`="publish" OR p.`post_status`="private" OR p.`post_status`="pending" OR p.`post_status`="draft" OR p.`post_status`="future" )';
$posts_ids = $wpdb->get_col( $wpdb->prepare( $sql, (int) $_POST['cat'] ) );
}
if( $_POST['types'] ){
$types = array_map('sanitize_key', (array) $_POST['types']);
$sql = 'SELECT
`ID`
FROM
'. $wpdb->posts .'
WHERE
`post_type` IN ('. implode( ',', array_fill( 0, count( $types ), '%s' ) ) .')
AND
( `post_status`="publish" OR `post_status`="private" OR `post_status`="pending" OR `post_status`="draft" OR `post_status`="future" )';
$posts_ids = $wpdb->get_col( $wpdb->prepare( $sql, ...$types ) );
}
if( $_POST['links'] ){
foreach( $_POST['links'] as $link ){
if( $id = $this->url_to_postid( $link ) )
$posts_ids[] = $id;
}
}
if( $posts_ids ){
$i = -1;
$data['attachments'] = [];
$posts_ids = array_map( 'absint', $posts_ids );
if( $posts = $wpdb->get_results( $wpdb->prepare('SELECT `ID`, `post_content` FROM '. $wpdb->posts .' WHERE `ID` IN ('. implode( ',', array_fill( 0, count( $posts_ids ), '%d' ) ) .')', ...$posts_ids ) ) ){
foreach( $posts as $post ){
if( $thumbnail_id = (int) get_post_meta( (int) $post->ID, '_thumbnail_id', true ) ){ $i++;
$thumbnail = wp_get_attachment_image_src( $thumbnail_id, 'full' );
$data['attachments'][ $i ] = [ 'post_id' => $post->ID, 'attach_id' => $thumbnail_id, 'url' => $thumbnail[0], 'thumbnail' => true ];
}
if( preg_match_all('/]*)src ?= ?["\']([^\'"]*)["\']([^>]*)>/isU', $post->post_content, $attachs) ){
if( $attachs[2] ){
foreach( $attachs[2] as $k => $attach ){ $i++;
$data['attachments'][ $i ] = [ 'post_id' => $post->ID, 'url' => $attach ];
if( preg_match('/wp-image-([0-9]*)/', $attachs[1][ $k ], $attach_id ) )
$data['attachments'][ $i ]['attach_id'] = (int) $attach_id[1];
elseif( preg_match('/wp-image-([0-9]*)/', $attachs[3][ $k ], $attach_id ) )
$data['attachments'][ $i ]['attach_id'] = (int) $attach_id[1];
if( preg_match('/size-([a-z]*)/', $attachs[1][ $k ], $size ) )
$data['attachments'][ $i ]['size'] = (string) $size[1];
elseif( preg_match('/wp-image-([a-z]*)/', $attachs[3][ $k ], $size ) )
$data['attachments'][ $i ]['size'] = (string) $size[1];
}
}
}
}
}
}
update_option('aiImagesData', $data);
wp_die( json_encode( $data ) );
}
private function replaceImages(){
$data = get_option('aiImagesData');
if( ! @$data['start'] || ! @$data['attachments'] )
return $data;
$break = false;
$compleat = 0;
$this->setInfo();
foreach( $data['attachments'] as $k => $attach ){
if( ! isset( $attach['attach_id'] ) ){
if( $attach_id = $this->getAttachIdByUrl( $attach['url'] ) )
$data['attachments'][ $k ]['attach_id'] = $attach['attach_id'] = (int) $attach_id;
}
if( ! isset( $attach['task_id'] ) ){
$attach_url = $attach['url'];
if( stripos( $attach_url, 'http' ) === false )
$attach_url = $this->getHost() . $attach_url;
$args = [
'url' => $attach_url,
'model' => $data['imageModel'],
'img_promt' => isset( $this->steps['promts']['img_replace'][ $this->getImageModelIndex( $data['imageModel'] ) ] ) ? $this->steps['promts']['img_replace'][ $this->getImageModelIndex( $data['imageModel'] ) ] : $this->info->promts->img_replace[ $this->getImageModelIndex( $data['imageModel'] ) ],
'action' => 'replaceImage',
'token' => $this->options->token
];
$task = json_decode( $this->wpCurl( $args ) );
if( $task->task_id )
$data['attachments'][ $k ]['task_id'] = $attach['task_id'] = (int) $task->task_id;
$break = true;
}
if( isset( $attach['task_id'] ) && ! isset( $attach['replace_id'] ) && $attach['request'] < 5 ){
$task = json_decode( $this->wpCurl( [ 'action' => 'getTask', 'id' => (int) $attach['task_id'], 'host' => $this->getHost(), 'token' => $this->options->token ] ) );
if( isset( $task->error ) && $task->error == 'error_url' ){
if( $attach['attach_id'] ){
$path = get_attached_file( $attach['attach_id'] );
if( $path[0] == '/' && stripos( $path, ABSPATH ) === false )
$path = ABSPATH . $path;
if( file_exists( $path ) ){
$args = [
'url' => $attach['url'],
'model' => $data['imageModel'],
'img_promt' => isset( $this->steps['promts']['img_replace'][ $this->getImageModelIndex( $data['imageModel'] ) ] ) ? $this->steps['promts']['img_replace'][ $this->getImageModelIndex( $data['imageModel'] ) ] : $this->info->promts->img_replace[ $this->getImageModelIndex( $data['imageModel'] ) ],
'action' => 'replaceImage',
'token' => $this->options->token,
'file' => file_get_contents( $path )
];
$args = $this->convertArrayToBoundaryData( $args );
$task = json_decode( $this->wpCurl( $args->args, [ 'Content-Type' => 'multipart/form-data; boundary=' . $args->boundary ] ) );
if( $task->task_id )
$data['attachments'][ $k ]['task_id'] = $attach['task_id'] = (int) $task->task_id;
}
}
}
if( isset( $task->image ) ){
if( $replace_id = (int) $this->loadFile( $task->image, (int) $attach['post_id'] ) ){
$data['attachments'][ $k ]['replace_id'] = (int) $replace_id;
if( isset( $attach['thumbnail'] ) ){
update_post_meta( (int) $attach['post_id'], '_thumbnail_id', $replace_id );
continue;
}
if( $src = wp_get_attachment_image_src( (int) $replace_id, ( isset( $attach['size'] ) ? $attach['size'] : 'full' ) ) ){
$post = get_post( (int) $attach['post_id'] );
if( strpos( $post->post_content, $attach['url'] ) === false )
continue;
$data['attachments'][ $k ]['replace_url'] = $src[0];
$post->post_content = str_replace( $attach['url'], $src[0], $post->post_content );
$post->post_content = str_replace( 'wp-image-'. (int) $attach['attach_id'], 'wp-image-'. (int) $replace_id, $post->post_content );
wp_update_post( [ 'ID' => (int) $attach['post_id'], 'post_content' => $post->post_content ] );
$this->replaceImageToElementor( $attach['post_id'], $attach['attach_id'], $replace_id, $src[0] );
}
}
} else
$data['attachments'][ $k ]['request']++;;
$break = true;
}
if( isset( $attach['replace_id'] ) || $attach['request'] > 4 )
$compleat++;
if( $break )
break;
}
if( $compleat >= count( $data['attachments'] ) )
$data['start'] = false;
update_option('aiImagesData', $data);
return $data;
}
private function replaceImageToElementor( $post_id, $attach_id, $replace_id, $url ){
$elementor = json_decode( get_post_meta($post_id, '_elementor_data', true ), true );
if( ! is_array( $elementor ) )
return;
foreach( $elementor as $index => $section ){
if( empty( $section['elements'] ) )
continue;
foreach( $section['elements'] as $key => $column ){
if( empty( $column['elements'] ) )
continue;
foreach( $column['elements'] as $k => $widget ){
if( ! empty( $widget['settings']['image']['id'] ) && (int) $widget['settings']['image']['id'] === (int) $attach_id ){
$elementor[ $index ]['elements'][ $key ]['elements'][ $k ]['settings']['image']['id'] = (int) $replace_id;
$elementor[ $index ]['elements'][ $key ]['elements'][ $k ]['settings']['image']['url'] = $url;
}
}
}
}
update_post_meta( $post_id, '_elementor_data', wp_slash( wp_json_encode( $elementor, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES ) ) );
}
public function removeQueueArticle(){
if( ! $this->checkNonce() || ! current_user_can('manage_options') || ! isset( $_POST['id'] ) )
return;
if( ! $autoGen = get_option('aiArticlesAutoGenData') )
$autoGen = [];
$id = (int) $_POST['id'];
if( isset( $autoGen['articles'][ $id ] ) )
unset( $autoGen['articles'][ $id ] );
update_option('aiArticlesAutoGenData', $autoGen);
}
private function getAttachIdByUrl( $url ) {
global $wpdb;
$dir = wp_upload_dir();
$url = preg_replace('/-\d+x\d+(?=\.\w+$)/', '', str_replace( $dir['baseurl'] . '/', '', $url ) );
return $wpdb->get_var( $wpdb->prepare('SELECT `post_id` FROM '. $wpdb->postmeta .' WHERE `meta_key`="_wp_attached_file" AND `meta_value`=%s', $url ) );
}
public function sign(){
if( ! $this->checkNonce() || ! current_user_can('manage_options') )
return;
if( ! empty( $_POST ) ){
$version = get_file_data(__DIR__ .'/index.php', [ 'version' => 'Version' ] );
@$_POST['v'] = $version['version'];
@$_POST['locale'] = get_locale();
@$_POST['action'] = sanitize_text_field( @$_POST['act'] );
wp_die( $this->wpCurl( $_POST ) );
}
wp_die('{"success":"false"}');
}
public function buy(){
if( ! $this->checkNonce() || ! current_user_can('manage_options') )
return;
wp_die( $this->wpcurl( [ 'token' => sanitize_text_field( $this->options->token ), 'action' => 'getPayUrl', 'recurring' => (int) $_POST['recurring'], 'promocode' => $_POST['promocode'], 'type' => sanitize_text_field( $_POST['type'] ), 'billing' => sanitize_text_field( $_POST['billing'] ), 'out_summ' => sanitize_text_field( $_POST['out_summ'] ), 'currency' => __('$', 'wp-ai-assistant'), 'locale' => get_locale() ] ) );
}
public function active(){
$this->wpcurl( [ 'host' => $this->getHost(), 'action' => 'active' ] );
}
public function inactive(){
$this->wpcurl( [ 'host' => $this->getHost(), 'action' => 'inactive' ] );
}
private function activation( $token, $cron = false ){
$this->wpcurl( [ 'host' => $this->getHost(), 'action' => 'activation', 'locale' => get_locale(), 'cron' => $cron, 'token' => sanitize_text_field( $token ) ] );
}
public function getBonus(){
if( ! $this->checkNonce() || ! current_user_can('manage_options') )
return;
wp_die( $this->wpcurl( [ 'method' => sanitize_text_field( $_POST['method'] ), 'wallet' => sanitize_text_field( $_POST['wallet'] ), 'info' => sanitize_text_field( $_POST['info'] ), 'token' => sanitize_text_field( $this->options->token ), 'action' => 'requestBonus' ] ) );
}
private function getInfo(){
$args = [ 'action' => 'getInfo', 'currency' => __('$', 'wp-ai-assistant'), 'token' => sanitize_text_field( @$_POST['token'] ?? @$this->options->token ) ];
if( isset( $_POST['promocode'] ) )
$args['promocode'] = sanitize_text_field( $_POST['promocode'] );
if( $info = json_decode( $this->wpcurl( $args, [], 3 ) ) )
return $info;
return false;
}
public function saveKey(){
if( ! $this->checkNonce() || ! current_user_can('manage_options') )
return;
if( isset( $_POST['key'] ) )
update_option('aiassist_gpt_key', sanitize_text_field( $_POST['key'] ) );
}
public function getStat(){
if( ! $this->checkNonce() || ! current_user_can('manage_options') )
return;
wp_die( $this->wpcurl( [ 'token' => sanitize_text_field( $this->options->token ), 'action' => 'getStat', 'host' => sanitize_text_field( $_REQUEST['host'] ), 'dateStart' => sanitize_text_field( $_REQUEST['dateStart'] ), 'dateEnd' => sanitize_text_field( $_REQUEST['dateEnd'] ) ] ) );
}
private function getHost(){
$host = get_option('siteurl');
if( $_SERVER['HTTPS'] == 'on' )
$host = str_replace('http://', 'https://', $host);
return $host;
}
private function aiArticlesAutoGen(){
if( $data = get_option('aiArticlesAutoGenData') ){
if( ! @$data['start'] || ! @$data['articles'] )
return $data;
if( $data['timezone'] )
date_default_timezone_set( $data['timezone'] );
$key = date('Ymd');
if( (int) $data['publishEveryDay'] ){
if( ! isset( $data['everyDayCounter'] ) )
$data['everyDayCounter'] = [ $key => true ];
$data['everyDayCounter'][ $key ] = true;
$everyDayCounter = count( $data['everyDayCounter'] );
if( $everyDayCounter <= $data['publishEveryDay'] && $everyDayCounter > 1 ){
update_option('aiArticlesAutoGenData', $data);
return $data;
}
if( $everyDayCounter > $data['publishEveryDay'] )
$data['everyDayCounter'] = [ $key => true ];
}
if( ! isset( $data['counter'][ $key ] ) )
$data['counter'] = [ $key => 0 ];
if( ! @$data['publishInDay'] )
$data['publishInDay'] = +100500;
if( $data['counter'][ $key ] > $data['publishInDay'] )
return $data;
$interval = $data['lastPublish'] + $data['minPublishInterval'] * 60;
if( $interval > time() )
return $data;
$this->setInfo();
$lang_id = $this->getDefaultLangId();
$break = false;
$data['publish'] = 0;
if( isset( $this->steps['promts']['multi_lang'] ) )
$lang_id = (int) $this->steps['promts']['multi_lang'];
$system_prompt = get_option('_ai_system_prompt');
if( $system_prompt === false )
$system_prompt = true;
foreach( $data['articles'] as $k => $article ){
if( $article['post_id'] )
$data['publish']++;
if( $break )
continue;
if(
( ! isset( $article['task_id'] ) && $data['publishInDay'] > $data['counter'][ $key ] )
||
( isset( $article['task_id'] ) && isset( $article['revision_id'] ) && ! isset( $article['post_id'] ) && ! get_post( $article['revision_id'] ) )
){
$break = true;
$args = [
'imageFormat' => 'jpg',
'author' => (int) $data['author'],
'thumb' => (bool) $data['thumb'],
'textModel' => $data['textModel'],
'imageModel' => $data['imageModel'],
'pictures' => $data['pictures'],
'max_pictures' => (int) $data['max_pictures'],
'lang_id' => $lang_id,
'promt' => isset( $this->steps['promts']['multi'][ $lang_id ] ) ? $this->steps['promts']['multi'][ $lang_id ] : $this->info->promts->multi[ $lang_id ],
'img_promt' => isset( $this->steps['promts']['img_auto'][ $this->getImageModelIndex( $data['imageModel'] ) ] ) ? $this->steps['promts']['img_auto'][ $this->getImageModelIndex( $data['imageModel'] ) ] : $this->info->promts->img_auto[ $this->getImageModelIndex( $data['imageModel'] ) ],
'kwd_promt' => isset( $this->steps['promts']['multi_keywords'][ $lang_id ] ) ? $this->steps['promts']['multi_keywords'][ $lang_id ] : $this->info->promts->multi_keywords[ $lang_id ],
'title' => isset( $this->steps['promts']['multi_title'][ $lang_id ] ) ? $this->steps['promts']['multi_title'][ $lang_id ] : $this->info->promts->multi_title[ $lang_id ],
'description' => isset( $this->steps['promts']['multi_desc'][ $lang_id ] ) ? $this->steps['promts']['multi_desc'][ $lang_id ] : $this->info->promts->multi_desc[ $lang_id ],
'theme' => $article['theme'],
'keywords' => $article['keywords'],
'action' => 'addAutoTask',
'token' => $this->options->token,
'sPrompt' => $system_prompt ? 1 : 0,
];
$task = json_decode( $this->wpCurl( $args ) );
if( $task->task_id ){
if( $revision_id = wp_insert_post( [ 'post_type' => 'wpai', 'post_title' => $this->validText( $article['keywords'] ), 'post_author' => (int) $data['author'] ] ) ){
$data['counter'][ $key ]++;
$data['articles'][ $k ]['task_id'] = $task->task_id;
$data['articles'][ $k ]['revision_id'] = $revision_id;
}
}
}
if( isset( $article['task_id'] ) && isset( $article['revision_id'] ) && ! isset( $article['post_id'] ) ){
$task = json_decode( $this->wpCurl( [ 'action' => 'getTask', 'id' => $article['task_id'], 'host' => $this->getHost(), 'token' => $this->options->token ] ) );
if( ! @$data['articles'][ $k ]['check'] || isset( $task->content ) )
$data['articles'][ $k ]['check'] = 0;
if( ! isset( $task->content ) )
$data['articles'][ $k ]['check']++;
if( $data['articles'][ $k ]['check'] > 120 )
$data['articles'][ $k ]['check'] = 0;
if( $data['articles'][ $k ]['check'] > 60 )
continue;
$break = true;
if( isset( $task->content ) ){
$args = [
'ID' => $article['revision_id'],
'post_type' => 'post',
'post_status' => $data['draft'] ? 'draft' : 'publish',
'post_title' => $this->validText( $task->header ),
'post_content' => $this->validText( $task->content, 'content' ),
'post_category' => [ (int) $article['cat_id'] ],
];
if( $post_id = wp_update_post( $args ) ){
$data['publish']++;
$data['lastPublish'] = time();
$data['articles'][ $k ]['post_id'] = $post_id;
$this->updatePostMeta( $post_id, $task->title, $task->description );
if( $task->thumb ){
if( $thumb_id = (int) $this->loadFile( $task->thumb, $post_id ) ){
set_post_thumbnail( $post_id, $thumb_id );
wp_update_post( [ 'ID' => $thumb_id, 'post_title' => $this->validText( $task->header ) ] );
update_post_meta( $thumb_id, '_wp_attachment_image_alt', $this->validText( $task->header ) );
}
}
if( $task->images ){
$task->alts = (array) $task->alts;
foreach( $task->images as $k => $src ){
if( strpos($task->content, $src) === false )
continue;
if( $img_id = (int) $this->loadFile( $src, $post_id ) ){
$image = wp_get_attachment_image( $img_id, 'full', false, [ 'class' => 'size-full wp-image-'. $img_id .' aligncenter', 'alt' => $this->clearTitle( $task->alts[ $k ] ), 'title' => $this->clearTitle( $task->alts[ $k ] ) ] );
$task->content = str_replace($src, $image, $task->content);
}
}
wp_update_post( [ 'ID' => $post_id, 'post_content' => $this->validText( $task->content, 'content' ) ] );
}
}
}
}
}
if( $data['publishInDay'] == 100500 )
unset( $data['publishInDay'] );
update_option('aiArticlesAutoGenData', $data);
}
return $data;
}
public function autoGenOptions(){
if( ! $this->checkNonce() || ! current_user_can('manage_options') )
return;
$data = get_option('aiArticlesAutoGenData');
$data['author'] = (int) $_POST['author'];
$data['draft'] = (bool) $_POST['draft'];
$data['rand'] = (bool) $_POST['rand'];
$data['thumb'] = (bool) $_POST['thumb'];
$data['publishInDay'] = (int) $_POST['publishInDay'];
$data['publishEveryDay'] = (int) $_POST['publishEveryDay'];
$data['minPublishInterval'] = (int) $_POST['minPublishInterval'];
$data['timezone'] = sanitize_text_field( $_POST['timezone'] );
$data['pictures'] = sanitize_text_field( $_POST['pictures'] );
$data['max_pictures'] = (int) $_POST['max_pictures'];
$data['imageModel'] = sanitize_text_field( $_POST['imageModel'] );
$data['textModel'] = sanitize_text_field( $_POST['textModel'] );
update_option('aiArticlesAutoGenData', $data);
wp_die( json_encode( $data ) );
}
public function stopArticlesGen(){
if( ! $this->checkNonce() || ! current_user_can('manage_options') )
return;
$data = get_option('aiArticlesAutoGenData');
$data['start'] = false;
update_option('aiArticlesAutoGenData', $data);
wp_die( json_encode( $data ) );
}
public function startArticlesGen(){
if( ! $this->checkNonce() || ! current_user_can('manage_options') )
return;
$data = get_option('aiArticlesAutoGenData');
$data['start'] = true;
update_option('aiArticlesAutoGenData', $data);
wp_die( json_encode( $data ) );
}
public function clearArticlesGen(){
if( ! $this->checkNonce() || ! current_user_can('manage_options') )
return;
$args = get_option('aiArticlesAutoGenData');
$data = [ 'start' => true, 'count' => 0, 'publish' => 0, 'articles' => [] ];
if( isset( $args['publishInDay'] ) )
$data['publishInDay'] = (int) $args['publishInDay'];
if( isset( $args['publishEveryDay'] ) )
$data['publishEveryDay'] = (int) $args['publishEveryDay'];
if( isset( $args['minPublishInterval'] ) )
$data['minPublishInterval'] = (int) $args['minPublishInterval'];
if( isset( $args['author'] ) )
$data['author'] = (int) $args['author'];
if( isset( $args['draft'] ) )
$data['draft'] = (bool) $args['draft'];
if( isset( $args['rand'] ) )
$data['rand'] = (bool) $args['rand'];
if( isset( $args['thumb'] ) )
$data['thumb'] = (bool) $args['thumb'];
if( isset( $args['pictures'] ) )
$data['pictures'] = $args['pictures'];
if( isset( $args['max_pictures'] ) )
$data['max_pictures'] = (int) $args['max_pictures'];
if( isset( $args['imageModel'] ) )
$data['imageModel'] = sanitize_text_field( $args['imageModel'] );
if( isset( $args['textModel'] ) )
$data['textModel'] = sanitize_text_field( $args['textModel'] );
if( isset( $args['timezone'] ) )
$data['timezone'] = sanitize_text_field( $args['timezone'] );
update_option('aiArticlesAutoGenData', $data);
wp_die( json_encode( $data ) );
}
public function initArticlesGen(){
if( ! $this->checkNonce() || ! current_user_can('manage_options') )
return;
if( $articles = $_POST['articles'] ){
$args = get_option('aiArticlesAutoGenData');
$data = [ 'start' => true, 'count' => 0, 'publish' => 0, 'articles' => [] ];
if( isset( $args['publishInDay'] ) )
$data['publishInDay'] = (int) $args['publishInDay'];
if( isset( $args['publishEveryDay'] ) )
$data['publishEveryDay'] = (int) $args['publishEveryDay'];
if( isset( $args['minPublishInterval'] ) )
$data['minPublishInterval'] = (int) $args['minPublishInterval'];
if( isset( $args['timezone'] ) )
$data['timezone'] = $args['timezone'];
if( isset( $args['author'] ) )
$data['author'] = (int) $args['author'];
if( isset( $args['draft'] ) )
$data['draft'] = (int) $args['draft'];
if( isset( $args['rand'] ) )
$data['rand'] = (int) $args['rand'];
if( isset( $args['thumb'] ) )
$data['thumb'] = (int) $args['thumb'];
if( isset( $args['pictures'] ) )
$data['pictures'] = $args['pictures'];
if( isset( $args['max_pictures'] ) )
$data['max_pictures'] = (int) $args['max_pictures'];
if( ! empty( $args['articles'] ) )
$data['articles'] = $args['articles'];
$data['textModel'] = sanitize_text_field( $_POST['textModel'] );
$data['imageModel'] = sanitize_text_field( $_POST['imageModel'] );
foreach( $articles as $cat_id => $items ){
foreach( $items as $item ){
if( $item['theme'] )
$data['articles'][] = [ 'theme' => $item['theme'], 'keywords' => $item['keywords'], 'cat_id' => $cat_id ];
}
}
if( $data['rand'] )
shuffle( $data['articles'] );
$data['count'] = count( $data['articles'] );
update_option('aiArticlesAutoGenData', $data);
wp_die( json_encode( $data ) );
}
wp_die('{"success":"false"}');
}
public function rewriteOptions(){
if( ! $this->checkNonce() || ! current_user_can('manage_options') )
return;
$data = get_option('aiRewritesData');
$data['thumb'] = (bool) $_POST['thumb'];
$data['split'] = (int) $_POST['split'];
$data['author'] = (int) $_POST['author'];
$data['draft'] = (bool) $_POST['draft'];
$data['pictures'] = sanitize_text_field( $_POST['pictures'] );
$data['excude_h1'] = (bool) $_POST['excude_h1'];
$data['excude_title'] = (bool) $_POST['excude_title'];
$data['excude_desc'] = (bool) $_POST['excude_desc'];
$data['max_pictures'] = (int) $_POST['max_pictures'];
$data['imageModel'] = sanitize_text_field( $_POST['imageModel'] );
$data['textModel'] = sanitize_text_field( $_POST['textModel'] );
update_option('aiRewritesData', $data);
wp_die( json_encode( $data ) );
}
private function aiRewrite(){
if( $data = get_option('aiRewritesData') ){
if( ! @$data['start'] || ! @$data['posts'] )
return $data;
$break = false;
if( $data['posts'] ){
$data['counter'] = 0;
$this->setInfo();
$lang_id = $this->getDefaultLangId();
if( isset( $this->steps['promts']['rewrite_lang'] ) )
$lang_id = (int) $this->steps['promts']['rewrite_lang'];
foreach( $data['posts'] as $k => $item ){
if( @$item['post_id'] )
$data['counter']++;
if( $break )
continue;
if( ! isset( $item['task_id'] ) ){
$break = true;
$args = [
'imageFormat' => 'jpg',
'split' => $data['split'],
'author' => (int) $data['author'],
'thumb' => (bool) $data['thumb'],
'excude_h1' => (bool) $data['excude_h1'],
'excude_title' => (bool) $data['excude_title'],
'excude_desc' => (bool) $data['excude_desc'],
'textModel' => $data['textModel'],
'imageModel' => $data['imageModel'],
'pictures' => $data['pictures'],
'max_pictures' => (int) $data['max_pictures'],
'lang_id' => $lang_id,
'promt' => isset( $this->steps['promts']['rewrite'][ $lang_id ] ) ? $this->steps['promts']['rewrite'][ $lang_id ] : $this->info->promts->rewrite[ $lang_id ],
'img_promt' => isset( $this->steps['promts']['img_rewrite'][ $this->getImageModelIndex( $data['imageModel'] ) ] ) ? $this->steps['promts']['img_rewrite'][ $this->getImageModelIndex( $data['imageModel'] ) ] : $this->info->promts->img_rewrite[ $this->getImageModelIndex( $data['imageModel'] ) ],
'promt_header' => isset( $this->steps['promts']['rewrite_header'][ $lang_id ] ) ? $this->steps['promts']['rewrite_header'][ $lang_id ] : $this->info->promts->rewrite_header[ $lang_id ],
'promt_title' => isset( $this->steps['promts']['rewrite_title'][ $lang_id ] ) ? $this->steps['promts']['rewrite_title'][ $lang_id ] : $this->info->promts->rewrite_title[ $lang_id ],
'promt_desc' => isset( $this->steps['promts']['rewrite_desc'][ $lang_id ] ) ? $this->steps['promts']['rewrite_desc'][ $lang_id ] : $this->info->promts->rewrite_desc[ $lang_id ],
'action' => 'addRewrite',
'token' => $this->options->token,
];
if( (int) $item['id'] ){
if( $post = get_post( $item['id'] ) ){
$args['post_title'] = $post->post_title;
$args['post_content'] = $post->post_content;
}
$meta = $this->getPostMeta( $item['id'] );
if( isset( $meta->title ) )
$args['meta_title'] = $meta->title;
if( isset( $meta->description ) )
$args['meta_description'] = $meta->description;
}
if( isset( $item['url'] ) )
$args['url'] = $item['url'];
$task = json_decode( $this->wpCurl( $args ) );
if( $task->task_id )
$data['posts'][ $k ]['task_id'] = $task->task_id;
}
if( isset( $item['task_id'] ) && ! isset( $item['post_id'] ) ){
$task = json_decode( $this->wpCurl( [ 'action' => 'getTask', 'id' => $item['task_id'], 'host' => $this->getHost(), 'token' => $this->options->token ] ) );
if( ! @$data['posts'][ $k ]['check'] || isset( $task->content ) )
$data['posts'][ $k ]['check'] = 0;
if( ! isset( $task->content ) )
$data['posts'][ $k ]['check']++;
if( $data['posts'][ $k ]['check'] > 120 )
$data['posts'][ $k ]['check'] = 0;
if( $data['posts'][ $k ]['check'] > 60 )
continue;
$break = true;
if( isset( $task->content ) ){
if( (int) $item['id'] ){
$post_id = $item['id'];
$revision = get_post( $post_id );
$args = [
'post_title' => $this->validText( $revision->post_title ),
'post_content' => $this->validText( $revision->post_content, 'content' ),
'post_status' => 'inherit',
'post_type' => 'revision',
'post_parent' => $post_id,
'post_author' => (int) $data['author'],
];
$revision_id = wp_insert_post( $args );
$data['posts'][ $k ]['revision_id'] = $revision_id;
$meta = $this->getPostMeta( $post_id );
if( $meta->title )
update_post_meta( $post_id, '_aiassist_revision_meta_title', $meta->title );
if( $meta->description )
update_post_meta( $post_id, '_aiassist_revision_meta_description', $meta->description );
if( $thumbnail_id = get_post_thumbnail_id( $post_id ) )
update_post_meta( $post_id, '_aiassist_revision_thumbnail_id', $thumbnail_id );
wp_update_post( [ 'ID' => $post_id, 'post_title' => $this->validText( $task->post_title ), 'post_content' => $this->validText( $task->content, 'content' ), 'post_author' => (int) $data['author'] ] );
} elseif( (int) $item['revision_id'] ){
$args = [
'ID' => (int) $item['revision_id'],
'post_type' => 'post',
'post_status' => (bool) $data['draft'] ? 'draft' : 'publish',
'post_title' => $this->validText( $task->post_title ),
'post_content' => $this->validText( $task->content, 'content' ),
'post_category' => [ (int) $item['cat_id'] ],
'post_author' => (int) $data['author'],
];
$post_id = wp_update_post( $args );
} else {
$args = [
'post_status' => (bool) $data['draft'] ? 'draft' : 'publish',
'post_title' => $this->validText( $task->post_title ),
'post_content' => $this->validText( $task->content, 'content'),
'post_category' => [ (int) $item['cat_id'] ],
'post_author' => (int) $data['author'],
];
$post_id = wp_insert_post( $args );
}
if( $post_id ){
$data['counter']++;
$data['posts'][ $k ]['post_id'] = $post_id;
$this->updatePostMeta( $post_id, $task->meta_title, $task->meta_description );
if( $task->thumb ){
if( $thumb_id = (int) $this->loadFile( $task->thumb, $post_id ) ){
set_post_thumbnail( $post_id, $thumb_id );
wp_update_post( [ 'ID' => $thumb_id, 'post_title' => $this->validText( $task->post_title ) ] );
update_post_meta( $thumb_id, '_wp_attachment_image_alt', $this->validText( $task->post_title ) );
}
}
if( $task->images ){
$task->alts = (array) $task->alts;
foreach( $task->images as $k => $src ){
if( strpos($task->content, $src) === false )
continue;
if( $img_id = (int) $this->loadFile( $src, $post_id ) ){
$image = wp_get_attachment_image( $img_id, 'full', false, [ 'class' => 'size-full wp-image-'. $img_id .' aligncenter', 'alt' => $this->clearTitle( $task->alts[ $k ] ) .' фото', 'title' => $this->clearTitle( $task->alts[ $k ] ) ] );
$task->content = str_replace($src, $image, $task->content);
}
}
}
wp_update_post( [ 'ID' => $post_id, 'post_title' => $this->validText( $task->post_title ), 'post_content' => $this->validText( $task->content, 'content' ) ] );
}
}
}
}
}
update_option('aiRewritesData', $data);
}
return $data;
}
public function initRewrite(){
global $wpdb;
if( ! $this->checkNonce() || ! current_user_can('manage_options') )
return;
$args = get_option('aiRewritesData');
$args['start'] = true;
$args['thumb'] = (bool) $_POST['thumb'];
$args['split'] = (int) $_POST['split'];
$args['author'] = (int) $_POST['author'];
$args['draft'] = (bool) $_POST['draft'];
$args['pictures'] = sanitize_text_field( $_POST['pictures'] );
$args['excude_h1'] = (bool) $_POST['excude_h1'];
$args['excude_title'] = (bool) $_POST['excude_title'];
$args['excude_desc'] = (bool) $_POST['excude_desc'];
$args['max_pictures'] = (int) $_POST['max_pictures'];
$args['imageModel'] = sanitize_text_field( $_POST['imageModel'] );
$args['textModel'] = sanitize_text_field( $_POST['textModel'] );
if( $_POST['cats'] ){
$sql = 'SELECT
p.`ID`, p.`post_title`
FROM
'. $wpdb->posts .' p
INNER JOIN
'. $wpdb->term_relationships .' tr ON ( p.ID = tr.object_id )
INNER JOIN
'. $wpdb->term_taxonomy .' tt ON ( tr.term_taxonomy_id = tt.term_taxonomy_id )
WHERE
tt.taxonomy = "category"
AND
tt.term_id IN ('. implode( ',', array_map( 'intval', $_POST['cats'] ) ) .')
AND
( p.`post_status`="publish" OR p.`post_status`="private" OR p.`post_status`="pending" OR p.`post_status`="draft" OR p.`post_status`="future" )';
if( $posts = $wpdb->get_results( $sql ) ){
foreach( $posts as $post )
$args['posts'][] = [ 'id' => (int) $post->ID, 'title' => $post->post_title ];
}
}
if( $_POST['types'] ){
$types = array_map('sanitize_key', (array) $_POST['types']);
$sql = 'SELECT
`ID`, `post_title`
FROM
'. $wpdb->posts .'
WHERE
`post_type` IN ('. implode( ',', array_fill( 0, count( $types ), '%s' ) ) .')
AND
( `post_status`="publish" OR `post_status`="private" OR `post_status`="pending" OR `post_status`="draft" OR `post_status`="future" )';
if( $posts = $wpdb->get_results( $wpdb->prepare( $sql, ...$types ) ) ){
foreach( $posts as $post )
$args['posts'][] = [ 'id' => (int) $post->ID, 'title' => $post->post_title ];
}
}
if( $_POST['links'] ){
$posts_ids = [];
foreach( $_POST['links'] as $cat_id => $links ){
foreach( $links as $link ){
if( stripos( $link, $_SERVER['HTTP_HOST'] ) !== false ){
if( $id = $this->url_to_postid( $link ) )
$posts_ids[] = $id;
} else {
$args['posts'][] = [ 'url' => $link, 'cat_id' => $cat_id, 'revision_id' => wp_insert_post( [ 'post_type' => 'wpai', 'post_title' => sanitize_url( $link ), 'post_author' => (int) $args['author'] ] ) ];
}
}
}
if( $posts_ids ){
$posts_ids = array_map( 'absint', $posts_ids );
if( $posts = $wpdb->get_results( $wpdb->prepare('SELECT `ID`, `post_title` FROM '. $wpdb->posts .' WHERE `ID` IN ('. implode( ',', array_fill( 0, count( $posts_ids ), '%d' ) ) .')', ...$posts_ids ) ) ){
foreach( $posts as $post )
$args['posts'][] = [ 'id' => (int) $post->ID, 'title' => $post->post_title ];
}
}
}
update_option('aiRewritesData', $args);
wp_die( json_encode( $args ) );
}
public function postRestore(){
if( ! $this->checkNonce() || ! current_user_can('manage_options') )
return;
if( ! ( $revision_id = (int) $_POST['revision_id'] ) || ! ( $post_id = (int) $_POST['post_id'] ) )
return;
wp_restore_post_revision( $revision_id, $post_id );
$meta_title = get_post_meta( $post_id, '_aiassist_revision_meta_title', true );
$meta_description = get_post_meta( $post_id, '_aiassist_revision_meta_description', true );
$this->updatePostMeta( $post_id, $meta_title, $meta_description );
if( $attachment_id = get_post_meta( $post_id, '_aiassist_revision_thumbnail_id', true ) )
set_post_thumbnail( $post_id, $attachment_id );
if( $args = get_option('aiRewritesData') ){
foreach( $args['posts'] as $k => $post ){
if( $post['post_id'] == $post_id )
$args['posts'][ $k ]['restore'] = true;
}
update_option('aiRewritesData', $args);
}
}
public function startRewrite(){
if( ! $this->checkNonce() || ! current_user_can('manage_options') )
return;
$data = get_option('aiRewritesData');
$data['start'] = true;
update_option('aiRewritesData', $data);
wp_die( json_encode( $data ) );
}
public function stopRewrite(){
if( ! $this->checkNonce() || ! current_user_can('manage_options') )
return;
$data = get_option('aiRewritesData');
$data['start'] = false;
update_option('aiRewritesData', $data);
wp_die( json_encode( $data ) );
}
public function clearRewrite(){
if( ! $this->checkNonce() || ! current_user_can('manage_options') )
return;
$args = get_option('aiRewritesData');
$data = [ 'start' => false, 'posts' => [] ];
if( isset( $args['split'] ) )
$data['split'] = (int) $args['split'];
if( isset( $args['author'] ) )
$data['author'] = (int) $args['author'];
if( isset( $args['thumb'] ) )
$data['thumb'] = (bool) $args['thumb'];
if( isset( $args['draft'] ) )
$data['draft'] = (bool) $args['draft'];
if( isset( $args['excude_h1'] ) )
$data['excude_h1'] = (bool) $args['excude_h1'];
if( isset( $args['excude_title'] ) )
$data['excude_title'] = (bool) $args['excude_title'];
if( isset( $args['excude_desc'] ) )
$data['excude_desc'] = (bool) $args['excude_desc'];
if( isset( $args['pictures'] ) )
$data['pictures'] = sanitize_text_field( $args['pictures'] );
if( isset( $args['max_pictures'] ) )
$data['max_pictures'] = (int) $args['max_pictures'];
if( isset( $args['imageModel'] ) )
$data['imageModel'] = sanitize_text_field( $args['imageModel'] );
if( isset( $args['textModel'] ) )
$data['textModel'] = sanitize_text_field( $args['textModel'] );
update_option('aiRewritesData', $data);
wp_die( json_encode( $data ) );
}
private function updatePostMeta( $post_id, $title, $description ){
global $wpdb;
update_post_meta( $post_id, '_yoast_wpseo_title', sanitize_text_field( wp_unslash( $title ) ) );
update_post_meta( $post_id, '_yoast_wpseo_metadesc', sanitize_text_field( wp_unslash( $description ) ) );
update_post_meta( $post_id, 'rank_math_title', sanitize_text_field( wp_unslash( $title ) ) );
update_post_meta( $post_id, 'rank_math_description', sanitize_text_field( wp_unslash( $description ) ) );
update_post_meta( $post_id, '_title', sanitize_text_field( wp_unslash( $title ) ) );
update_post_meta( $post_id, '_description', sanitize_text_field( wp_unslash( $description ) ) );
update_post_meta( $post_id, '_aioseo_title', sanitize_text_field( wp_unslash( $title ) ) );
update_post_meta( $post_id, '_aioseo_description', sanitize_text_field( wp_unslash( $description ) ) );
if( defined('AIOSEO_VERSION') || function_exists('aioseo') )
$wpdb->replace( $wpdb->prefix . 'aioseo_posts', [ 'post_id' => $post_id, 'title' => $title, 'description' => $description ], [ '%d', '%s', '%s' ] );
}
private function getPostMeta( $post_id ){
if( defined('WPSEO_VERSION') )
return (object) [ 'title' => get_post_meta( $post_id, '_yoast_wpseo_title', true ), 'description' => get_post_meta( $post_id, '_yoast_wpseo_metadesc', true ) ];
if( defined('AIOSEOP_VERSION') || defined('AIOSEO_VERSION') || class_exists( 'AIOSEO\Plugin\Common\Main\Main' ) )
return (object) [ 'title' => get_post_meta( $post_id, '_aioseo_title', true ), 'description' => get_post_meta( $post_id, '_aioseo_description', true ) ];
if( defined( 'RANK_MATH_VERSION' ) || function_exists('rank_math') || class_exists( 'RankMath' ) )
return (object) [ 'title' => get_post_meta( $post_id, 'rank_math_title', true ), 'description' => get_post_meta( $post_id, 'rank_math_description', true ) ];
if( class_exists('All_in_One_SEO_Pack') )
return (object) [ 'title' => get_post_meta( $post_id, '_title', true ), 'description' => get_post_meta( $post_id, '_description', true ) ];
return (object) [ 'title' => null, 'description' => null ];
}
public function saveTranslateImagesPromts(){
if( ! $this->checkNonce() || ! current_user_can('edit_posts') )
return;
if( isset( $_POST['promts'] ) ){
$this->setInfo();
if( ! ( $data = $this->steps ) )
$data = [];
foreach( $_POST['promts'] as $item )
$data[ sanitize_text_field( $item['act'] ) ] = wp_kses_post( wp_unslash( $item['val'] ) );
if( update_option('_aiassist_generator', $data) )
wp_die('{"success":"true"}');
}
wp_die('{"success":"false"}');
}
public function saveStep(){
if( ! $this->checkNonce() || ! current_user_can('edit_posts') )
return;
if( isset( $_POST['val'] ) && isset( $_POST['act'] ) ){
if( is_array( $_POST['val'] ) )
$val = array_map(function( $v ){
return wp_unslash( $v );
}, $_POST['val']);
else
$val = wp_kses_post( wp_unslash( $_POST['val'] ) );
$act = sanitize_text_field( $_POST['act'] );
$this->setInfo();
if( ! ( $data = $this->steps ) )
$data = [];
$data[ $act ] = $val;
update_option('_aiassist_generator', $data);
wp_die('{"success":"true"}');
}
wp_die('{"success":"false"}');
}
public function resetStep(){
$this->setInfo();
update_option('_aiassist_generator', [ 'promts' => ( @$this->steps['promts'] ? $this->steps['promts'] : $this->info->promts ) ] );
}
public function loadImage(){
if( ! $this->checkNonce() || ! current_user_can('edit_posts') )
return;
if( $post_id = (int) $_POST['post_id'] ){
$url = sanitize_url( $_POST['image']['src'] );
$title = sanitize_text_field( $_POST['image']['title'] );
if( $url && $title ){
if( $img_id = (int) $this->loadFile( $url, $post_id ) ){
$image = wp_get_attachment_image( $img_id, 'full', false, [ 'class' => 'size-full wp-image-'. $img_id .' aligncenter', 'alt' => $this->clearTitle( $title ), 'title' => $this->clearTitle( $title ) ] );
wp_die( json_encode( [ 'id' => $img_id, 'url' => wp_get_attachment_url( $img_id ), 'image' => $image ] ) );
}
}
}
wp_die('{"success":"false"}');
}
public function clearContent(){
if( ! $this->checkNonce() || ! current_user_can('edit_posts') )
return;
$this->resetStep();
}
public function saveContent(){
if( ! $this->checkNonce() || ! current_user_can('edit_posts') )
return;
if( $post_id = (int) $_POST['post_id'] ){
if( ! trim( $_POST['header'] ) ){
if( preg_match('/