", * "post_mode": "publish" | "draft", * "default_category_id": , * "default_author_id": , * "default_tag_ids": [, ...], * "remove_duplicate_content": true | false * } * * @package Ranktim */ if ( ! defined( 'ABSPATH' ) ) { exit; } class Ranktim_Settings { /** wp_options key. */ const OPTION_KEY = 'ranktim_settings'; /** @var Ranktim_Encryption */ private Ranktim_Encryption $encryption; public function __construct( Ranktim_Encryption $encryption ) { $this->encryption = $encryption; } // ----------------------------------------------------------------------- // Read // ----------------------------------------------------------------------- /** * Return all settings with the integration key decrypted. * * @return array{ * integration_key: string, * post_mode: string, * default_category_id: int, * default_author_id: int, * default_tag_ids: int[], * remove_duplicate_content: bool * } */ public function get(): array { $stored = get_option( self::OPTION_KEY, array() ); if ( ! is_array( $stored ) ) { $stored = array(); } $key = ''; if ( ! empty( $stored['integration_key_enc'] ) ) { $key = $this->encryption->decrypt( $stored['integration_key_enc'] ); } $tag_ids = isset( $stored['default_tag_ids'] ) && is_array( $stored['default_tag_ids'] ) ? array_map( 'intval', $stored['default_tag_ids'] ) : array(); // Default to enabled when the setting does not exist yet (fresh installs). $remove_duplicate_content = array_key_exists( 'remove_duplicate_content', $stored ) ? ! empty( $stored['remove_duplicate_content'] ) : true; return array( 'integration_key' => $key, 'post_mode' => $stored['post_mode'] ?? 'publish', 'default_category_id' => isset( $stored['default_category_id'] ) ? (int) $stored['default_category_id'] : 0, 'default_author_id' => isset( $stored['default_author_id'] ) ? (int) $stored['default_author_id'] : 0, 'default_tag_ids' => $tag_ids, 'remove_duplicate_content' => $remove_duplicate_content, ); } /** * Convenience — return only the plaintext integration key. */ public function get_key(): string { return $this->get()['integration_key']; } /** * Convenience — return only the post mode ("publish" or "draft"). */ public function get_post_mode(): string { return $this->get()['post_mode']; } /** * Convenience — return the default category ID (0 = uncategorised). */ public function get_default_category_id(): int { return $this->get()['default_category_id']; } /** * Convenience — return the default author ID (0 = use site default). */ public function get_default_author_id(): int { return $this->get()['default_author_id']; } /** * Convenience — return the default tag IDs (empty array = no tags). * * @return int[] */ public function get_default_tag_ids(): array { return $this->get()['default_tag_ids']; } /** * Convenience — return whether duplicate content should be stripped. */ public function get_remove_duplicate_content(): bool { return $this->get()['remove_duplicate_content']; } // ----------------------------------------------------------------------- // Write // ----------------------------------------------------------------------- /** * Persist settings. * * @param string $integration_key Plaintext key (will be encrypted). * @param string $post_mode "publish" or "draft". * @param int $default_category_id Term ID of the default category (0 = uncategorised). * @param int $default_author_id User ID of the default author (0 = site default). * @param int[] $default_tag_ids Array of tag term IDs to apply to every post. * @param bool $remove_duplicate_content Whether to strip the first heading and image from content. */ public function save( string $integration_key, string $post_mode, int $default_category_id = 0, int $default_author_id = 0, array $default_tag_ids = array(), bool $remove_duplicate_content = true ): void { update_option( self::OPTION_KEY, array( 'integration_key_enc' => $this->encryption->encrypt( $integration_key ), 'post_mode' => $post_mode, 'default_category_id' => $default_category_id, 'default_author_id' => $default_author_id, 'default_tag_ids' => array_map( 'intval', $default_tag_ids ), 'remove_duplicate_content' => $remove_duplicate_content, ), false // Do not autoload — only needed on specific admin/cron pages. ); } }