| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Class StockpackAdmin |
| 5 |
* |
| 6 |
* Handle the settings admin page |
| 7 |
*/ |
| 8 |
class StockpackAdmin { |
| 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 |
add_action( 'plugins_loaded', array( $this, 'init' ) ); |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* |
| 41 |
*/ |
| 42 |
public function init() { |
| 43 |
$this->actions(); |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
/** |
| 48 |
* |
| 49 |
*/ |
| 50 |
public function actions() { |
| 51 |
add_action( 'admin_init', array( $this, 'register_settings' ) ); |
| 52 |
add_action( 'admin_menu', array( $this, 'register_settings_page' ) ); |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Register settings |
| 57 |
*/ |
| 58 |
public function register_settings() { |
| 59 |
|
| 60 |
$this->settings_api = new WeDevs_Settings_API(); |
| 61 |
//set sections and fields |
| 62 |
$this->settings_api->set_sections( $this->get_settings_sections() ); |
| 63 |
$this->settings_api->set_fields( $this->get_settings_fields() ); |
| 64 |
|
| 65 |
//initialize them |
| 66 |
$this->settings_api->admin_init(); |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* |
| 71 |
*/ |
| 72 |
public function register_settings_page() { |
| 73 |
add_options_page( __( 'StockPack', 'stockpack' ), __( 'StockPack', 'stockpack' ), 'delete_posts', 'stockpack', |
| 74 |
array( $this, 'plugin_page' ) |
| 75 |
); |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
/** |
| 80 |
* |
| 81 |
*/ |
| 82 |
public function plugin_page() { |
| 83 |
|
| 84 |
echo '<div class="wrap">'; |
| 85 |
$this->settings_api->show_navigation(); |
| 86 |
$this->settings_api->show_forms(); |
| 87 |
$link_text = esc_textarea( __( 'Get your license key', 'stockpack' ) ); |
| 88 |
echo '<a class="license-key-link" href="https://stockpack.co/register" target="_blank">' . $link_text . '</a>'; |
| 89 |
|
| 90 |
echo '</div>'; |
| 91 |
} |
| 92 |
|
| 93 |
public function get_license_state() { |
| 94 |
return $this->get_option( 'license_state', 'stockpack_basics' ); |
| 95 |
} |
| 96 |
|
| 97 |
public function get_safe_search() { |
| 98 |
return $this->get_option( 'safe_search', 'stockpack_basics' ); |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* @return mixed |
| 103 |
*/ |
| 104 |
public function get_api_key() { |
| 105 |
return $this->get_option( 'auth_token', 'stockpack_basics' ); |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* @return mixed |
| 110 |
*/ |
| 111 |
public function set_api_key( $token ) { |
| 112 |
return $this->set_option( 'auth_token', 'stockpack_basics', $token ); |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
/** |
| 117 |
* @return array |
| 118 |
*/ |
| 119 |
private function get_settings_sections() { |
| 120 |
return array( |
| 121 |
array( |
| 122 |
'id' => 'stockpack_basics', |
| 123 |
'title' => __( 'StockPack', 'stockpack' ), |
| 124 |
), |
| 125 |
); |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* @return array |
| 130 |
*/ |
| 131 |
private function get_settings_fields() { |
| 132 |
return array( |
| 133 |
'stockpack_basics' => array( |
| 134 |
array( |
| 135 |
'name' => 'auth_token', |
| 136 |
'label' => __( 'License key', 'stockpack' ), |
| 137 |
'desc' => __( 'This is the license key that is used for authentication', 'stockpack' ), |
| 138 |
'type' => 'text', |
| 139 |
'default' => '', |
| 140 |
'size' => 'validate-stockpack-key regular' //small hack to add class |
| 141 |
), |
| 142 |
array( |
| 143 |
'name' => 'license_state', |
| 144 |
'label' => __( 'Search with authorization', 'stockpack' ), |
| 145 |
'desc' => __( 'Providers that support oauth can get the license state for all images directly. You see it in the sidebar. The downside is that the searches are slower', 'stockpack' ), |
| 146 |
'options' => array( |
| 147 |
'yes' => __( 'Yes', 'stockpack' ), |
| 148 |
'no' => __( 'No', 'stockpack' ) |
| 149 |
), |
| 150 |
'type' => 'radio', |
| 151 |
'default' => 'no', |
| 152 |
), |
| 153 |
array( |
| 154 |
'name' => 'safe_search', |
| 155 |
'label' => __( 'Apply safe search', 'stockpack' ), |
| 156 |
'desc' => __( 'Some providers support this mode to prevent nude images to show up in the search', 'stockpack' ), |
| 157 |
'options' => array( |
| 158 |
'yes' => __( 'Yes', 'stockpack' ), |
| 159 |
'no' => __( 'No', 'stockpack' ) |
| 160 |
), |
| 161 |
'type' => 'radio', |
| 162 |
'default' => 'yes', |
| 163 |
), |
| 164 |
), |
| 165 |
); |
| 166 |
} |
| 167 |
|
| 168 |
/** |
| 169 |
* Get the value of a settings field |
| 170 |
* |
| 171 |
* @param string $option settings field name |
| 172 |
* @param string $section the section name this field belongs to |
| 173 |
* @param string $default default text if it's not found |
| 174 |
* |
| 175 |
* @return mixed |
| 176 |
*/ |
| 177 |
private function get_option( $option, $section, $default = '' ) { |
| 178 |
|
| 179 |
$options = get_option( $section, array() ); |
| 180 |
|
| 181 |
if ( isset( $options[ $option ] ) ) { |
| 182 |
return $options[ $option ]; |
| 183 |
} |
| 184 |
|
| 185 |
return $default; |
| 186 |
} |
| 187 |
|
| 188 |
/** |
| 189 |
* Set the value of a settings field |
| 190 |
* |
| 191 |
* @param string $option settings field name |
| 192 |
* @param string $section the section name this field belongs to |
| 193 |
* @param string $value |
| 194 |
* |
| 195 |
* @return mixed |
| 196 |
*/ |
| 197 |
private function set_option( $option, $section, $value ) { |
| 198 |
|
| 199 |
$options = get_option( $section, array() ); |
| 200 |
if ( ! is_array( $options ) ) { |
| 201 |
$options = array(); |
| 202 |
} |
| 203 |
|
| 204 |
$options[ $option ] = $value; |
| 205 |
|
| 206 |
update_option( $section, $options ); |
| 207 |
} |
| 208 |
} |
| 209 |
|