' . 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;
}
}