'sanitize_text_field', 'type' => 'string', ) ); add_settings_section( 'interpost_api_settings_section', __( 'API Settings', 'interpost' ), function () { echo '

' . esc_html__( 'Enter your Google Gemini API key below. This key is used for both generating embeddings and AI-powered link suggestions.', 'interpost' ) . '

'; }, 'interpost' ); add_settings_field( 'interpost_gemini_api_key_field', __( 'Gemini API Key', 'interpost' ), function () { $api_key = get_option( 'interpost_gemini_api_key' ); printf( '', esc_attr( $api_key ) ); }, 'interpost', 'interpost_api_settings_section' ); register_setting( 'interpost_settings_group', 'interpost_delete_data', array( 'sanitize_callback' => 'interpost_sanitize_checkbox', 'type' => 'boolean', 'default' => 0, ) ); add_settings_section( 'interpost_data_settings_section', __( 'Deleting this plugin', 'interpost' ), function () { echo '

' . esc_html__( 'Deactivating Interpost changes nothing. This only applies when you delete the plugin from the Plugins screen.', 'interpost' ) . '

'; }, 'interpost' ); add_settings_field( 'interpost_delete_data_field', __( 'On deletion', 'interpost' ), function () { printf( '', checked( (bool) get_option( 'interpost_delete_data' ), true, false ), esc_html__( 'Also remove the embedding index and the API key', 'interpost' ) ); echo '

' . esc_html__( 'Rebuilding the index means one Gemini API call for every post, so it costs time and API usage. Leave this off if you might reinstall.', 'interpost' ) . '

'; }, 'interpost', 'interpost_data_settings_section' ); } /** * An unchecked box is not posted at all, so anything present means checked. * * @param mixed $value * @return int */ function interpost_sanitize_checkbox( $value ) { return empty( $value ) ? 0 : 1; } function interpost_settings_page_html() { if ( ! current_user_can( 'manage_options' ) ) { return; } $stats = Interpost_Database::get_index_stats(); ?>


' . esc_html( $stats['indexed'] ) . '', '' . esc_html( $stats['total'] ) . '' ); ?>
admin_url( 'admin-ajax.php' ), 'nonce' => wp_create_nonce( 'interpost_bulk_index_nonce' ), 'i18n' => array( 'starting' => __( 'Starting...', 'interpost' ), 'complete' => __( 'Indexing complete!', 'interpost' ), /* translators: 1: indexed count, 2: total count, 3: percentage. The placeholders are replaced in JavaScript, so a literal percent sign is not escaped. */ 'progress' => __( 'Indexed %1$s of %2$s (%3$s%)', 'interpost' ), /* translators: %s: number of errors. */ 'batchErrors' => __( '%s error(s) in this batch', 'interpost' ), /* translators: %s: number of posts that could not be indexed. */ 'finishedWithErrors' => __( 'Indexing finished. %s post(s) could not be indexed.', 'interpost' ), /* translators: %s: number of posts that could not be indexed. */ 'stalled' => __( 'Indexing stopped: nothing in the last batch could be indexed. %s post(s) failed. Check your API key and quota.', 'interpost' ), 'error' => __( 'Error:', 'interpost' ), 'networkError' => __( 'Network error:', 'interpost' ), 'unknownError' => __( 'Unknown error', 'interpost' ), ), ) ); } /* |-------------------------------------------------------------------------- | AJAX Handlers |-------------------------------------------------------------------------- */ add_action( 'wp_ajax_interpost_bulk_index', array( 'Interpost_Embeddings', 'ajax_bulk_index' ) ); add_action( 'wp_ajax_interpost_index_status', array( 'Interpost_Database', 'ajax_index_status' ) ); /* |-------------------------------------------------------------------------- | Gutenberg Editor Assets |-------------------------------------------------------------------------- */ add_action( 'enqueue_block_editor_assets', 'interpost_enqueue_editor_assets' ); function interpost_enqueue_editor_assets() { $screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null; // The sidebar has nothing to offer on a post type Interpost does not // index: it would search a corpus that cannot contain this post. if ( $screen && ! in_array( $screen->post_type, Interpost_Database::indexed_post_types(), true ) ) { return; } $asset_file_path = INTERPOST_PLUGIN_DIR . 'build/index.asset.php'; if ( ! file_exists( $asset_file_path ) ) { return; } $asset_file = include $asset_file_path; wp_enqueue_script( 'interpost-editor-script', INTERPOST_PLUGIN_URL . 'build/index.js', $asset_file['dependencies'], $asset_file['version'], array( 'in_footer' => true ) ); wp_set_script_translations( 'interpost-editor-script', 'interpost' ); } /* |-------------------------------------------------------------------------- | Auto-Index on Post Save |-------------------------------------------------------------------------- */ add_action( 'save_post', array( 'Interpost_Embeddings', 'on_save_post' ), 10, 3 ); /* |-------------------------------------------------------------------------- | Clean Up After Deleted Posts |-------------------------------------------------------------------------- | | Trashing a post goes through save_post, but deleting one for good does | not. Without this the embedding outlives the post and keeps competing | for a place in the suggestions. | */ add_action( 'deleted_post', 'interpost_on_deleted_post' ); function interpost_on_deleted_post( $post_id ) { Interpost_Database::delete_embedding( (int) $post_id ); } /* |-------------------------------------------------------------------------- | REST API |-------------------------------------------------------------------------- */ add_action( 'rest_api_init', array( 'Interpost_Rest_API', 'register_routes' ) );