#adminmenu #toplevel_page_wp101 div.wp-menu-image:before { content: "\f236" !important; }'; } private function validate_api_key_with_server( $key=NULL ) { if ( NULL === $key ) { $key = $this->get_key(); } if ( ! $key ) { return 'invalid'; } $query = wp_remote_get( self::$api_base . 'action=check_key&api_key=' . $key . '&url=' . urlencode( get_option('siteurl') ), array( 'timeout' => 45, 'sslverify' => false, 'user-agent' => 'WP101Plugin' ) ); if ( is_wp_error( $query ) ) return false; // Failed to query the server $result = json_decode( wp_remote_retrieve_body( $query ) ); return $result->data->status; } private function get_key() { if ( defined( 'GD_WP101_API_KEY' ) ) { return GD_WP101_API_KEY; } return ''; } public function load() { $this->enqueue(); if ( $message = get_transient( 'wp101_message' ) ) { delete_transient( 'wp101_message' ); add_action( 'admin_notices', array( $this, 'api_key_' . $message . '_message' ) ); } elseif ( !isset( $_GET['configure'] ) ) { $result = $this->validate_api_key(); if ( 'valid' !== $result && current_user_can( 'manage_options' ) ) { set_transient( 'wp101_message', $result, 300 ); wp_redirect( admin_url( 'admin.php?page=wp101&configure=1' ) ); exit(); } } } private function enqueue() { wp_enqueue_style( 'wp101', plugins_url( "css/wp101.css", __FILE__ ), array(), '20140923k' ); } private function validate_api_key() { if ( ! get_transient( 'wp101_api_key_valid' ) ) { // Check the API key against the server $response = $this->validate_api_key_with_server(); if ( 'valid' === $response ) { set_transient( 'wp101_api_key_valid', 1, 30*24*3600 ); // Good for 30 days. } return $response; } else { return 'valid'; // Cached response } } private function get_document( $id ) { $topics = $this->get_help_topics(); if ( isset( $topics[$id] ) ) return $topics[$id]; else return false; } private function get_help_topics() { if ( 'valid' === $this->validate_api_key() ) { if ( $topics = get_transient( 'wp101_topics' ) ) { return $topics; } elseif ( $this->get_key() ) { $result = wp_remote_get( self::$api_base . 'action=get_topics&api_key=' . $this->get_key() . '&url=' . urlencode( get_option('siteurl') ), array( 'timeout' => 45, 'sslverify' => false, 'user-agent' => 'WP101Plugin' ) ); if ( ! is_wp_error( $result ) ) { $result = json_decode( $result['body'], true ); if ( !$result['error'] && count( $result['data'] ) ) { set_transient( 'wp101_topics', $result['data'], 30 ); // Good for a day. return $result['data']; } } } } } private function get_help_topics_html( $edit_mode = false ) { $topics = $this->get_help_topics(); if ( !$topics ) return false; $return = ''; return $return; } public function render_listing_page() { $document_id = isset( $_GET['document'] ) ? sanitize_title( $_GET['document'] ) : 1; if ( $document_id ) : ?>

validate_api_key(); ?>

subscribe at WP101.com.', 'wp101' ), 'https://www.wp101.com/' ); ?>

WP101 is ready to go!', 'wp101' ); ?>

validate_api_key() ) : ?> get_help_topics_html(); ?>
get_document( $document_id ); ?>