admin_url('admin-ajax.php'), 'nonce' => $nonce )); } public function save_token(){ if (!current_user_can('manage_options')) { wp_die('You do not have sufficient permissions to access this page.'); } if ( ! isset( $_POST['nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash ( $_POST['nonce'] ) ) , 'autoblog-ai-nonce' ) ){ wp_die('Nonce verification failed!'); } $token = sanitize_text_field($_POST['token']); add_user_meta(get_current_user_id(), 'autoblog-ai_token', $token, true); wp_die(); } public function revoke_token(){ if (!current_user_can('manage_options')) { wp_die('You do not have sufficient permissions to access this page.'); } if ( ! isset( $_POST['nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash ( $_POST['nonce'] ) ) , 'autoblog-ai-nonce' ) ){ wp_die('Nonce verification failed!'); } delete_user_meta(get_current_user_id(), 'autoblog-ai_token'); wp_die(); } public function get_projects(){ if (!current_user_can('manage_options')) { wp_die('You do not have sufficient permissions to access this page.'); } if ( ! isset( $_GET['nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash ( $_GET['nonce'] ) ) , 'autoblog-ai-nonce' ) ){ wp_die('Nonce verification failed!'); } $url = 'https://airticle-flow.com/api/projects'; $token = get_user_meta(get_current_user_id(), 'autoblog-ai_token', true); $headers = array( 'Authorization' => 'Bearer ' . $token ); $response = wp_remote_get($url, array( 'headers' => $headers, 'method' => 'GET' )); if (!is_wp_error($response)) { $body = wp_remote_retrieve_body($response); $body = json_decode($body); echo wp_json_encode($body); } wp_die(); } public function publish_articles(){ if (!current_user_can('manage_options')) { wp_die('You do not have sufficient permissions to access this page.'); } if ( ! isset( $_POST['nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash ( $_POST['nonce'] ) ) , 'autoblog-ai-nonce' ) ){ wp_die('Nonce verification failed!'); } $projectId = (int) $_POST['project_id']; $category_id = (int) $_POST['category_id']; $schedule = sanitize_text_field($_POST['schedule']); $planning = $_POST['planning']; //this is a nested array, all fields will be sanitized when accessed $post_status = "publish"; if($schedule === "draft"){ $post_status = "draft"; } if($schedule === "drip"){ if(!empty($planning)){ $post_status = "future"; $period_time = (int) $_POST['planning']['period_time']; $period_type = sanitize_text_field($_POST['planning']['period_type']); $frequency_type = sanitize_text_field($_POST['planning']['frequency_type']); if($frequency_type === "static"){ $frequency_hours = (int) $_POST['planning']['frequency_hours']; $frequency_minutes = (int) $_POST['planning']['frequency_minutes']; } } else{ wp_die('Malformed Request', 'Malformed Request', array( 'response' => 400 )); } } $url = 'https://airticle-flow.com/api/projects/'.$projectId.'/articles'; $token = get_user_meta(get_current_user_id(), 'autoblog-ai_token', true); $headers = array( 'Authorization' => 'Bearer ' . $token ); $response = wp_remote_get($url, array( 'headers' => $headers, 'method' => 'GET' )); $body = wp_remote_retrieve_body($response); $articles = json_decode($body); $postIds = []; $initialDay = new DateTime(); foreach ($articles as $index => $article){ $post_data = array( 'post_title' => sanitize_text_field($article->title), 'post_content' => wp_kses_post($article->content), 'post_status' => $post_status, 'post_author' => get_current_user_id(), ); if(!empty($category_id)){ $post_data['post_category'] = array( $category_id ); } if($post_status === "future"){ $post_date = $initialDay->modify("+$period_time $period_type"); if($frequency_type === "static"){ $post_date->setTime($frequency_hours, $frequency_minutes); } else{ $randomHours = rand(0, 23); $randomMinutes = rand(0, 59); $post_date->setTime($randomHours, $randomMinutes); } $post_data['post_date'] = $post_date->format("Y-m-d H:i:s"); } $postIds[] = wp_insert_post( $post_data ); } echo wp_json_encode($postIds); wp_die(); } public function addImage(){ if (!current_user_can('manage_options')) { wp_die('You do not have sufficient permissions to access this page.'); } if ( ! isset( $_POST['nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash ( $_POST['nonce'] ) ) , 'autoblog-ai-nonce' ) ){ wp_die('Nonce verification failed!'); } $post_id = (int) $_POST['post_id']; $post = get_post($post_id); $content = $post->post_content; $this->setFeaturedImage($content, $post_id); } private function setFeaturedImage($html, $post_id){ if ( ! class_exists('Autoblogai_DomParser') ) { require AUTOBLOGAI_PLUGIN_PATH . 'includes/Autoblogai_DomParser.php'; } $domParser = new Autoblogai_DomParser(); $image_url = $domParser->get_first_img_src($html); if(!empty($image_url)){ $media = media_sideload_image($image_url, $post_id); // Check if there was an error sideloading the image. if (is_wp_error($media)) { return; } $attachments = get_posts(array( 'post_type' => 'attachment', 'numberposts' => 1, 'post_status' => 'any', 'post_parent' => $post_id, 'orderby' => 'post_date', 'order' => 'DESC' )); if ($attachments) { $attachment_id = $attachments[0]->ID; // Set the post thumbnail. set_post_thumbnail($post_id, $attachment_id); } } } }