| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Class StockpackSettings |
| 5 |
* |
| 6 |
* Fetches settings (decouple from admin) |
| 7 |
*/ |
| 8 |
class StockpackSettings { |
| 9 |
/** |
| 10 |
* @var Singleton The reference the *Singleton* instance of this class |
| 11 |
*/ |
| 12 |
private static $instance; |
| 13 |
|
| 14 |
/** |
| 15 |
* @var WeDevs_Settings_API |
| 16 |
*/ |
| 17 |
public $settings_api; |
| 18 |
|
| 19 |
/** |
| 20 |
* Returns the *Singleton* instance of this class. |
| 21 |
* |
| 22 |
* @return Singleton The *Singleton* instance. |
| 23 |
*/ |
| 24 |
public static function get_instance() { |
| 25 |
if ( null === self::$instance ) { |
| 26 |
self::$instance = new self(); |
| 27 |
} |
| 28 |
|
| 29 |
return self::$instance; |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* Stockpack constructor. |
| 34 |
*/ |
| 35 |
protected function __construct() { |
| 36 |
|
| 37 |
} |
| 38 |
|
| 39 |
public function get_license_state() { |
| 40 |
return $this->get_option( 'license_state', 'stockpack_advanced' ); |
| 41 |
} |
| 42 |
|
| 43 |
public function add_license_state_to_filename() { |
| 44 |
return $this->get_option( 'add_license_state_to_filename', 'stockpack_advanced' ); |
| 45 |
} |
| 46 |
|
| 47 |
public function get_safe_search() { |
| 48 |
return $this->get_option( 'safe_search', 'stockpack_basics' ); |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
public function get_file_name_change_setting() { |
| 53 |
return $this->get_option( 'file_name_change', 'stockpack_basics' ); |
| 54 |
} |
| 55 |
|
| 56 |
public function get_premium_providers_caption_setting() { |
| 57 |
return $this->get_option( 'caption_premium_providers', 'stockpack_advanced' ); |
| 58 |
} |
| 59 |
|
| 60 |
public function get_standard_fields_caption_setting() { |
| 61 |
return $this->get_option( 'caption_standard_fields', 'stockpack_advanced' ); |
| 62 |
} |
| 63 |
|
| 64 |
public function get_featured_caption_setting() { |
| 65 |
return $this->get_option( 'caption_featured_image', 'stockpack_advanced' ); |
| 66 |
} |
| 67 |
|
| 68 |
public function get_url_debugging_setting() { |
| 69 |
return $this->get_option( 'url_debugging', 'stockpack_debug' ); |
| 70 |
} |
| 71 |
|
| 72 |
public function get_selected_providers_setting() { |
| 73 |
return $this->get_option( 'providers', 'stockpack_basics', array( |
| 74 |
'Adobe Stock'=> 'Adobe Stock', |
| 75 |
'Deposit Photos'=> 'Deposit Photos', |
| 76 |
'Freepik'=> 'Freepik', |
| 77 |
'Magnific AI'=> 'Magnific AI', |
| 78 |
'Getty'=> 'Getty', |
| 79 |
'iStock'=> 'iStock', |
| 80 |
'Pixabay' => 'Pixabay', |
| 81 |
'Pexels' => 'Pexels', |
| 82 |
'Unsplash' => 'Unsplash', |
| 83 |
) ); |
| 84 |
} |
| 85 |
|
| 86 |
public function get_available_search_provider_slugs() { |
| 87 |
$slugs = array(); |
| 88 |
foreach ( (array) $this->get_selected_providers_setting() as $provider ) { |
| 89 |
$slug = strtolower( str_replace( ' ', '_', trim( $provider ) ) ); |
| 90 |
if ( '' === $slug || 'magnific_ai' === $slug ) { |
| 91 |
continue; |
| 92 |
} |
| 93 |
$slugs[] = $slug; |
| 94 |
} |
| 95 |
|
| 96 |
return $slugs; |
| 97 |
} |
| 98 |
|
| 99 |
public function get_selected_ai_models() { |
| 100 |
$selected = $this->get_option( 'ai_models', 'stockpack_basics', null ); |
| 101 |
|
| 102 |
return is_array( $selected ) ? array_keys( array_filter( $selected ) ) : null; |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* @return mixed |
| 107 |
*/ |
| 108 |
public function get_api_key() { |
| 109 |
$api_key = $this->get_option( 'auth_token', 'stockpack_basics' ); |
| 110 |
|
| 111 |
if ( ! $api_key && defined( 'STOCKPACK_TOKEN' ) ) { |
| 112 |
return STOCKPACK_TOKEN; |
| 113 |
} |
| 114 |
|
| 115 |
return $api_key; |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
/** |
| 120 |
* Get the value of a settings field |
| 121 |
* |
| 122 |
* @param string $option settings field name |
| 123 |
* @param string $section the section name this field belongs to |
| 124 |
* @param string $default default text if it's not found |
| 125 |
* |
| 126 |
* @return mixed |
| 127 |
*/ |
| 128 |
private function get_option( $option, $section, $default = '' ) { |
| 129 |
|
| 130 |
$options = get_option( $section, array() ); |
| 131 |
|
| 132 |
if ( isset( $options[ $option ] ) ) { |
| 133 |
return $options[ $option ]; |
| 134 |
} |
| 135 |
|
| 136 |
return $default; |
| 137 |
} |
| 138 |
|
| 139 |
} |
| 140 |
|