Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
73.91% |
17 / 23 |
|
75.00% |
3 / 4 |
CRAP | |
0.00% |
0 / 1 |
| Script_Data_Controller | |
80.95% |
17 / 21 |
|
75.00% |
3 / 4 |
6.25 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
| register_routes | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
1 | |||
| get_item_permissions_check | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
3 | |||
| get_item | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Publicize: Script Data Controller |
| 4 | * |
| 5 | * @package automattic/jetpack-publicize |
| 6 | */ |
| 7 | |
| 8 | namespace Automattic\Jetpack\Publicize\REST_API; |
| 9 | |
| 10 | use Automattic\Jetpack\Publicize\Publicize_Script_Data; |
| 11 | use Automattic\Jetpack\Publicize\Publicize_Utils as Utils; |
| 12 | use WP_Error; |
| 13 | use WP_REST_Server; |
| 14 | |
| 15 | if ( ! defined( 'ABSPATH' ) ) { |
| 16 | exit( 0 ); |
| 17 | } |
| 18 | |
| 19 | /** |
| 20 | * Serves the Social dashboard's page data, so the dashboard can boot on any admin page. |
| 21 | * |
| 22 | * @phan-constructor-used-for-side-effects |
| 23 | */ |
| 24 | class Script_Data_Controller extends Base_Controller { |
| 25 | |
| 26 | /** |
| 27 | * Register the route on `rest_api_init`. |
| 28 | */ |
| 29 | public function __construct() { |
| 30 | parent::__construct(); |
| 31 | |
| 32 | $this->namespace = 'wpcom/v2'; |
| 33 | $this->rest_base = 'publicize/script-data'; |
| 34 | |
| 35 | add_action( 'rest_api_init', array( $this, 'register_routes' ) ); |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * Register the routes. |
| 40 | */ |
| 41 | public function register_routes() { |
| 42 | register_rest_route( |
| 43 | $this->namespace, |
| 44 | '/' . $this->rest_base, |
| 45 | array( |
| 46 | 'methods' => WP_REST_Server::READABLE, |
| 47 | 'callback' => array( $this, 'get_item' ), |
| 48 | 'permission_callback' => array( $this, 'get_item_permissions_check' ), |
| 49 | ) |
| 50 | ); |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * The Social admin page's gate, which unlike publicize_permissions_check() passes while Publicize is off. |
| 55 | * |
| 56 | * @param \WP_REST_Request $request Full details about the request. |
| 57 | * @return true|WP_Error |
| 58 | */ |
| 59 | public function get_item_permissions_check( $request ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
| 60 | if ( current_user_can( 'publish_posts' ) && Utils::can_open_social_dashboard() ) { |
| 61 | return true; |
| 62 | } |
| 63 | |
| 64 | return new WP_Error( |
| 65 | 'invalid_user_permission_publicize', |
| 66 | __( 'Sorry, you are not allowed to access Jetpack Social data on this site.', 'jetpack-publicize-pkg' ), |
| 67 | array( 'status' => rest_authorization_required_code() ) |
| 68 | ); |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Return the data the Social dashboard reads as `JetpackScriptData.social`. |
| 73 | * |
| 74 | * @param \WP_REST_Request $request Full details about the request. |
| 75 | * @return \WP_REST_Response |
| 76 | */ |
| 77 | public function get_item( $request ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
| 78 | return rest_ensure_response( Publicize_Script_Data::get_social_script_data() ); |
| 79 | } |
| 80 | } |