load( $url ); if ( ! $html ) { return false; } // Check if the $host contain 'bing.com/news' then use the news card selector. if ( strpos( $host, 'bing.com/news' ) !== false ) { $results = $html->find( 'div[class="news-card newsitem cardcommon"] a.title' ); } else { $results = $html->find( 'li.b_algo h2 a' ); } // Check if the results are empty then return false. if ( empty( (array) $results ) ) { return false; } $count = 0; foreach ( $results as $a ) { $title = wp_strip_all_tags( $a->plaintext ); $link = $a->href; // Decode the Bing URL if it contains 'bing.com/ck/'. if ( strpos( $link, 'bing.com/ck/' ) !== false ) { $link = aicw_decode_bing_url( $link ); } else { $link = esc_url( $link ); } if ( empty( $title ) || empty( $link ) || ! self::is_relevant( $title, $search_keyword ) ) { continue; } // Check if the title already exists. $post_exists = aicw_get_post_by_title( $title ); if ( $post_exists && ( get_post_meta( $post_exists, '_aicw_content_source_link', true ) === $link ) ) { continue; } // Create a new post. $post_id = wp_insert_post( array( 'post_type' => 'aicw_post', 'post_title' => $title, 'post_content' => '', 'post_status' => 'pending', 'post_author' => get_post_field( 'post_author', $campaign_id ), ) ); if ( is_wp_error( $post_id ) ) { continue; } // Add the post meta. update_post_meta( $post_id, '_aicw_campaign_id', $campaign_id ); update_post_meta( $post_id, '_aicw_content_source_link', $link ); // Update the counter. ++$count; // If the counter is greater than or equal to the target then break the loop. if ( $count >= $posts_needed ) { ai_content_writer()->create_log( sprintf( /* translators: 1: Number of posts generated. */ __( '%d posts titles and source links generated successfully.', 'ai-content-writer' ), $count ), 'success', $campaign_id ); break; } } // If the loop executed successfully, return true. return $count > 0; } /** * Whether a search result title shares at least one word with the keyword. * * @param string $title The result title. * @param string $keyword The searched keyword. * * @since 2.6.0 * @return bool */ private static function is_relevant( $title, $keyword ) { $title = mb_strtolower( $title ); $words = preg_split( '/[\s,.\-]+/u', mb_strtolower( $keyword ), -1, PREG_SPLIT_NO_EMPTY ); $words = array_filter( $words, function ( $word ) { return mb_strlen( $word ) >= 3; } ); if ( empty( $words ) ) { return true; } foreach ( $words as $word ) { if ( false !== mb_strpos( $title, $word ) ) { return true; } } return false; } /** * Generate content. * This function is responsible for generating content based on the source link. * * @param int $post_id The ID of the post for which content is being generated. * @param string $title The title of the post. * @param string $keyword The keyword that will be used to generate the content. * * @since 1.7.0 * @return bool Whether the content was generated successfully or not. */ public static function generate_content( $post_id, $title = '', $keyword = '' ) { return SourceArticle::import( $post_id, $title, $keyword ); } }