Skip to content

Commit

Permalink
#241 organize frontend api data keys selection for various frontend c…
Browse files Browse the repository at this point in the history
…ontexts, expose filter hook to select additional keys for output
  • Loading branch information
MichelleBlanchette committed Dec 21, 2024
1 parent 51a490f commit 48f2e34
Showing 1 changed file with 107 additions and 83 deletions.
190 changes: 107 additions & 83 deletions src/admin/class-admin-pages.php
Original file line number Diff line number Diff line change
Expand Up @@ -262,14 +262,7 @@ public static function register_scripts( $hook_suffix ) {
array(
'saved_workspace_gid' => Options::get( Options::ASANA_WORKSPACE_GID ),
'saved_tag_gid' => Options::get( Options::ASANA_TAG_GID ),
'api' => array_intersect_key(
static::get_frontend_api_data(),
array(
'auth_nonce' => true,
'nonce_get_tags' => true,
'v1' => true,
)
),
'api' => static::get_frontend_api_data_for_context( 'settings_deprecated' ),
)
);
break;
Expand All @@ -290,18 +283,7 @@ public static function register_scripts( $hook_suffix ) {
'ptc_completionist_settings',
array(
'deprecated_url' => admin_url( 'admin.php?page=ptc-completionist-deprecated' ),
'api' => array_intersect_key(
static::get_frontend_api_data(),
array(
'nonce_connect_asana' => true,
'nonce_disconnect_asana' => true,
'nonce_update_frontend_auth_user' => true,
'nonce_update_asana_cache_ttl' => true,
'nonce_clear_asana_cache' => true,
'nonce_update_asana_workspace_tag' => true,
'v1' => true,
)
),
'api' => static::get_frontend_api_data_for_context( 'settings' ),
)
);
break;
Expand All @@ -327,18 +309,7 @@ public static function register_scripts( $hook_suffix ) {
'ptc_completionist_automations',
array(
'action_options' => Automations\Actions::ACTION_OPTIONS,
'api' => array_intersect_key(
static::get_frontend_api_data(),
array(
'auth_nonce' => true,
'nonce_create_automation' => true,
'nonce_delete_automation' => true,
'nonce_get_automation' => true,
'nonce_get_post' => true,
'nonce_update_automation' => true,
'v1' => true,
)
),
'api' => static::get_frontend_api_data_for_context( 'automations' ),
'automations' => Automations\Data::get_automation_overviews( null, true ),
'connected_workspace_users' => Asana_Interface::get_connected_workspace_user_options(),
'event_custom_options' => Automations\Events::CUSTOM_OPTIONS,
Expand Down Expand Up @@ -450,18 +421,7 @@ private static function get_frontend_task_data() {
}

$js_data = array(
'api' => array_intersect_key(
static::get_frontend_api_data(),
array(
'auth_nonce' => true,
'nonce_create_task' => true,
'nonce_delete_task' => true,
'nonce_pin_task' => true,
'nonce_unpin_task' => true,
'nonce_update_task' => true,
'v1' => true,
)
),
'api' => static::get_frontend_api_data_for_context( 'task_data' ),
'tasks' => $display_tasks,
'users' => Asana_Interface::get_connected_workspace_users(),
'projects' => Asana_Interface::get_workspace_project_options(),
Expand Down Expand Up @@ -493,57 +453,121 @@ private static function get_frontend_task_data() {
*/
public static function get_frontend_api_data() : array {

if ( ! empty( static::$frontend_api_data ) ) {
return static::$frontend_api_data;
if ( empty( static::$frontend_api_data ) ) {

$api_data = array(
// Generic.
'auth_nonce' => wp_create_nonce( 'wp_rest' ),
'nonce' => wp_create_nonce( 'ptc_completionist' ),
// REST API.
'url' => rest_url(),
'v1' => rest_url( REST_API_NAMESPACE_V1 ),
);

$nonce_actions = array(
// Automations.
'create_automation',
'create_task',
'delete_automation',
'delete_task',
'get_automation',
'get_post',
'get_tags',
'pin_task',
'unpin_task',
'update_automation',
'update_task',
// Settings.
'connect_asana',
'disconnect_asana',
'update_frontend_auth_user',
'update_asana_cache_ttl',
'clear_asana_cache',
'update_asana_workspace_tag',
);

/**
* Filters the actions for which nonces will
* be created for frontend scripts to use.
*
* @since [unreleased]
*
* @param string[] $nonce_actions An array of frontend action names.
*/
$nonce_actions = apply_filters( 'ptc_completionist_frontend_nonce_actions', $nonce_actions );

foreach ( $nonce_actions as $action ) {
$api_data[ "nonce_{$action}" ] = wp_create_nonce( "ptc_completionist_{$action}" );
}

static::$frontend_api_data = $api_data;
}

$api_data = array(
// Generic.
'auth_nonce' => wp_create_nonce( 'wp_rest' ),
'nonce' => wp_create_nonce( 'ptc_completionist' ),
// REST API.
'url' => rest_url(),
'v1' => rest_url( REST_API_NAMESPACE_V1 ),
);
return static::$frontend_api_data;
}

$nonce_actions = array(
// Automations.
'create_automation',
'create_task',
'delete_automation',
'delete_task',
'get_automation',
'get_post',
'get_tags',
'pin_task',
'unpin_task',
'update_automation',
'update_task',
// Settings.
'connect_asana',
'disconnect_asana',
'update_frontend_auth_user',
'update_asana_cache_ttl',
'clear_asana_cache',
'update_asana_workspace_tag',
public static function get_frontend_api_data_for_context( string $context ) : array {

$context_to_keys = array(
'task_data' => array(
'auth_nonce',
'nonce_create_task',
'nonce_delete_task',
'nonce_pin_task',
'nonce_unpin_task',
'nonce_update_task',
'v1',
),
'settings_deprecated' => array(
'auth_nonce',
'nonce_get_tags',
'v1',
),
'settings' => array(
'nonce_connect_asana',
'nonce_disconnect_asana',
'nonce_update_frontend_auth_user',
'nonce_update_asana_cache_ttl',
'nonce_clear_asana_cache',
'nonce_update_asana_workspace_tag',
'v1',
),
'automations' => array(
'auth_nonce',
'nonce_create_automation',
'nonce_delete_automation',
'nonce_get_automation',
'nonce_get_post',
'nonce_update_automation',
'v1',
),
);

if ( empty( $context_to_keys[ $context ] ) ) {
trigger_error( 'Invalid frontend API data context: ' . esc_html( $context ), \E_USER_WARNING );
return array();
}

/**
* Filters the actions for which nonces will
* be created for frontend scripts to use.
* Filters the frontend API data keys which will be selected
* and output with their values for the specified context.
*
* @see get_frontend_api_data()
*
* @since [unreleased]
*
* @param string[] $nonce_actions An array of frontend action names.
* @param string[] $keys The keys to select.
*/
$nonce_actions = apply_filters( 'ptc_completionist_frontend_nonce_actions', $nonce_actions );

foreach ( $nonce_actions as $action ) {
$api_data[ "nonce_{$action}" ] = wp_create_nonce( "ptc_completionist_{$action}" );
$keys = apply_filters( "ptc_completionist_{$context}_frontend_data", $context_to_keys[ $context ] );
if ( empty( $keys ) || ! is_array( $keys ) ) {
trigger_error( 'Missing frontend API data for context: ' . esc_html( $context ), \E_USER_WARNING );
return array();
}

static::$frontend_api_data = $api_data;
return $api_data;
return array_intersect_key(
static::get_frontend_api_data(),
array_flip( $keys )
);
}

/**
Expand Down

0 comments on commit 48f2e34

Please sign in to comment.