Skip to content
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

Fix timeout on ajax_verify_external_connection #245

Merged
merged 8 commits into from
Mar 24, 2020
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -804,65 +804,48 @@ public function check_connections() {
$can_get = array();
$can_post = array();

$blacklisted_types = [ 'dt_subscription' ];

foreach ( $types as $type_key => $type ) {

if ( in_array( $type_key, $blacklisted_types, true ) ) {
continue;
}
$permission_url = untrailingslashit( $this->base_url ) . '/' . self::$namespace . 'distributor/post-types-permissions';

if ( function_exists( 'vip_safe_wp_remote_get' ) && \Distributor\Utils\is_vip_com() ) {
$permission_response = vip_safe_wp_remote_get(
$permission_url,
false,
3,
3,
10,
$this->auth_handler->format_get_args()
);
} else {

$link = $this->parse_type_items_link( $type );
if ( empty( $link ) ) {
continue;
}
$permission_response = wp_remote_get(
$permission_url,
$this->auth_handler->format_get_args(
array(
'timeout' => self::$timeout,
)
)
);
}
$permission_body = wp_remote_retrieve_body( $permission_response );

$route = str_replace( untrailingslashit( $this->base_url ), '', $link );

if ( ! empty( $routes[ $route ] ) ) {
if ( in_array( 'GET', $routes[ $route ]['methods'], true ) ) {
if ( function_exists( 'vip_safe_wp_remote_get' ) && \Distributor\Utils\is_vip_com() ) {
$type_response = vip_safe_wp_remote_get(
$link,
false,
3,
3,
10,
$this->auth_handler->format_get_args()
);
} else {
$type_response = wp_remote_get( $link, $this->auth_handler->format_get_args( array( 'timeout' => self::$timeout ) ) );
}

if ( ! is_wp_error( $type_response ) ) {
$code = (int) wp_remote_retrieve_response_code( $type_response );

if ( 401 !== $code ) {
$can_get[] = $type_key;
}
}
}

if ( in_array( 'POST', $routes[ $route ]['methods'], true ) ) {
$type_response = wp_remote_post(
$link,
$this->auth_handler->format_post_args(
array(
'timeout' => self::$timeout,
'body' => array( 'test' => 1 ),
)
)
);

if ( ! is_wp_error( $type_response ) ) {
$code = (int) wp_remote_retrieve_response_code( $type_response );

if ( 401 !== $code ) {
$can_post[] = $type_key;
}
}
}
}
if ( is_wp_error( $permission_response ) || empty( $permission_body ) ) {
$output['errors']['no_permissions'] = 'no_permissions';
} else {
$permissions = json_decode( $permission_body );
$can_get = array_filter(
$permissions->can_get,
function ( $key ) {
return 'dt_subscription' !== $key;
},
ARRAY_FILTER_USE_KEY
madmax3365 marked this conversation as resolved.
Show resolved Hide resolved
);
$can_post = array_filter(
$permissions->can_post,
function ( $key ) {
return 'dt_subscription' !== $key;
},
ARRAY_FILTER_USE_KEY
);
madmax3365 marked this conversation as resolved.
Show resolved Hide resolved
}

$output['can_get'] = $can_get;
Expand Down
40 changes: 40 additions & 0 deletions includes/rest-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ function setup() {
'init',
function() {
add_action( 'rest_api_init', __NAMESPACE__ . '\register_endpoints' );
add_action( 'rest_api_init', __NAMESPACE__ . '\register_rest_routes' );

$post_types = get_post_types(
array(
Expand Down Expand Up @@ -128,6 +129,20 @@ function process_distributor_attributes( $post, $request, $update ) {
do_action( 'dt_process_distributor_attributes', $post, $request, $update );
}

/**
* Register custom routes to handle distributor specific functionality.
*/
function register_rest_routes() {
register_rest_route(
'wp/v2',
'distributor/post-types-permissions',
array(
'methods' => 'GET',
'callback' => __NAMESPACE__ . '\check_post_types_permissions',
)
);
}

/**
* Filter the data requested over REST API when a post is pulled.
*
Expand Down Expand Up @@ -253,3 +268,28 @@ function register_endpoints() {
)
);
}

/**
* Check user permissions for available post types
*/
function check_post_types_permissions() {
$types = get_post_types(
array(
'show_in_rest' => true,
),
'objects'
);
$response = array(
'can_get' => array(),
'can_post' => array(),
);
foreach ( $types as $type ) {
$caps = $type->cap;
$response['can_get'][] = $type->name;

if ( current_user_can( $caps->edit_posts ) && current_user_can( $caps->create_posts ) && current_user_can( $caps->publish_posts ) ) {
$response['can_post'][] = $type->name;
}
}
return $response;
}