diff --git a/admin.php b/admin.php index 0a2b2e2..d8be5ff 100644 --- a/admin.php +++ b/admin.php @@ -22,7 +22,7 @@ function rest_oauth1_profile_section( $user ) { global $wpdb; - $results = $wpdb->get_col( "SELECT option_value FROM {$wpdb->options} WHERE option_name LIKE 'oauth1_access_%'", 0 ); + $results = $wpdb->get_col( "SELECT option_value FROM $wpdb->options WHERE option_name LIKE 'oauth1_access_%'" ); $approved = array(); foreach ( $results as $result ) { $row = unserialize( $result ); @@ -31,8 +31,6 @@ function rest_oauth1_profile_section( $user ) { } } - $authenticator = new WP_REST_OAuth1(); // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable - ?>
post_title ); ?> | diff --git a/lib/class-wp-rest-client.php b/lib/class-wp-rest-client.php index 26a848a..b3459e0 100644 --- a/lib/class-wp-rest-client.php +++ b/lib/class-wp-rest-client.php @@ -10,12 +10,20 @@ * REST Client. */ abstract class WP_REST_Client { + + /** + * Post object. + * + * @var WP_Post + */ + protected $post; + /** * Get the client type. * * Must be overridden in subclass. * - * @return string + * @return string | WP_Error */ protected static function get_type() { return new WP_Error( 'rest_client_missing_type', __( 'Overridden class must implement get_type', 'rest_oauth1' ) ); @@ -126,7 +134,8 @@ public static function get( $id ) { return new WP_Error( 'rest_oauth1_invalid_id', __( 'Client ID is not valid.', 'rest_oauth1' ), array( 'status' => 404 ) ); } - $class = function_exists( 'get_called_class' ) ? get_called_class() : self::get_called_class(); + $class = get_called_class(); + return new $class( $post ); } @@ -137,8 +146,7 @@ public static function get( $id ) { * @return WP_Post|WP_Error */ public static function get_by_key( $key ) { - $class = function_exists( 'get_called_class' ) ? get_called_class() : self::get_called_class(); - $type = call_user_func( array( $class, 'get_type' ) ); + $type = call_user_func( array( get_called_class(), 'get_type' ) ); $query = new WP_Query(); $consumers = $query->query( @@ -159,8 +167,7 @@ public static function get_by_key( $key ) { ); if ( empty( $consumers ) || empty( $consumers[0] ) ) { - $code = is_user_logged_in() ? 403 : 401; - return new WP_Error( 'json_consumer_notfound', __( 'Consumer Key is invalid', 'rest_oauth1' ), array( 'status' => $code ) ); + return new WP_Error( 'json_consumer_notfound', __( 'Consumer Key is invalid', 'rest_oauth1' ), array( 'status' => 401 ) ); } return $consumers[0]; @@ -170,11 +177,11 @@ public static function get_by_key( $key ) { * Create a new client. * * @param array $params { . - * @type string $name Client name - * @type string $description Client description - * @type array $meta Metadata for the client (map of key => value) + * @type string $name Client name + * @type string $description Client description + * @type array $meta Metadata for the client (map of key => value) * } - * @return WP_Post|WP_Error + * @return WP_REST_Client|WP_Error */ public static function create( $params ) { $default = array( @@ -194,12 +201,12 @@ public static function create( $params ) { return $id; } - $class = function_exists( 'get_called_class' ) ? get_called_class() : self::get_called_class(); $meta = $params['meta']; + $class = get_called_class(); $meta['type'] = call_user_func( array( $class, 'get_type' ) ); // Allow types to add their own meta too. - $meta = $class::add_extra_meta( $meta, $params ); + $meta = call_user_func( array( $class, 'add_extra_meta' ), $meta, $params ); /** * Add extra meta to the consumer on creation. @@ -235,9 +242,15 @@ protected static function add_extra_meta( $meta, $params ) { //phpcs:ignore Vari /** * Shim for get_called_class() for PHP 5.2 * + * @deprecated 0.4.0 * @return string Class name. */ protected static function get_called_class() { + _deprecated_function( __METHOD__, '0.4.0', 'get_called_class()' ); + if ( function_exists( 'get_called_class' ) ) { + return get_called_class(); + } + // PHP 5.2 only. $backtrace = debug_backtrace(); // [0] WP_REST_Client::get_called_class() diff --git a/lib/class-wp-rest-oauth1-admin.php b/lib/class-wp-rest-oauth1-admin.php index 33db347..ece6651 100644 --- a/lib/class-wp-rest-oauth1-admin.php +++ b/lib/class-wp-rest-oauth1-admin.php @@ -24,6 +24,8 @@ public static function register() { */ include_once __DIR__ . '/class-wp-rest-oauth1-listtable.php'; + $class = get_class(); + $hook = add_users_page( // Page title. __( 'Registered OAuth Applications', 'rest_oauth1' ), @@ -34,10 +36,10 @@ public static function register() { // Menu slug. self::BASE_SLUG, // Callback. - array( get_class(), 'dispatch' ) + array( $class, 'dispatch' ) ); - add_action( 'load-' . $hook, array( get_class(), 'load' ) ); + add_action( 'load-' . $hook, array( $class, 'load' ) ); } /** @@ -68,22 +70,19 @@ public static function load() { switch ( self::current_action() ) { case 'add': case 'edit': - return self::render_edit_page(); - + self::render_edit_page(); + break; case 'delete': - return self::handle_delete(); - + self::handle_delete(); + break; case 'regenerate': - return self::handle_regenerate(); - + self::handle_regenerate(); + break; default: global $wp_list_table; $wp_list_table = new WP_REST_OAuth1_ListTable(); - $wp_list_table->prepare_items(); - - return; } } @@ -91,14 +90,11 @@ public static function load() { * Render callback. */ public static function dispatch() { - switch ( self::current_action() ) { - case 'add': - case 'edit': - case 'delete': - break; - default: - self::render(); + if ( in_array( self::current_action(), array( 'add', 'edit', 'delete' ), true ) ) { + return; } + + self::render(); } /** @@ -163,19 +159,18 @@ protected static function validate_parameters( $params ) { $valid['description'] = wp_filter_post_kses( $params['description'] ); if ( empty( $params['callback'] ) ) { - return new WP_Error( 'rest_oauth1_missing_description', __( 'Consumer callback is required and must be a valid URL.', 'rest_oauth1' ) ); - } - if ( ! empty( $params['callback'] ) ) { - $valid['callback'] = $params['callback']; + return new WP_Error( 'rest_oauth1_missing_callback', __( 'Consumer callback is required and must be a valid URL.', 'rest_oauth1' ) ); } + $valid['callback'] = $params['callback']; + return $valid; } /** * Handle submission of the add page - * @param WP_User $consumer Consumer user. + * @param WP_REST_Client $consumer Consumer user. * * @return array|null List of errors. Issues a redirect and exits on success. */ @@ -197,8 +192,6 @@ protected static function handle_edit_submit( $consumer ) { } if ( empty( $consumer ) ) { - new WP_REST_OAuth1(); - // Create the consumer. $data = array( 'name' => $params['name'], @@ -248,8 +241,9 @@ public static function render_edit_page() { } // Are we editing? - $consumer = null; - $form_action = self::get_url( 'action=add' ); + $consumer = null; + $regenerate_action = ''; + $form_action = self::get_url( 'action=add' ); if ( ! empty( $_REQUEST['id'] ) ) { $id = absint( $_REQUEST['id'] ); $consumer = WP_REST_OAuth1_Client::get( $id ); @@ -430,13 +424,15 @@ public static function handle_delete() { $client = WP_REST_OAuth1_Client::get( $id ); if ( is_wp_error( $client ) ) { wp_die( $client ); - return; } if ( ! $client->delete() ) { - $message = 'Invalid consumer ID'; - wp_die( $message ); - return; + $code = is_user_logged_in() ? 403 : 401; + wp_die( + '