self::AISummarizer_decrypt($aws_access_key), 'aws_secret_key' => self::AISummarizer_decrypt($aws_secret_key), 'aws_region' => $aws_region, 'model_id' => $bedrock_model_id ]; $_model = ModelFactory::AISummarizer_create($config); // Test the model with a dummy post object $postObject = "test this is a test"; $summary = $_model->AISummarizer_summarize( wp_json_encode($postObject), $ai_summarizer_summary_length ); if ($summary['success']) { // Update options in the database only if the new values are different if ($ai_summarizer_summary_length !== $existing_ai_summarizer_summary_length) { update_option('ai_summarizer_summary_length', $ai_summarizer_summary_length); } if ($ai_summarizer_configuration !== $existing_ai_summarizer_configuration) { update_option('ai_summarizer_configuration', $ai_summarizer_configuration); } if ($aws_access_key !== $existing_access_key) { update_option('ai_summarizer_aws_access_key', $aws_access_key); } if ($aws_secret_key !== $existing_secret_key) { update_option('ai_summarizer_aws_secret_key', $aws_secret_key); } if ($aws_region !== $existing_region) { update_option('ai_summarizer_aws_region', $aws_region); } if ($bedrock_model_id !== $existing_bedrock_model_id) { update_option('ai_summarizer_bedrock_model_id', $bedrock_model_id); } if ($model_type !== $existing_model_type) { update_option('ai_summarizer_model_type', $model_type); } // Redirect to the settings page after successful update wp_redirect(admin_url('admin.php?page=ai-summarizer')); exit; } else { // Set transient to store the error message set_transient('ai_summarizer_admin_notice', 'Failed : ' . $summary['summary'], 30); // Do not redirect or halt execution. The notice will display automatically. wp_redirect(admin_url('admin.php?page=ai-summarizer')); exit; } } // Hook to display the admin notice public static function AISummarizer_displayAdminNotice() { // Check if there's an error message stored if ($notice = get_transient('ai_summarizer_admin_notice')) { echo '

' . esc_html($notice) . '

'; // Clear the transient after displaying the message delete_transient('ai_summarizer_admin_notice'); } } /** * Encrypts the given data using AES-256-CBC and returns the base64-encoded result. * * This function uses the encryption key retrieved from the 'ai_summarizer_key' option * and the AES-256-CBC algorithm to encrypt the provided data. * * @param string $data The plaintext data that needs to be encrypted. * * @return string The base64-encoded encrypted string. * * @throws Exception If encryption key is missing or the encryption fails. */ private static function AISummarizer_encrypt(string $data): string { // Fetch the encryption key from WordPress options $encryption_key = get_option('ai_summarizer_key'); if (!$encryption_key) { return 'Encryption key not found.'; } // Encrypt the data using AES-256-CBC $encrypted_data = openssl_encrypt( $data, 'aes-256-cbc', $encryption_key ); if ($encrypted_data === false) { return 'Encryption failed.'; } // Return the base64-encoded encrypted data return base64_encode($encrypted_data); } /** * Decrypts the given base64-encoded encrypted data using AES-256-CBC. * * This function retrieves the encryption key from the 'ai_summarizer_key' option and * uses it to decrypt the provided base64-encoded encrypted data. * * @param string $encryptedData The base64-encoded encrypted data that needs to be decrypted. * * @return string The decrypted plaintext string. * * @throws Exception If the encryption key is missing or the decryption fails. */ public static function AISummarizer_decrypt(string $encryptedData): string { // Fetch the encryption key from WordPress options $encryption_key = get_option('ai_summarizer_key'); if (!$encryption_key) { return 'Encryption key not found.'; } // Decode the base64-encoded encrypted data $encrypted_data = base64_decode($encryptedData); if ($encrypted_data === false) { return 'Failed to decode base64 data.'; } // Decrypt the data using AES-256-CBC $decrypted_data = openssl_decrypt( $encrypted_data, 'aes-256-cbc', $encryption_key ); if ($decrypted_data === false) { return 'Decryption failed.'; } // Return the decrypted data return $decrypted_data; } /** * Determines if a post type can be summarized based on whether it uses * the editor or ACF fields for content. * * @param string $post_type The post type to check. * @return bool True if the post type can be summarized, false otherwise. */ public static function AISummarizer_canPostTypeBeSummarized($post_type) { // Check if the post type supports the default WordPress editor if (post_type_supports($post_type, 'editor')) { return true; } // Check if ACF is active and if the post has relevant ACF fields (dynamic check) if (function_exists('get_field_objects')) { // Get all ACF fields for the post type (empty if no fields exist) $acf_fields = get_field_objects(); // Check if there are any text-related ACF fields (e.g., 'text', 'textarea', 'wysiwyg') if ($acf_fields && self::AISummarizer_hasAcfTextContent($acf_fields)) { return true; } } // No summarizable content found return false; } /** * Dynamically checks if ACF fields contain relevant text-based content for summarization. * * @param array $acf_fields The array of ACF fields for the post. * @return bool True if relevant content fields exist, false otherwise. */ public static function AISummarizer_hasAcfTextContent($acf_fields) { foreach ($acf_fields as $field) { // Only consider text-based fields for summarization if (in_array($field['type'], ['text', 'textarea', 'wysiwyg'])) { return true; } } return false; } }