| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* The file that defines the core plugin class |
| 5 |
* |
| 6 |
* A class definition that includes attributes and functions used across both the |
| 7 |
* public-facing side of the site and the admin area. |
| 8 |
* |
| 9 |
* @link http://socketlabs.com |
| 10 |
* @since 1.0.0 |
| 11 |
* |
| 12 |
* @package Socketlabs |
| 13 |
* @subpackage Sl_SocketlabsSignup/includes |
| 14 |
*/ |
| 15 |
|
| 16 |
/** |
| 17 |
* The core plugin class. |
| 18 |
* |
| 19 |
* This is used to define internationalization, admin-specific hooks, and |
| 20 |
* public-facing site hooks. |
| 21 |
* |
| 22 |
* Also maintains the unique identifier of this plugin as well as the current |
| 23 |
* version of the plugin. |
| 24 |
* |
| 25 |
* @since 1.0.0 |
| 26 |
* @package Socketlabs |
| 27 |
* @subpackage Socketlabs/includes |
| 28 |
* @author SocketLabs Dev Team <support@socketlabs.com> |
| 29 |
*/ |
| 30 |
class Socketlabs { |
| 31 |
|
| 32 |
/** |
| 33 |
* The loader that's responsible for maintaining and registering all hooks that power |
| 34 |
* the plugin. |
| 35 |
* |
| 36 |
* @since 1.0.0 |
| 37 |
* @access protected |
| 38 |
* @var Socketlabs_Loader $loader Maintains and registers all hooks for the plugin. |
| 39 |
*/ |
| 40 |
protected $loader; |
| 41 |
|
| 42 |
/** |
| 43 |
* The unique identifier of this plugin. |
| 44 |
* |
| 45 |
* @since 1.0.0 |
| 46 |
* @access protected |
| 47 |
* @var string $plugin_name The string used to uniquely identify this plugin. |
| 48 |
*/ |
| 49 |
protected $plugin_name; |
| 50 |
|
| 51 |
/** |
| 52 |
* The current version of the plugin. |
| 53 |
* |
| 54 |
* @since 1.0.0 |
| 55 |
* @access protected |
| 56 |
* @var string $version The current version of the plugin. |
| 57 |
*/ |
| 58 |
protected $version; |
| 59 |
|
| 60 |
/** |
| 61 |
* Define the core functionality of the plugin. |
| 62 |
* |
| 63 |
* Set the plugin name and the plugin version that can be used throughout the plugin. |
| 64 |
* Load the dependencies, define the locale, and set the hooks for the admin area and |
| 65 |
* the public-facing side of the site. |
| 66 |
* |
| 67 |
* @since 1.0.0 |
| 68 |
*/ |
| 69 |
public function __construct() { |
| 70 |
|
| 71 |
$this->plugin_name = SOCKETLABS_OPTION_GROUP; //So happens to be the same as the option group name. |
| 72 |
|
| 73 |
register_setting( SOCKETLABS_OPTION_GROUP, $this->plugin_name); |
| 74 |
|
| 75 |
if ( defined( 'SOCKETLABS_VERSION' ) ) { |
| 76 |
$this->version = SOCKETLABS_VERSION; |
| 77 |
} else { |
| 78 |
$this->version = '1.0.0'; |
| 79 |
} |
| 80 |
|
| 81 |
$this->load_dependencies(); |
| 82 |
$this->set_locale(); |
| 83 |
$this->define_admin_hooks(); |
| 84 |
$this->define_public_hooks(); |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Load the required dependencies for this plugin. |
| 89 |
* |
| 90 |
* Include the following files that make up the plugin: |
| 91 |
* |
| 92 |
* - Socketlabs_Loader. Orchestrates the hooks of the plugin. |
| 93 |
* - Socketlabs_i18n. Defines internationalization functionality. |
| 94 |
* - Socketlabs_Admin. Defines all hooks for the admin area. |
| 95 |
* - Socketlabs_Public. Defines all hooks for the public side of the site. |
| 96 |
* |
| 97 |
* Create an instance of the loader which will be used to register the hooks |
| 98 |
* with WordPress. |
| 99 |
* |
| 100 |
* @since 1.0.0 |
| 101 |
* @access private |
| 102 |
*/ |
| 103 |
private function load_dependencies() { |
| 104 |
|
| 105 |
/** |
| 106 |
* The class responsible for orchestrating the actions and filters of the |
| 107 |
* core plugin. |
| 108 |
*/ |
| 109 |
require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/class-socketlabs-loader.php'; |
| 110 |
|
| 111 |
/** |
| 112 |
* The class responsible for defining internationalization functionality |
| 113 |
* of the plugin. |
| 114 |
*/ |
| 115 |
require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/class-socketlabs-i18n.php'; |
| 116 |
|
| 117 |
/** |
| 118 |
* The class responsible for defining all actions that occur in the admin area. |
| 119 |
*/ |
| 120 |
require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/class-socketlabs-admin.php'; |
| 121 |
|
| 122 |
/** |
| 123 |
* The class responsible for defining all actions that occur in the public-facing |
| 124 |
* side of the site. |
| 125 |
*/ |
| 126 |
require_once plugin_dir_path( dirname( __FILE__ ) ) . 'public/class-socketlabs-public.php'; |
| 127 |
|
| 128 |
/** |
| 129 |
* The api status constants |
| 130 |
*/ |
| 131 |
require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/class-socketlabs-api-status.php'; |
| 132 |
|
| 133 |
/** |
| 134 |
* Service that will assemble and send an injection api message given a wordpress email message |
| 135 |
*/ |
| 136 |
require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/class-socketlabs-mailer.php'; |
| 137 |
|
| 138 |
/** |
| 139 |
* Helper functions for file managment |
| 140 |
*/ |
| 141 |
require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/class-socketlabs-io.php'; |
| 142 |
|
| 143 |
|
| 144 |
$this->loader = new Socketlabs_Loader(); |
| 145 |
|
| 146 |
} |
| 147 |
|
| 148 |
/** |
| 149 |
* Define the locale for this plugin for internationalization. |
| 150 |
* |
| 151 |
* Uses the Socketlabs_i18n class in order to set the domain and to register the hook |
| 152 |
* with WordPress. |
| 153 |
* |
| 154 |
* @since 1.0.0 |
| 155 |
* @access private |
| 156 |
*/ |
| 157 |
private function set_locale() { |
| 158 |
|
| 159 |
$plugin_i18n = new Socketlabs_i18n(); |
| 160 |
|
| 161 |
$this->loader->add_action( 'plugins_loaded', $plugin_i18n, 'load_plugin_textdomain' ); |
| 162 |
|
| 163 |
} |
| 164 |
|
| 165 |
/** |
| 166 |
* Register all of the hooks related to the admin area functionality |
| 167 |
* of the plugin. |
| 168 |
* |
| 169 |
* @since 1.0.0 |
| 170 |
* @access private |
| 171 |
*/ |
| 172 |
private function define_admin_hooks() { |
| 173 |
$plugin_admin = new Socketlabs_Admin( $this->get_plugin_name(), $this->get_version() ); |
| 174 |
|
| 175 |
$this->loader->add_action( 'admin_enqueue_scripts', $plugin_admin, 'enqueue_styles' ); |
| 176 |
$this->loader->add_action( 'admin_enqueue_scripts', $plugin_admin, 'enqueue_scripts' ); |
| 177 |
$this->loader->add_action( 'admin_menu', $plugin_admin, 'admin_menu' ); |
| 178 |
} |
| 179 |
|
| 180 |
/** |
| 181 |
* Register all of the hooks related to the public-facing functionality |
| 182 |
* of the plugin. |
| 183 |
* |
| 184 |
* @since 1.0.0 |
| 185 |
* @access private |
| 186 |
*/ |
| 187 |
private function define_public_hooks() { |
| 188 |
|
| 189 |
$plugin_public = new Socketlabs_Public( $this->get_plugin_name(), $this->get_version() ); |
| 190 |
|
| 191 |
$this->loader->add_action( 'wp_enqueue_scripts', $plugin_public, 'enqueue_styles' ); |
| 192 |
$this->loader->add_action( 'wp_enqueue_scripts', $plugin_public, 'enqueue_scripts' ); |
| 193 |
|
| 194 |
} |
| 195 |
|
| 196 |
/** |
| 197 |
* Run the loader to execute all of the hooks with WordPress. |
| 198 |
* |
| 199 |
* @since 1.0.0 |
| 200 |
*/ |
| 201 |
public function run() { |
| 202 |
$this->loader->run(); |
| 203 |
} |
| 204 |
|
| 205 |
/** |
| 206 |
* The name of the plugin used to uniquely identify it within the context of |
| 207 |
* WordPress and to define internationalization functionality. |
| 208 |
* |
| 209 |
* @since 1.0.0 |
| 210 |
* @return string The name of the plugin. |
| 211 |
*/ |
| 212 |
public function get_plugin_name() { |
| 213 |
return $this->plugin_name; |
| 214 |
} |
| 215 |
|
| 216 |
/** |
| 217 |
* The reference to the class that orchestrates the hooks with the plugin. |
| 218 |
* |
| 219 |
* @since 1.0.0 |
| 220 |
* @return Sl_Signup_Loader Orchestrates the hooks of the plugin. |
| 221 |
*/ |
| 222 |
public function get_loader() { |
| 223 |
return $this->loader; |
| 224 |
} |
| 225 |
|
| 226 |
/** |
| 227 |
* Retrieve the version number of the plugin. |
| 228 |
* |
| 229 |
* @since 1.0.0 |
| 230 |
* @return string The version number of the plugin. |
| 231 |
*/ |
| 232 |
public function get_version() { |
| 233 |
return $this->version; |
| 234 |
} |
| 235 |
|
| 236 |
/** |
| 237 |
* Retrieve the SocketLabs api key. |
| 238 |
* |
| 239 |
* @since 1.0.0 |
| 240 |
* @return string |
| 241 |
*/ |
| 242 |
public static function get_api_key(){ |
| 243 |
$options = get_option( SOCKETLABS_OPTION_GROUP ); |
| 244 |
return isset($options[SOCKETLABS_API_KEY]) ? $options[SOCKETLABS_API_KEY] : ''; |
| 245 |
} |
| 246 |
|
| 247 |
/** |
| 248 |
* Retrieve the server id. |
| 249 |
* |
| 250 |
* @since 1.0.0 |
| 251 |
* @return number |
| 252 |
*/ |
| 253 |
public static function get_server_id(){ |
| 254 |
$options = get_option( SOCKETLABS_OPTION_GROUP ); |
| 255 |
return isset($options[SOCKETLABS_SERVER_ID]) ? $options[SOCKETLABS_SERVER_ID] : ''; |
| 256 |
} |
| 257 |
|
| 258 |
/** |
| 259 |
* Retrieve the the api status. |
| 260 |
* |
| 261 |
* @since 1.0.0 |
| 262 |
* @return string |
| 263 |
*/ |
| 264 |
public static function get_api_status(){ |
| 265 |
|
| 266 |
//Check to see if credentials are set |
| 267 |
$serverId = Socketlabs::get_server_id(); |
| 268 |
$apiKey = Socketlabs::get_api_key(); |
| 269 |
|
| 270 |
if(!defined("SOCKETLABS_API_STATUS") && (empty($serverId) || empty($apiKey))){ |
| 271 |
define("SOCKETLABS_API_STATUS", Socketlabs_Api_Status::$NO_CREDENTIALS); |
| 272 |
} |
| 273 |
|
| 274 |
//Attempt an api call with the available credentials |
| 275 |
if(!defined("SOCKETLABS_API_STATUS")){ |
| 276 |
$headers = array( |
| 277 |
'Content-Type' => 'application/json', |
| 278 |
'Accept' => 'application/json' |
| 279 |
); |
| 280 |
|
| 281 |
// If apiKey is 61 characters, use Bearer token in Authorization header |
| 282 |
if (strlen($apiKey) === 61) { |
| 283 |
$headers['Authorization'] = 'Bearer ' . $apiKey; |
| 284 |
$payload = (object) array( |
| 285 |
"ServerId" => $serverId, |
| 286 |
"Messages" => array((object)array()) |
| 287 |
); |
| 288 |
} else { |
| 289 |
// Otherwise, include apiKey in the payload |
| 290 |
$payload = (object) array( |
| 291 |
"ServerId" => $serverId, |
| 292 |
"Messages" => array((object)array()), |
| 293 |
"ApiKey" => $apiKey |
| 294 |
); |
| 295 |
} |
| 296 |
|
| 297 |
$response = wp_remote_post( SOCKETLABS_INJECTION_URL, array( |
| 298 |
'method' => 'POST', |
| 299 |
'body' => json_encode($payload), |
| 300 |
'headers' => $headers |
| 301 |
)); |
| 302 |
|
| 303 |
if ( is_wp_error( $response ) ) { |
| 304 |
define("SOCKETLABS_API_STATUS", Socketlabs_Api_Status::$NETWORK_ERROR); |
| 305 |
} else { |
| 306 |
$jsonResponse = json_decode($response['body']); |
| 307 |
|
| 308 |
if(isset($jsonResponse->ErrorCode)){ |
| 309 |
$errorCode = $jsonResponse->ErrorCode; |
| 310 |
switch(strtolower($errorCode)){ |
| 311 |
case "invalidauthentication": |
| 312 |
define("SOCKETLABS_API_STATUS", SocketLabs_Api_Status::$BAD_CREDENTIALS); |
| 313 |
break; |
| 314 |
case "novalidrecipients": |
| 315 |
define("SOCKETLABS_API_STATUS", Socketlabs_Api_Status::$SUCCESS); |
| 316 |
break; |
| 317 |
default : |
| 318 |
define("SOCKETLABS_API_STATUS", $errorCode); |
| 319 |
} |
| 320 |
} |
| 321 |
else{ |
| 322 |
define("SOCKETLABS_API_STATUS", SocketLabs_Api_Status::$UNKNOWN); |
| 323 |
} |
| 324 |
} |
| 325 |
} |
| 326 |
return SOCKETLABS_API_STATUS; |
| 327 |
} |
| 328 |
} |
| 329 |
|