er_id = $api_key['consumer_id']; $this->consumer_secret = $api_key['consumer_secret']; $this->permissions = $api_key['permissions']; $this->last_access = $api_key['last_access']; $this->user_id = $api_key['user_id']; $this->api_key_loaded = true; $this->loaded_api_key_name = $api_key['name']; } /** * Get the listing of connections. * * @since 6.0.0 Migrated to Common from Event Automator * * @param boolean $all_data Whether to return all api_key data, default is only name and status. * * @return array $list_of_api_keys An array of all the connections. */ public function get_list_of_api_keys( $all_data = false ) { $list_of_api_keys = get_option( $this->all_api_keys_key, [] ); foreach ( $list_of_api_keys as $consumer_id => $api_key ) { if ( empty( $api_key['name'] ) ) { continue; } $list_of_api_keys[ $consumer_id ]['name'] = $api_key['name']; // If false (default ) skip getting all the api_key data. if ( empty( $all_data ) ) { continue; } $api_key_data = $this->get_api_key_by_id( $consumer_id ); $list_of_api_keys[ $consumer_id ] = $api_key_data; } return $list_of_api_keys; } /** * Update the list of Connections with provided api_key. * * @since 6.0.0 Migrated to Common from Event Automator * * @param array $api_key_data The array of data for an api_key to add to the list. */ protected function update_list_of_api_keys( $api_key_data ) { $api_keys = $this->get_list_of_api_keys(); $api_id = static::$api_id; /** * Fires after before the api_key list is updated for an API. * * @since 6.0.0 Migrated to Common from Event Automator * * @param array An array of connections formatted for options dropdown. * @param array $api_key_data The array of data for an api_key to add to the list. * @param string $api_id The id of the API in use. */ do_action( "tec_automator_before_update_{$api_id}_api_keys", $api_keys, $api_key_data, $api_id ); $api_keys[ esc_attr( $api_key_data['consumer_id'] ) ] = [ 'name' => esc_attr( $api_key_data['name'] ), ]; update_option( $this->all_api_keys_key, $api_keys ); } /** * Delete from the list of connections the provided api_key. * * @since 6.0.0 Migrated to Common from Event Automator * * @param string $consumer_id The id of the single api_key to save. */ protected function delete_from_list_of_api_keys( $consumer_id ) { $api_keys = $this->get_list_of_api_keys(); unset( $api_keys[ $consumer_id ] ); update_option( $this->all_api_keys_key, $api_keys ); } /** * Get a Single connection by id. * * @since 6.0.0 Migrated to Common from Event Automator * * @param string $consumer_id The id of the single api_key. * * @return array $api_key The api_key data or empty array if no api_key. */ public function get_api_key_by_id( $consumer_id ) { return get_option( $this->single_api_key_prefix . $consumer_id, [] ); } /** * Set a connection with the provided id. * * @since 6.0.0 Migrated to Common from Event Automator * * @param array $api_key_data A specific api_key data to save. */ public function set_api_key_by_id( array $api_key_data ) { // hash the consumer id and secret if they start with the prefix. $api_key_data['consumer_id'] = strpos( $api_key_data['consumer_id'], 'ci_' ) === 0 ? static::api_hash( $api_key_data['consumer_id'] ) : $api_key_data['consumer_id']; $api_key_data['consumer_secret'] = strpos( $api_key_data['consumer_secret'], 'ck_' ) === 0 ? static::api_hash( $api_key_data['consumer_secret'] ) : $api_key_data['consumer_secret']; update_option( $this->single_api_key_prefix . $api_key_data['consumer_id'], $api_key_data, false ); $this->update_list_of_api_keys( $api_key_data ); } /** * Updates the last access valid access of the connection. * * @since 6.0.0 Migrated to Common from Event Automator * * @param string $consumer_id The id of the single api_key. * @param string $app_name The optional app name used with this connection. */ public function set_api_key_last_access( $consumer_id, $app_name = '' ) { $hashed_consumer_id = strpos( $consumer_id, 'ci_' ) === 0 ? static::api_hash($consumer_id ) : $consumer_id; $api_key_data = $this->get_api_key_by_id( $hashed_consumer_id ); $api_key_data['last_access'] = $this->get_last_access( $app_name ); update_option( $this->single_api_key_prefix . $hashed_consumer_id, $api_key_data, false ); } /** * Delete an api_key by ID. * * @since 6.0.0 Migrated to Common from Event Automator * * @param string $consumer_id The id of the single api_key. * * @return bool Whether the api_key has been deleted and the access access_token revoked. */ public function delete_api_key_by_id( $consumer_id ) { delete_option( $this->single_api_key_prefix . $consumer_id ); $this->delete_from_list_of_api_keys( $consumer_id ); return true; } /** * Maybe hash the consumer secret. * * @since 6.0.0 Migrated to Common from Event Automator * * @param string $consumer_secret The consumer secret to verify the connection with. * * @return string The hashed consumer secret. */ protected function hash_the_secret( $consumer_secret) { return strpos( $consumer_secret, 'ck_' ) === 0 ? static::api_hash( $consumer_secret ) : $consumer_secret; } /** * Check the consumer secret. * * @since 6.0.0 Migrated to Common from Event Automator * * @param string $consumer_secret The consumer secret to verify the connection with. * * @return boolean Whether the consumer secret matches the saved one. */ protected function check_secret( $consumer_secret) { // Validate user secret. if ( ! hash_equals( $this->consumer_secret, $consumer_secret ) ) { return false; } return true; } /** * Hash the specified text. * * @since 6.0.0 Migrated to Common from Event Automator * * @param string $text Text to be hashed. * * @return string The hashed text. */ public static function api_hash( $text ) { return hash_hmac( 'sha256', $text, 'tec-automator-' . static::$api_id ); } /** * Get the WP_User object from thelLoaded connection. * * @since 6.0.0 Migrated to Common from Event Automator * * @return WP_User|null Returns the WP_User object or null if not loaded. */ public function get_user() { return ! empty( $this->user ) ? $this->user : null; } /** * Get the WP_User_Query query results. * * @since 6.0.0 Migrated to Common from Event Automator * * @return array An array of user objects. */ public function get_users() { $args = [ 'number' => 1000, 'role__in' => [ 'Administrator', 'Editor' ], ]; $api_id = static::$api_id; /** * Filters the argument array to query users for the connection fields user dropdown. * * @since 6.0.0 Migrated to Common from Event Automator * * @param array The default array to query users for the connection fields user dropdown. */ $args = apply_filters( "tec_event_automator_{$api_id}_api_get_user_arguments", $args ); // Custom query. $user_query = new WP_User_Query( $args ); return $user_query->results; } /** * Get a user's information formatted for internal use. * * @since 6.0.0 Migrated to Common from Event Automator * * @param array $user Information for a user from an API, * * @return array An array of a user's information formatted for internal use. */ protected function get_formatted_user_info( $user ) { $user_data = $user->data; $user_info = []; $user_info['name'] = Arr::get( $user_data, 'user_login', '' ); $user_info['id'] = Arr::get( $user_data, 'ID', '' ); $user_info['email'] = Arr::get( $user_data, 'user_email', '' ); return $user_info; } /** * Get list of users formatted for options dropdown. * * @since 6.0.0 Migrated to Common from Event Automator * * @return array An array of WordPress Users to use for connection. */ public function get_users_options_list() { $available_users = $this->get_users(); $current_user_id = get_current_user_id(); if ( empty( $available_users ) ) { return []; } $users = []; foreach ( $available_users as $user ) { $user_info = $this->get_formatted_user_info( $user ); if ( empty( $user_info['name'] ) || empty( $user_info['id'] ) || empty( $user_info['email'] ) ) { continue; } $users[] = [ 'text' => (string) trim( $user_info['name'] ), 'sort' => (string) trim( $user_info['name'] ), 'id' => (string) $user_info['id'], 'value' => (string) $user_info['id'], 'selected' => $current_user_id === (int) $user_info['id'] ? true : false, ]; } // Sort the users array by text(email). $sort_arr = array_column( $users, 'sort' ); array_multisort( $sort_arr, SORT_ASC, $users ); return $users; } /** * Get users dropdown fields for a tribe dropdown input. * * @since 6.0.0 Migrated to Common from Event Automator * * @return array An array of WordPress Users to use in a tribe dropdown input. */ public function get_users_dropdown() { $users = $this->get_users_options_list(); $current_user_id = get_current_user_id(); $api_id = static::$api_id; $users_dropdown = [ 'label' => _x( 'Users', 'The label of the users dropdown for an integration.', 'tribe-common' ), 'id' => "tec-settings-{$api_id}-users", 'classes_label' => [ 'screen-reader-text' ], 'classes_select' => [ 'tec-settings-form__users-dropdown', "tec-settings-{$api_id}-details-api-key__users-dropdown" ], 'name' => "tec_automator_{$api_id}[]['users']", 'selected' => $current_user_id, 'users_count' => count( $users ), 'users_arr' => $users, 'attrs' => [ 'placeholder' => _x( 'Select a User', 'The placeholder for the dropdown to select a user.', 'tribe-common' ), 'data-prevent-clear' => true, 'data-force-search' => true, 'data-options' => json_encode( $users ), ], ]; return $users_dropdown; } }