-
Notifications
You must be signed in to change notification settings - Fork 20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add REST API support #59
Open
rmccue
wants to merge
2
commits into
master
Choose a base branch
from
add-api-support
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,129 @@ | ||||||
<?php | ||||||
|
||||||
class Falcon_API { | ||||||
const USER_PREF_FIELD = 'falcon_preferences'; | ||||||
|
||||||
/** | ||||||
* Bootstrap. | ||||||
*/ | ||||||
public static function bootstrap() { | ||||||
register_rest_field( 'user', static::USER_PREF_FIELD, [ | ||||||
'get_callback' => [ get_called_class(), 'get_pref_field' ], | ||||||
'update_callback' => [ get_called_class(), 'update_pref_field' ], | ||||||
'schema' => static::get_pref_schema(), | ||||||
] ); | ||||||
} | ||||||
|
||||||
/** | ||||||
* Get the preferences field value. | ||||||
* | ||||||
* Calls out to the enabled connectors. | ||||||
* | ||||||
* @param array $data Full response data. | ||||||
* @return array Data for the field. | ||||||
*/ | ||||||
public static function get_pref_field( $data ) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why name the functions tersely like this?
Suggested change
|
||||||
$user = get_user_by( 'id', $data['id'] ); | ||||||
$field_data = []; | ||||||
foreach ( Falcon::get_connectors() as $type => $connector ) { | ||||||
/** | ||||||
* Filter preference field value. | ||||||
* | ||||||
* Connect to this in your connector to make the data available | ||||||
* via the REST API. | ||||||
* | ||||||
* @param mixed $connector_data Data for your connector. Null to skip in the API. | ||||||
* @param WP_User $user User to get data for. | ||||||
*/ | ||||||
$connector_data = apply_filters( "falcon.api.get_pref_field.$type", null, $user ); | ||||||
if ( $connector_data === null ) { | ||||||
continue; | ||||||
} | ||||||
|
||||||
$field_data[ $type ] = $connector_data; | ||||||
} | ||||||
return $field_data; | ||||||
} | ||||||
|
||||||
/** | ||||||
* Update the preferences field value. | ||||||
* | ||||||
* Calls out to the enabled connectors. | ||||||
* | ||||||
* @param array $value Value passed by the user (validated by the schema). | ||||||
* @param WP_User $user User being updated. | ||||||
* @return boolean|WP_Error True if field was updated, error otherwise. | ||||||
*/ | ||||||
public static function update_pref_field( $value, WP_User $user ) { | ||||||
if ( empty( $value ) ) { | ||||||
return true; | ||||||
} | ||||||
|
||||||
$connectors = Falcon::get_connectors(); | ||||||
foreach ( $value as $type => $type_options ) { | ||||||
if ( empty( $connectors[ $type ] ) ) { | ||||||
return new WP_Error( | ||||||
'falcon.api.update_pref_field.invalid_type', | ||||||
__( 'Attempted to set preference for invalid connector', 'falcon' ), | ||||||
compact( 'type' ) | ||||||
); | ||||||
} | ||||||
|
||||||
/** | ||||||
* Filter the result of updating the field. | ||||||
* | ||||||
* Connect to this in your connector to make the data updatable via | ||||||
* the REST API. | ||||||
* | ||||||
* @param mixed $result True if field was updated, WP_Error if cannot update, null if unhandled. | ||||||
*/ | ||||||
$result = apply_filters( "falcon.api.update_pref_field.$type", null, $type_options, $user ); | ||||||
if ( $result === null ) { | ||||||
return new WP_Error( | ||||||
'falcon.api.update_pref_field.unhandled_update', | ||||||
__( 'Connector does not support updating via the API', 'falcon' ), | ||||||
compact( 'type' ) | ||||||
); | ||||||
} | ||||||
if ( is_wp_error( $result ) ) { | ||||||
return $result; | ||||||
} | ||||||
} | ||||||
|
||||||
return true; | ||||||
} | ||||||
|
||||||
/** | ||||||
* Get the schema for the preferences field. | ||||||
* | ||||||
* Calls out to the enabled connectors. | ||||||
* | ||||||
* @return array Schema for the preferences field. | ||||||
*/ | ||||||
public static function get_pref_schema() { | ||||||
$schema = [ | ||||||
'description' => __( 'Falcon notification preferences', 'falcon' ), | ||||||
'type' => 'object', | ||||||
'properties' => [], | ||||||
]; | ||||||
|
||||||
foreach ( Falcon::get_connectors() as $type => $connector ) { | ||||||
/** | ||||||
* Filter preference field schema. | ||||||
* | ||||||
* Connect to this in your connector to make the data available | ||||||
* via the REST API. | ||||||
* | ||||||
* @param mixed $connector_schema Schema for your connector. Null to skip in the API. | ||||||
*/ | ||||||
$connector_schema = apply_filters( "falcon.api.get_pref_schema.$type", null ); | ||||||
if ( $connector_schema === null ) { | ||||||
continue; | ||||||
} | ||||||
|
||||||
$schema['properties'][ $type ] = $connector_schema; | ||||||
} | ||||||
|
||||||
return $schema; | ||||||
} | ||||||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMO this should have
REST
in the class name.