PluginProbe
Tiered Pricing Table for WooCommerce / trunk
Tiered Pricing Table for WooCommerce vtrunk
7.1.7 7.1.5 7.1.4 7.1.1 6.5.0 6.4.0 6.1.0 trunk 1.0 1.1 2.0 2.0.1 2.0.2 2.1 2.1.1 2.1.2 2.2.0 2.2.1 2.2.2 2.2.3 2.3.0 2.3.1 2.3.2 2.3.3 2.3.4 All 90 releases
tier-pricing-table / src / Addons / TierLabels / API / TierLabelsEndpoints.php

TierLabelsEndpoints.php in Tiered Pricing Table for WooCommerce trunk, at src/Addons/TierLabels/API/TierLabelsEndpoints.php

58 lines 1.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php namespace TierPricingTable\Addons\TierLabels\API;
2
3 use TierPricingTable\Addons\TierLabels\TierLabel;
4 use TierPricingTable\Addons\TierLabels\TierLabelsManager;
5
6 class TierLabelsEndpoints {
7
8 const NAMESPACE = 'tier-pricing-table/features/tier-labels/v1';
9
10 public function __construct() {
11 add_action( 'rest_api_init', array( $this, 'registerRoutes' ) );
12 }
13
14 public function registerRoutes() {
15
16 $labelsManager = TierLabelsManager::getInstance();
17
18 register_rest_route( self::NAMESPACE, '/labels', array(
19 array(
20 'methods' => 'GET',
21 'callback' => function () use ( $labelsManager ) {
22 $labels = $labelsManager->getLabels();
23
24 $labelsArray = array_map( function ( $label ) {
25 return $label->toArray();
26 }, $labels );
27
28 return rest_ensure_response( $labelsArray );
29 },
30 'permission_callback' => function () {
31 return current_user_can( 'manage_options' );
32 },
33 ),
34 array(
35 'methods' => 'POST',
36 'callback' => function ( $request ) use ( $labelsManager ) {
37 $labels = $request->get_param( 'labels' );
38 $labelInstances = array();
39
40 foreach ( $labels as $label ) {
41 $labelInstance = TierLabel::fromArray( $label );
42
43 if ( $labelInstance->isValid() ) {
44 $labelInstances[] = $labelInstance;
45 }
46 }
47
48 $labelsManager->saveLabels( $labelInstances );
49
50 return rest_ensure_response( array( 'success' => true ) );
51 },
52 'permission_callback' => function () {
53 return current_user_can( 'manage_options' );
54 },
55 ),
56 ) );
57 }
58 }