*/
add_action('wp_head', 'check_default_options_and_function');
add_action('admin_menu', 'fun_add_solr_settings');
add_action('admin_init', 'wpsolr_admin_init');
add_action('wp_enqueue_scripts', 'my_enqueue');
// Register WpSolr widgets when current theme's search is used.
if (WPSOLR_Global::getOption()->get_search_is_use_current_theme_search_template()) {
    require_once 'classes/ui/widget/WPSOLR_Widget.php';
    WPSOLR_Widget::Autoload();
}
if (is_admin()) {
    /*
     * Register metabox
     */
    require_once 'classes/metabox/wpsolr-metabox.php';
    WPSOLR_Metabox::register();
}
/*
 * Display Solr errors in admin when a save on a post can't index to Solr
 */
function solr_post_save_admin_notice()
{
    if ($out = get_transient(get_current_user_id() . 'error_solr_post_save_admin_notice')) {
        delete_transient(get_current_user_id() . 'error_solr_post_save_admin_notice');
        echo "<div class=\"error wpsolr_admin_notice_error\"><p>(WPSOLR) Error while indexing this post/page in Solr:<br><br>{$out}</p></div>";
    }
    if ($out = get_transient(get_current_user_id() . 'updated_solr_post_save_admin_notice')) {
        delete_transient(get_current_user_id() . 'updated_solr_post_save_admin_notice');
        echo "<div class=\"updated wpsolr_admin_notice_updated\"><p>(WPSOLR) {$out}</p></div>";
    }
    if ($out = get_transient(get_current_user_id() . 'wpsolr_some_languages_have_no_solr_index_admin_notice')) {
 /**
  * Retrieve attachments in the fields of type file of the post
  *
  * @param array $attachments
  * @param string $post
  *
  */
 public function filter_get_post_attachments($attachments, $post_id)
 {
     if (!WPSOLR_Metabox::get_metabox_is_do_index_acf_field_files($post_id)) {
         // Do nothing
         return $attachments;
     }
     // Get post ACF field objects
     $fields = get_field_objects($post_id);
     if ($fields) {
         foreach ($fields as $field_name => $field) {
             // Retrieve the post_id of the file
             if (!empty($field['value']) && self::ACF_TYPE_FILE === $field['type']) {
                 switch ($field['save_format']) {
                     case self::ACF_TYPE_FILE_ID:
                         array_push($attachments, array('post_id' => $field['value']));
                         break;
                     case self::ACF_TYPE_FILE_OBJECT:
                         array_push($attachments, array('post_id' => $field['value']['id']));
                         break;
                     case self::ACF_TYPE_FILE_URL:
                         array_push($attachments, array('url' => $field['value']));
                         break;
                     default:
                         // Do nothing
                         break;
                 }
             }
         }
     }
     return $attachments;
 }
 public static function register()
 {
     if (!isset(self::$metabox)) {
         self::$metabox = new self();
     }
 }
 /**
  * @param int $batch_size
  * @param null $post
  *
  * @return array
  * @throws Exception
  */
 public function index_data($batch_size = 100, $post = null, $is_debug_indexing = false)
 {
     global $wpdb;
     // Debug variable containing debug text
     $debug_text = '';
     // Last post date set in previous call. We begin with posts published after.
     // Reset the last post date is reindexing is required.
     $lastPostDate = $this->get_last_post_date_indexed();
     $query_from = $wpdb->prefix . self::TABLE_POSTS . ' AS ' . self::TABLE_POSTS;
     $query_join_stmt = '';
     $query_where_stmt = '';
     $client = $this->solarium_client;
     $updateQuery = $client->createUpdate();
     // Get body of attachment
     $solarium_extract_query = $client->createExtract();
     $post_types = str_replace(",", "','", $this->solr_indexing_options['p_types']);
     $exclude_id = $this->solr_indexing_options['exclude_ids'];
     $ex_ids = array();
     $ex_ids = explode(',', $exclude_id);
     // Build the WHERE clause
     // Where clause for post types
     $where_p = " post_type in ('{$post_types}') ";
     // Build the attachment types clause
     $attachment_types = str_replace(",", "','", $this->solr_indexing_options['attachment_types']);
     if (isset($attachment_types) && $attachment_types != '') {
         $where_a = " ( post_status='publish' OR post_status='inherit' ) AND post_type='attachment' AND post_mime_type in ('{$attachment_types}') ";
     }
     if (isset($where_p)) {
         $query_where_stmt = "post_status='publish' AND ( {$where_p} )";
         if (isset($where_a)) {
             $query_where_stmt = "( {$query_where_stmt} ) OR ( {$where_a} )";
         }
     } elseif (isset($where_a)) {
         $query_where_stmt = $where_a;
     }
     if ($batch_size == 0) {
         // count only
         $query_select_stmt = "count(ID) as TOTAL";
     } else {
         $query_select_stmt = "ID, post_modified, post_parent, post_type";
     }
     if (isset($post)) {
         // Add condition on the $post
         $query_where_stmt = " ID = %d " . " AND ( {$query_where_stmt} ) ";
     } else {
         // Condition on the date only for the batch, not for individual posts
         $query_where_stmt = " post_modified > %s " . " AND ( {$query_where_stmt} ) ";
     }
     $query_order_by_stmt = "post_modified ASC";
     // Filter the query
     $query_statements = apply_filters(WpSolrFilters::WPSOLR_FILTER_SQL_QUERY_STATEMENT, array('SELECT' => $query_select_stmt, 'FROM' => $query_from, 'JOIN' => $query_join_stmt, 'WHERE' => $query_where_stmt, 'ORDER' => $query_order_by_stmt, 'LIMIT' => $batch_size), array('index_indice' => $this->index_indice));
     // Generate query string from the query statements
     $query = sprintf('SELECT %s FROM %s %s WHERE %s ORDER BY %s LIMIT %s', $query_statements['SELECT'], $query_statements['FROM'], $query_statements['JOIN'], $query_statements['WHERE'], $query_statements['ORDER'], $query_statements['LIMIT'] === 0 ? 1 : $query_statements['LIMIT']);
     $documents = array();
     $doc_count = 0;
     $no_more_posts = false;
     while (true) {
         if ($is_debug_indexing) {
             $this->add_debug_line($debug_text, 'Beginning of new loop (batch size)');
         }
         // Execute query (retrieve posts IDs, parents and types)
         if (isset($post)) {
             if ($is_debug_indexing) {
                 $this->add_debug_line($debug_text, 'Query document with post->ID', array('Query' => $query, 'Post ID' => $post->ID));
             }
             $ids_array = $wpdb->get_results($wpdb->prepare($query, $post->ID), ARRAY_A);
         } else {
             if ($is_debug_indexing) {
                 $this->add_debug_line($debug_text, 'Query documents from last post date', array('Query' => $query, 'Last post date' => $lastPostDate));
             }
             $ids_array = $wpdb->get_results($wpdb->prepare($query, $lastPostDate), ARRAY_A);
         }
         if ($batch_size == 0) {
             $nb_docs = $ids_array[0]['TOTAL'];
             if ($is_debug_indexing) {
                 $this->add_debug_line($debug_text, 'End of loop', array('Number of documents in database to be indexed' => $nb_docs));
             }
             // Just return the count
             return $nb_docs;
         }
         // Aggregate current batch IDs in one Solr update statement
         $postcount = count($ids_array);
         if ($postcount == 0) {
             // No more documents to index, stop now by exiting the loop
             if ($is_debug_indexing) {
                 $this->add_debug_line($debug_text, 'No more documents, end of document loop');
             }
             $no_more_posts = true;
             break;
         }
         // For the batch, update the last post date with current post's date
         if (!isset($post)) {
             // In 2 steps to be valid in PHP 5.3
             $lastPost = end($ids_array);
             $lastPostDate = $lastPost['post_modified'];
         }
         for ($idx = 0; $idx < $postcount; $idx++) {
             $postid = $ids_array[$idx]['ID'];
             // If post is not on blacklist, and post is not marked as not indexed
             if (!in_array($postid, $ex_ids, true) && !WPSOLR_Metabox::get_metabox_is_do_not_index($postid)) {
                 // If post is not an attachment
                 if ($ids_array[$idx]['post_type'] !== 'attachment') {
                     // Count this post
                     $doc_count++;
                     // Customize the attachment body, if attachments are linked to the current post
                     $post_attachments = apply_filters(WpSolrFilters::WPSOLR_FILTER_GET_POST_ATTACHMENTS, array(), $postid);
                     // Get the attachments body with a Solr Tika extract query
                     $attachment_body = '';
                     foreach ($post_attachments as $post_attachment) {
                         $attachment_body .= (empty($attachment_body) ? '' : '. ') . self::extract_attachment_text_by_calling_solr_tika($solarium_extract_query, $post_attachment);
                     }
                     // Get the posts data
                     $document = self::create_solr_document_from_post_or_attachment($updateQuery, get_post($postid), $attachment_body);
                     if ($is_debug_indexing) {
                         $this->add_debug_line($debug_text, null, array('Post to be sent' => json_encode($document->getFields(), JSON_PRETTY_PRINT)));
                     }
                     $documents[] = $document;
                 } else {
                     // Post is of type "attachment"
                     if ($is_debug_indexing) {
                         $this->add_debug_line($debug_text, null, array('Post ID to be indexed (attachment)' => $postid));
                     }
                     // Count this post
                     $doc_count++;
                     // Get the attachments body with a Solr Tika extract query
                     $attachment_body = self::extract_attachment_text_by_calling_solr_tika($solarium_extract_query, array('post_id' => $postid));
                     // Get the posts data
                     $document = self::create_solr_document_from_post_or_attachment($updateQuery, get_post($postid), $attachment_body);
                     if ($is_debug_indexing) {
                         $this->add_debug_line($debug_text, null, array('Attachment to be sent' => json_encode($document->getFields(), JSON_PRETTY_PRINT)));
                     }
                     $documents[] = $document;
                 }
             }
         }
         if (empty($documents) || !isset($documents)) {
             // No more documents to index, stop now by exiting the loop
             if ($is_debug_indexing) {
                 $this->add_debug_line($debug_text, 'End of loop, no more documents');
             }
             break;
         }
         // Send batch documents to Solr
         try {
             $res_final = self::send_posts_or_attachments_to_solr_index($updateQuery, $documents);
         } catch (Exception $e) {
             if ($is_debug_indexing) {
                 // Echo debug text now, else it will be hidden by the exception
                 echo $debug_text;
             }
             // Continue
             throw $e;
         }
         // Solr error, or only $post to index: exit loop
         if (!$res_final or isset($post)) {
             break;
         }
         if (!isset($post)) {
             // Store last post date sent to Solr (for batch only)
             $this->set_last_post_date_indexed($lastPostDate);
         }
         // AJAX: one loop by ajax call
         break;
     }
     $status = !isset($res_final) ? 0 : $res_final->getStatus();
     return $res_final = array('nb_results' => $doc_count, 'status' => $status, 'indexing_complete' => $no_more_posts, 'debug_text' => $is_debug_indexing ? $debug_text : null);
 }