Push.php
44 lines
| 1 | <?php |
| 2 | |
| 3 | namespace OTGS\Installer\Rest; |
| 4 | |
| 5 | use \WP_REST_Response; |
| 6 | |
| 7 | class Push { |
| 8 | |
| 9 | const REFRESH_INTERVAL = 7200; |
| 10 | |
| 11 | const REST_NAMESPACE = 'otgs/installer/v1'; |
| 12 | |
| 13 | public static function register_routes() { |
| 14 | register_rest_route( |
| 15 | self::REST_NAMESPACE, |
| 16 | 'push/fetch-subscription', |
| 17 | [ |
| 18 | 'methods' => 'GET', |
| 19 | 'callback' => self::class . '::fetch_subscription', |
| 20 | 'permission_callback' => '__return_true', |
| 21 | ] |
| 22 | ); |
| 23 | } |
| 24 | |
| 25 | public static function fetch_subscription() { |
| 26 | $installer = OTGS_Installer(); |
| 27 | $last_refresh = $installer->get_last_subscriptions_refresh(); |
| 28 | |
| 29 | if ( defined( 'OTGS_INSTALLER_OVERRIDE_SUB_LAST_REFRESH' ) ) { |
| 30 | $last_refresh = constant( 'OTGS_INSTALLER_OVERRIDE_SUB_LAST_REFRESH' ); |
| 31 | } |
| 32 | |
| 33 | if ( time() - $last_refresh > self::REFRESH_INTERVAL ) { |
| 34 | $installer->refresh_subscriptions_data(); |
| 35 | |
| 36 | do_action( 'otgs_installer_subscription_refreshed' ); |
| 37 | |
| 38 | return new WP_REST_Response( [ 'message' => 'OK' ], 200 ); |
| 39 | } |
| 40 | |
| 41 | return new WP_REST_Response( [ 'message' => 'OK' ], 403 ); |
| 42 | } |
| 43 | } |
| 44 |