*/ class Alive5_Admin_Settings { /** * The ID of this plugin. * * @since 1.0.0 * @access private * @var string $plugin_name The ID of this plugin. */ private $plugin_name; /** * The version of this plugin. * * @since 1.0.0 * @access private * @var string $version The current version of this plugin. */ private $version; /** * Initialize the class and set its properties. * * @since 1.0.0 * @param string $plugin_name The name of this plugin. * @param string $version The version of this plugin. */ public function __construct( $plugin_name, $version ) { $this->plugin_name = $plugin_name; $this->version = $version; } public function alive5_defaults() { $defaults = array( 'a5_auth_url' => 'https://api.alive5.com/1.0/authuser', 'a5_widgets_url' => 'https://api.alive5.com/1.0/widget-code/get-all' ); return $defaults; } public function alive5_page() { add_menu_page( 'Alive5', 'Alive5', 'manage_options', 'alive5_page', array($this, 'render_alive5_page'), plugin_dir_url(__FILE__) . 'images/logo-small.png' ); } public function render_alive5_page( $active_tab = '' ) { ?>
'', 'a5_username' => '', 'a5_password' => ''); } echo ''; echo ''; echo ''; // $this->a5_auth(); } ///////////////////// End Alive 5 Settings Tab ///////////////////// ///////////////////// Alive 5 Widgets Tab ///////////////////// public function initialize_a5_widgets() { add_settings_section( 'a5_widgets_section', __('Widgets', 'a5_widgets'), array($this, 'a5_widgets_section_callback'), 'a5_widgets' ); register_setting( 'a5_widgets', 'a5_widgets' ); } // Callback function for a5_widgets public function a5_widgets_section_callback() { if (false == get_option('a5_settings')) { echo '

Please configure plugin using Settings tab.

'; wp_die(); } if (true == $this->a5_auth()) { if (true == $a5_auth_creds = get_option('a5_settings')) { $defaults = $this->alive5_defaults(); $a5_widgets_url = $defaults['a5_widgets_url'] . '?org_name=' . $a5_auth_creds['a5_org_name']; } else { return false; } $a5_jwt = get_option('a5_jwt'); $request = wp_remote_get("$a5_widgets_url", array( 'headers' => array( 'authorization' => $a5_jwt ) )); if (is_wp_error($request)) { // var_dump($request); echo '

Error occured while fetching widgets, check settings

'; return false; } $body = wp_remote_retrieve_body($request); $data = json_decode($body); // var_dump ($data); if (!empty($data->data)) { $a5_widgets = get_option('a5_widgets'); foreach ($data->data as $widget) { $html = '
'; $html .= '
'; $html .= 'id, $a5_widgets['selected_widget'], false ) . ' />'; $html .= '
'; $html .= '
'; $html .= ''; $html .= '

Phone Number: '.$widget->sms_phone_number.'

'; $html .= '

Channel: #

'; $html .= '
'; $html .= '
'; echo $html; $arr = get_object_vars($widget); if (false == get_option($wiget->id)) { add_option($widget->id, $arr); } } } else { echo '

Widgets not configured on Alive5 account or Settings are incorrect.

'; wp_die(); } } else { echo '

Authentication Error: Please check credentials on settings tab.

'; wp_die(); } } ///////////////////// End Alive 5 Widgets Tab ///////////////////// public function a5_auth($reauth = false) { $a5_jwt = get_option('a5_jwt'); if (false == $a5_jwt || $reauth == true) { $defaults = $this->alive5_defaults(); $a5_auth_creds = get_option('a5_settings'); // $auth_url = $defaults['a5_auth_url'].'?org_name='.$a5_auth_creds['a5_org_name'].'&email='.$a5_auth_creds['a5_username'].'&password='.$a5_auth_creds['a5_password']; $request = wp_remote_post($defaults['a5_auth_url'], array( 'body' => array( 'org_name' => $a5_auth_creds['a5_org_name'], 'email' => $a5_auth_creds['a5_username'], 'password' => $a5_auth_creds['a5_password'] ) )); if( is_wp_error( $request ) ) { return false; // Bail early } $body = wp_remote_retrieve_body($request); $data = json_decode($body); // print_r($data); if (!empty($data->data->jwt)) { add_option('a5_jwt', $data->data->jwt); return true; } else { return false; } } else { // print_r($a5_jwt); return true; } } }