'smof',
'media-overview' => 'smof_media_overview'
];
// If a specific page is requested
if (!empty($page_name)) {
if (!isset($plugin_pages[$page_name])) {
return false; // Invalid page name provided
}
// Check if the current page matches the requested page
return $current_page === $plugin_pages[$page_name];
}
// Check if we're on any plugin page
return in_array($current_page, array_values($plugin_pages));
}
}
/**
* Generate copyright text
*
* @return string
*/
/**
* Generate copyright and support text
*
* @return string
*/
if (!function_exists('smof_get_copyright_text')) {
function smof_get_copyright_text(): string
{
return esc_html__('Adoovy Media Offload — developed by Adoovy. Based on Advanced Media Offloader by WP Fitter (GPL v2 or later).', 'adoovy-media-offload');
}
}
/**
* Get bulk offload data.
*
* @return array The bulk offload data.
*/
if (!function_exists('smof_get_bulk_offload_data')) {
function smof_get_bulk_offload_data(): array
{
$defaults = array(
'total' => 0,
'status' => '',
'processed' => 0,
'errors' => 0,
'last_update' => null,
'oversized_skipped' => 0
);
$stored_data = get_option('smof_bulk_offload_data', array());
return array_merge($defaults, $stored_data);
}
}
/**
* Update bulk offload data.
*
* @param array $new_data The new data to update.
* @return array The updated bulk offload data.
*/
if (!function_exists('smof_update_bulk_offload_data')) {
function smof_update_bulk_offload_data(array $new_data): array
{
// Define the allowed keys
$allowed_keys = array(
'total',
'status',
'processed',
'errors',
'oversized_skipped',
'last_recovery',
'last_cleanup',
'last_update'
);
// Filter the new data to only include allowed keys
$filtered_new_data = array_intersect_key($new_data, array_flip($allowed_keys));
// Get the existing data
$existing_data = smof_get_bulk_offload_data();
// Merge the filtered new data with the existing data
$updated_data = array_merge($existing_data, $filtered_new_data);
// Ensure only allowed keys are in the final data set
$final_data = array_intersect_key($updated_data, array_flip($allowed_keys));
// Keep counters internally consistent: processed should never exceed total.
// This can happen if background tasks overlap/retry while total comes from a smaller batch selection.
if (isset($final_data['processed'], $final_data['total']) && (int) $final_data['processed'] > (int) $final_data['total']) {
$final_data['total'] = (int) $final_data['processed'];
}
// Normalise timestamp fields.
foreach (array('last_recovery', 'last_cleanup') as $timestamp_key) {
if (isset($final_data[$timestamp_key])) {
$final_data[$timestamp_key] = (int) $final_data[$timestamp_key];
}
}
// Add a timestamp for the last update if not explicitly provided.
if (!isset($filtered_new_data['last_update'])) {
$final_data['last_update'] = time();
} else {
$final_data['last_update'] = (int) $filtered_new_data['last_update'];
}
// Update the option in the database
update_option('smof_bulk_offload_data', $final_data);
update_option('smof_bulk_offload_last_update', $final_data['last_update']);
return $final_data;
}
}
/**
* Check if media is organized by year and month.
*
* @return bool True if media is organized by year and month, false otherwise.
*/
if (!function_exists('smof_is_media_organized_by_year_month')) {
function smof_is_media_organized_by_year_month(): bool
{
return get_option('uploads_use_yearmonth_folders') ? true : false;
}
}
/**
* Sanitize a URL path.
*
* @param string $path The path to sanitize.
* @return string The sanitized path.
*/
if (!function_exists('smof_sanitize_path')) {
function smof_sanitize_path(string $path): string
{
// Remove leading and trailing whitespace
$path = trim($path);
// Remove or encode potentially harmful characters
$path = wp_sanitize_redirect($path);
// Convert to lowercase for consistency (optional, depending on your needs)
$path = strtolower($path);
// Remove any directory traversal attempts
$path = str_replace(['../', './'], '', $path);
// Normalize slashes and remove duplicate slashes
$path = preg_replace('#/+#', '/', $path);
// Remove leading and trailing slashes
$path = trim($path, '/');
// Optionally, you can use wp_normalize_path() if you want to ensure consistent directory separators
$path = wp_normalize_path($path);
return $path;
}
}
/**
* Clear bulk offload data.
*
* @return void
*/
if (!function_exists('smof_clear_bulk_offload_data')) {
function smof_clear_bulk_offload_data(): void
{
delete_option('smof_bulk_offload_data');
}
}
/**
* Get the cloud provider key.
*
* @return string The cloud provider key.
*/
if (!function_exists('smof_get_cloud_provider_key')) {
function smof_get_cloud_provider_key(): string
{
// 1) Provider chosen on the settings page.
$options = get_option('smof_settings', []);
if (!empty($options['cloud_provider'])) {
return (string) $options['cloud_provider'];
}
$providers = array('aws_s3', 'cloudflare_r2', 'backblaze_b2', 'digitalocean', 'wasabi', 'minio');
$constant_keys = array('aws_s3' => 'aws', 'digitalocean' => 'dos');
// 2) Explicit wp-config.php constant: define('SMOF_CLOUD_PROVIDER', 'cloudflare_r2');
if (defined('SMOF_CLOUD_PROVIDER') && in_array(SMOF_CLOUD_PROVIDER, $providers, true)) {
return SMOF_CLOUD_PROVIDER;
}
// 3) Auto-detect: first provider whose KEY, SECRET and BUCKET constants are all defined.
foreach ($providers as $provider) {
$prefix = 'SMOF_' . strtoupper($constant_keys[$provider] ?? $provider) . '_';
if (defined($prefix . 'KEY') && defined($prefix . 'SECRET') && defined($prefix . 'BUCKET')) {
return $provider;
}
}
return '';
}
}
/**
* List the required storage details that are still missing.
*
* @return string[] Empty array when everything required is present. The first
* item is 'provider' when no storage provider is selected/detected.
*/
if (!function_exists('smof_get_missing_credentials')) {
function smof_get_missing_credentials(): array
{
$provider = smof_get_cloud_provider_key();
if ($provider === '') {
return array('provider');
}
// Credentials are stored under short keys for Amazon S3 ("aws") and DigitalOcean ("dos").
$credential_keys = array('aws_s3' => 'aws', 'digitalocean' => 'dos');
$credential_key = $credential_keys[$provider] ?? $provider;
$missing = array();
foreach (array('key', 'secret', 'bucket') as $field) {
if (smof_get_provider_credential($credential_key, $field) === '') {
$missing[] = $field;
}
}
return $missing;
}
}
/**
* Get the count of unoffloaded media items.
*
* @return int The count of unoffloaded media items.
*/
if (!function_exists('smof_get_unoffloaded_media_items_count')) {
function smof_get_unoffloaded_media_items_count(): int
{
global $wpdb;
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Table names come from $wpdb; no user input.
return (int) $wpdb->get_var("SELECT COUNT(*) FROM {$wpdb->posts} p
LEFT JOIN {$wpdb->postmeta} pm ON p.ID = pm.post_id AND pm.meta_key = 'smof_offloaded'
WHERE p.post_type = 'attachment'
AND (pm.meta_value IS NULL OR pm.meta_value = '')");
}
}
if (!function_exists('smof_get_offloaded_media_items_count')) {
function smof_get_offloaded_media_items_count(): int
{
global $wpdb;
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Count of offloaded attachments.
return (int) $wpdb->get_var($wpdb->prepare(
"SELECT COUNT(DISTINCT p.ID)
FROM {$wpdb->posts} p
INNER JOIN {$wpdb->postmeta} pm ON p.ID = pm.post_id
WHERE p.post_type = %s
AND p.post_status != %s
AND pm.meta_key = %s
AND pm.meta_value != %s",
array(
'attachment',
'trash',
'smof_offloaded',
''
)
));
}
}
/**
* Check if a credential constant exists in wp-config.php
*
* @param string $constant_name The name of the constant to check.
* @return bool True if constant is defined, false otherwise.
*/
if (!function_exists('smof_credential_exists_in_config')) {
function smof_credential_exists_in_config(string $constant_name): bool
{
return defined($constant_name);
}
}
/**
* Get provider credentials from constants or saved options.
* Constants take priority over saved options.
*
* @param string $provider_key The cloud provider key (e.g., 'cloudflare_r2', 'amazon_s3').
* @param string $field_name The credential field name (e.g., 'key', 'secret', 'bucket').
* @return string The credential value or empty string if not found.
*/
if (!function_exists('smof_get_provider_credential')) {
function smof_get_provider_credential(string $provider_key, string $field_name): string
{
// Build constant name (e.g., SMOF_CLOUDFLARE_R2_KEY)
$constant_name = 'SMOF_' . strtoupper($provider_key) . '_' . strtoupper($field_name);
// Check if constant exists first (priority)
if (defined($constant_name)) {
$value = constant($constant_name);
// Support scalar constant types (common in wp-config.php), especially booleans for checkboxes.
if (is_bool($value)) {
return $value ? '1' : '0';
}
if (is_int($value) || is_float($value)) {
return (string) $value;
}
return is_string($value) ? $value : '';
}
// Fall back to saved options
$credentials = get_option('smof_credentials', []);
if (isset($credentials[$provider_key][$field_name])) {
return $credentials[$provider_key][$field_name];
}
return '';
}
}
/**
* Save provider credentials to options.
*
* @param string $provider_key The cloud provider key.
* @param array $credentials Associative array of credential fields and values.
* @return bool True on success, false on failure.
*/
if (!function_exists('smof_save_provider_credentials')) {
function smof_save_provider_credentials(string $provider_key, array $credentials): bool
{
$all_credentials = get_option('smof_credentials', []);
$all_credentials[$provider_key] = $credentials;
return update_option('smof_credentials', $all_credentials);
}
}
/**
* Get all credentials for a specific provider.
*
* @param string $provider_key The cloud provider key.
* @return array Associative array of all credential fields for the provider.
*/
if (!function_exists('smof_get_provider_credentials')) {
function smof_get_provider_credentials(string $provider_key): array
{
$credentials = get_option('smof_credentials', []);
return $credentials[$provider_key] ?? [];
}
}
/**
* Brand logo (inline SVG).
*/
if (!function_exists('smof_get_logo_svg')) {
function smof_get_logo_svg(int $size = 52): string
{
return '';
}
}
/**
* Human readable provider name.
*/
if (!function_exists('smof_get_provider_label')) {
function smof_get_provider_label(string $key): string
{
$labels = array(
'aws_s3' => 'Amazon S3',
'cloudflare_r2' => 'Cloudflare R2',
'backblaze_b2' => 'Backblaze B2',
'digitalocean' => 'DigitalOcean Spaces',
'wasabi' => 'Wasabi',
'minio' => 'MinIO',
);
return $labels[$key] ?? ucwords(str_replace('_', ' ', $key));
}
}
/**
* Allowed HTML for plugin-generated markup (credential fields, icons, navigation).
* Used with wp_kses() when echoing markup that is assembled from already-escaped parts.
*
* @return array>
*/
if (!function_exists('smof_allowed_html')) {
function smof_allowed_html(): array
{
$common = array('class' => true, 'id' => true, 'aria-hidden' => true, 'aria-label' => true, 'title' => true);
$svg_attrs = array(
'class' => true, 'id' => true, 'xmlns' => true, 'width' => true, 'height' => true, 'viewbox' => true,
'fill' => true, 'stroke' => true, 'stroke-width' => true, 'stroke-linecap' => true, 'stroke-linejoin' => true,
'fill-rule' => true, 'clip-rule' => true, 'role' => true, 'aria-hidden' => true, 'aria-label' => true, 'focusable' => true,
);
return array(
'div' => $common,
'p' => $common,
'span' => $common,
'strong' => array(),
'code' => array(),
'em' => array(),
'br' => array(),
'a' => array('href' => true, 'class' => true, 'target' => true, 'rel' => true),
'label' => array('for' => true, 'class' => true),
'input' => array(
'type' => true, 'id' => true, 'name' => true, 'value' => true, 'placeholder' => true, 'class' => true,
'checked' => true, 'disabled' => true, 'readonly' => true, 'autocomplete' => true,
),
'button' => array('type' => true, 'class' => true, 'id' => true, 'aria-label' => true),
'svg' => $svg_attrs,
'g' => $svg_attrs,
'path' => array_merge($svg_attrs, array('d' => true)),
'circle' => array_merge($svg_attrs, array('cx' => true, 'cy' => true, 'r' => true)),
'rect' => array_merge($svg_attrs, array('x' => true, 'y' => true, 'rx' => true, 'ry' => true)),
'defs' => array(),
'lineargradient' => array('id' => true, 'x1' => true, 'y1' => true, 'x2' => true, 'y2' => true, 'gradientunits' => true),
'stop' => array('offset' => true, 'stop-color' => true),
);
}
}