Event_Import::unlinked_drafts(),
'canImport' => ! Site_Identity::has_changed(),
)
) . ';',
'before'
);
}
/**
* 画面を描く。
*
* @return void
*/
public static function render_page() {
if ( ! current_user_can( self::CAPABILITY ) ) {
return;
}
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- 表のページ送りで読むだけ。
$listed = self::read_list( isset( $_GET['paged'] ) ? max( 1, absint( $_GET['paged'] ) ) : 1 );
?>
0 && $paged > 1 ) {
return Event_Import::list_page( (int) ceil( $listed['total'] / Event_Import::PAGE_SIZE ) - 1 );
}
return $listed;
}
/**
* 一覧を読めなかったときの文。
*
* @param WP_Error $error 失敗。
* @return string
*/
private static function list_error_text( WP_Error $error ) {
if ( self::is_key_error( $error ) ) {
return Event_Import::stop_reason( $error->get_error_code() );
}
return __( 'The list couldn’t be loaded from EventWatch. Wait a moment, then reload this page.', 'eventwatch' );
}
/**
* キーか団体の問題で断られたか。
*
* @param WP_Error $error 失敗。
* @return bool
*/
private static function is_key_error( WP_Error $error ) {
return in_array( $error->get_error_code(), array( Api_Client::UNAUTHORIZED, Api_Client::SUSPENDED, Api_Client::FORBIDDEN ), true );
}
/**
* REST を登録する。取り込みはこの経路だけから動く。
*
* @return void
*/
public static function register_routes() {
register_rest_route(
Sync_Rest::REST_NAMESPACE,
'/import/prepare',
array(
'methods' => WP_REST_Server::CREATABLE,
'callback' => array( self::class, 'prepare' ),
'permission_callback' => array( self::class, 'can_import' ),
'args' => array(
'eventIds' => array(
'type' => 'array',
'required' => true,
'minItems' => 1,
'maxItems' => self::MAX_IDS,
'items' => array(
'type' => 'string',
'pattern' => self::ID_PATTERN,
),
),
),
)
);
register_rest_route(
Sync_Rest::REST_NAMESPACE,
'/import/run',
array(
'methods' => WP_REST_Server::CREATABLE,
'callback' => array( self::class, 'run' ),
'permission_callback' => array( self::class, 'can_import' ),
'args' => array(
'eventId' => array(
'type' => 'string',
'required' => true,
'pattern' => self::ID_PATTERN,
),
),
)
);
}
/**
* 取り込める人か。
*
* @return bool
*/
public static function can_import() {
return current_user_can( self::CAPABILITY );
}
/**
* 選ばれた投稿を読む。
*
* @param WP_REST_Request $request 要求。
* @return WP_REST_Response
*/
public static function prepare( WP_REST_Request $request ) {
$items = Event_Import::prepare( array_values( array_unique( $request->get_param( 'eventIds' ) ) ) );
if ( is_wp_error( $items ) ) {
$stops = self::is_key_error( $items );
return new WP_REST_Response(
array(
'status' => $stops ? 'stop' : 'failed',
'reason' => $stops ? Event_Import::stop_reason( $items->get_error_code() ) : __( 'The list couldn’t be loaded from EventWatch. Wait a moment, then try again.', 'eventwatch' ),
'items' => array(),
)
);
}
return new WP_REST_Response(
array(
'status' => 'ok',
'reason' => '',
'items' => $items,
)
);
}
/**
* 1 件を取り込む。
*
* @param WP_REST_Request $request 要求。
* @return WP_REST_Response
*/
public static function run( WP_REST_Request $request ) {
return new WP_REST_Response( Event_Import::run( (string) $request->get_param( 'eventId' ) ) );
}
}