| 1 |
<?php |
| 2 |
|
| 3 |
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly |
| 4 |
|
| 5 |
if ( ! class_exists( 'Dfrapi_Configuration' ) ) { |
| 6 |
|
| 7 |
/** |
| 8 |
* Configuration page. |
| 9 |
*/ |
| 10 |
class Dfrapi_Configuration { |
| 11 |
|
| 12 |
private $page = 'dfrapi-configuration'; |
| 13 |
private $key; |
| 14 |
private $account; |
| 15 |
|
| 16 |
public function __construct() { |
| 17 |
$this->key = 'dfrapi_configuration'; |
| 18 |
$this->account = (array) get_option( 'dfrapi_account', array( 'max_length' => 50 ) ); |
| 19 |
add_action( 'init', array( &$this, 'load_settings' ) ); |
| 20 |
add_action( 'admin_init', array( &$this, 'register_settings' ) ); |
| 21 |
add_action( 'admin_menu', array( $this, 'admin_menu' ) ); |
| 22 |
add_action( 'admin_notices', array( $this, 'admin_notice' ) ); |
| 23 |
} |
| 24 |
|
| 25 |
function admin_menu() { |
| 26 |
add_submenu_page( |
| 27 |
'dfrapi', |
| 28 |
__( 'Configuration — Datafeedr API', DFRAPI_DOMAIN ), |
| 29 |
__( 'Configuration', DFRAPI_DOMAIN ), |
| 30 |
'manage_options', |
| 31 |
'dfrapi', |
| 32 |
array( $this, 'output' ) |
| 33 |
); |
| 34 |
} |
| 35 |
|
| 36 |
function admin_notice() { |
| 37 |
if ( isset( $_GET['settings-updated'] ) && $_GET['settings-updated'] == true && isset( $_GET['page'] ) && 'dfrapi' == $_GET['page'] ) { |
| 38 |
echo '<div class="updated"><p>'; |
| 39 |
_e( 'Configuration successfully updated!', DFRAPI_DOMAIN ); |
| 40 |
echo '</p></div>'; |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
function output() { |
| 45 |
echo '<div class="wrap" id="' . $this->key . '">'; |
| 46 |
echo '<h2>' . dfrapi_setting_pages( $this->page ) . ' — Datafeedr API</h2>'; |
| 47 |
echo '<form method="post" action="options.php">'; |
| 48 |
wp_nonce_field( 'update-options' ); |
| 49 |
settings_fields( $this->page ); |
| 50 |
do_settings_sections( $this->page); |
| 51 |
submit_button(); |
| 52 |
echo '</form>'; |
| 53 |
echo '</div>'; |
| 54 |
} |
| 55 |
|
| 56 |
function load_settings() { |
| 57 |
$this->options = (array) get_option( $this->key ); |
| 58 |
$this->options = array_merge( |
| 59 |
array( |
| 60 |
'access_id' => '', |
| 61 |
'secret_key' => '', |
| 62 |
'transport_method' => 'curl', |
| 63 |
'zanox_connection_key' => '', |
| 64 |
'zanox_secret_key' => '', |
| 65 |
), |
| 66 |
$this->options |
| 67 |
); |
| 68 |
} |
| 69 |
|
| 70 |
function register_settings() { |
| 71 |
register_setting( $this->page, $this->key, array( $this, 'validate' ) ); |
| 72 |
add_settings_section( 'api_settings', __( 'Datafeedr API Settings', DFRAPI_DOMAIN ), array( &$this, 'section_api_settings_desc' ), $this->page ); |
| 73 |
add_settings_field( 'access_id', __( 'API Access ID', DFRAPI_DOMAIN ), array( &$this, 'field_access_id' ), $this->page, 'api_settings' ); |
| 74 |
add_settings_field( 'secret_key', __( 'API Secret Key', DFRAPI_DOMAIN ), array( &$this, 'field_secret_key' ), $this->page, 'api_settings' ); |
| 75 |
add_settings_field( 'transport_method', __( 'Transport Method', DFRAPI_DOMAIN ), array( &$this, 'field_transport_method' ), $this->page, 'api_settings' ); |
| 76 |
|
| 77 |
add_settings_section( 'zanox_api_settings', __( 'Zanox Settings', DFRAPI_DOMAIN ), array( &$this, 'section_zanox_api_settings_desc' ), $this->page ); |
| 78 |
add_settings_field( 'zanox_connection_key', __( 'Connection Key', DFRAPI_DOMAIN ), array( &$this, 'field_zanox_connection_key' ), $this->page, 'zanox_api_settings' ); |
| 79 |
add_settings_field( 'zanox_secret_key', __( 'Secret Key', DFRAPI_DOMAIN ), array( &$this, 'field_zanox_secret_key' ), $this->page, 'zanox_api_settings' ); |
| 80 |
} |
| 81 |
|
| 82 |
function section_api_settings_desc() { |
| 83 |
echo __( 'Add your ', DFRAPI_DOMAIN ); |
| 84 |
echo ' <a href="'.DFRAPI_KEYS_URL.'?utm_source=plugin&utm_medium=link&utm_campaign=dfrapiconfigpage" target="_blank" title="' . __( 'Get your Datafeedr API Keys', DFRAPI_DOMAIN ) . '">'; |
| 85 |
echo __( 'Datafeedr API Keys', DFRAPI_DOMAIN ); |
| 86 |
echo '</a>.'; |
| 87 |
} |
| 88 |
|
| 89 |
function field_access_id() { |
| 90 |
?> |
| 91 |
<input type="text" class="regular-text" name="<?php echo $this->key; ?>[access_id]" value="<?php echo esc_attr( $this->options['access_id'] ); ?>" /> |
| 92 |
<?php |
| 93 |
} |
| 94 |
|
| 95 |
function field_secret_key() { |
| 96 |
?> |
| 97 |
<input type="text" class="regular-text" name="<?php echo $this->key; ?>[secret_key]" value="<?php echo esc_attr( $this->options['secret_key'] ); ?>" /> |
| 98 |
<?php |
| 99 |
} |
| 100 |
|
| 101 |
function section_zanox_api_settings_desc() { |
| 102 |
echo __( 'If you want to use the Zanox affiliate network, add your ', DFRAPI_DOMAIN ); |
| 103 |
echo ' <a href="http://publisher.zanox.com/ws_gettingstarted/ws.gettingstarted.html" target="_blank" title="' . __( 'Get your Zanox Keys', DFRAPI_DOMAIN ) . '">'; |
| 104 |
echo __( 'Zanox Keys', DFRAPI_DOMAIN ); |
| 105 |
echo '</a>.'; |
| 106 |
} |
| 107 |
|
| 108 |
function field_zanox_connection_key() { |
| 109 |
?> |
| 110 |
<input type="text" class="regular-text" name="<?php echo $this->key; ?>[zanox_connection_key]" value="<?php echo esc_attr( $this->options['zanox_connection_key'] ); ?>" /> |
| 111 |
<?php |
| 112 |
} |
| 113 |
|
| 114 |
function field_zanox_secret_key() { |
| 115 |
?> |
| 116 |
<input type="text" class="regular-text" name="<?php echo $this->key; ?>[zanox_secret_key]" value="<?php echo esc_attr( $this->options['zanox_secret_key'] ); ?>" /> |
| 117 |
<?php |
| 118 |
} |
| 119 |
|
| 120 |
function field_transport_method() { |
| 121 |
?> |
| 122 |
<select id="transport_method" name="<?php echo $this->key; ?>[transport_method]"> |
| 123 |
<option value="curl" <?php selected( $this->options['transport_method'], 'curl', true ); ?>><?php _e( 'CURL', DFRAPI_DOMAIN ); ?></option> |
| 124 |
<option value="file" <?php selected( $this->options['transport_method'], 'file', true ); ?>><?php _e( 'File', DFRAPI_DOMAIN ); ?></option> |
| 125 |
<option value="socket" <?php selected( $this->options['transport_method'], 'socket', true ); ?>><?php _e( 'Socket', DFRAPI_DOMAIN ); ?></option> |
| 126 |
</select> |
| 127 |
<p class="description"><?php _e( 'If you\'re not sure, use CURL.', DFRAPI_DOMAIN ); ?></p> |
| 128 |
<?php |
| 129 |
} |
| 130 |
|
| 131 |
function validate( $input ) { |
| 132 |
|
| 133 |
if ( !isset( $input ) || !is_array( $input ) || empty( $input ) ) { return $input; } |
| 134 |
|
| 135 |
$new_input = array(); |
| 136 |
|
| 137 |
foreach( $input as $key => $value ) { |
| 138 |
|
| 139 |
// Validate "access_id" |
| 140 |
if ( $key == 'access_id' ) { |
| 141 |
$new_input['access_id'] = trim( $value ); |
| 142 |
} |
| 143 |
|
| 144 |
// Validate "secret_key" |
| 145 |
if ( $key == 'secret_key' ) { |
| 146 |
$new_input['secret_key'] = trim( $value ); |
| 147 |
} |
| 148 |
|
| 149 |
// Validate "transport_method" |
| 150 |
if ( $key == 'transport_method' ) { |
| 151 |
$new_input['transport_method'] = trim( $value ); |
| 152 |
} |
| 153 |
|
| 154 |
// Validate "zanox_connection_key" |
| 155 |
if ( $key == 'zanox_connection_key' ) { |
| 156 |
$new_input['zanox_connection_key'] = trim( $value ); |
| 157 |
} |
| 158 |
|
| 159 |
// Validate "zanox_secret_key" |
| 160 |
if ( $key == 'zanox_secret_key' ) { |
| 161 |
$new_input['zanox_secret_key'] = trim( $value ); |
| 162 |
} |
| 163 |
|
| 164 |
} // foreach |
| 165 |
|
| 166 |
return $new_input; |
| 167 |
} |
| 168 |
|
| 169 |
} // class Dfrapi_Configuration |
| 170 |
|
| 171 |
} // class_exists check |
| 172 |
|