@@ -6,56 +6,56 @@ |
||
| 6 | 6 | |
| 7 | 7 | class Module { |
| 8 | 8 | |
| 9 | - const RUN_EVENT = 'wl_food_kg__run'; |
|
| 10 | - |
|
| 11 | - /** |
|
| 12 | - * @var Api_Service_Ext |
|
| 13 | - */ |
|
| 14 | - private $api_service; |
|
| 15 | - |
|
| 16 | - /** |
|
| 17 | - * @var Recipe_Lift_Strategy |
|
| 18 | - */ |
|
| 19 | - private $recipe_lift_strategies; |
|
| 20 | - |
|
| 21 | - /** |
|
| 22 | - * @param Api_Service_Ext $api_service |
|
| 23 | - * @param Recipe_Lift_Strategy[] $recipe_lift_strategies |
|
| 24 | - */ |
|
| 25 | - public function __construct( Api_Service_Ext $api_service, array $recipe_lift_strategies ) { |
|
| 26 | - $this->api_service = $api_service; |
|
| 27 | - $this->recipe_lift_strategies = $recipe_lift_strategies; |
|
| 28 | - } |
|
| 29 | - |
|
| 30 | - public function register_hooks() { |
|
| 31 | - add_action( 'wl_key_updated', array( $this, '__key_updated' ) ); |
|
| 32 | - add_action( 'wp_ajax_wl_food_kg__run', array( $this, '__run' ) ); |
|
| 33 | - } |
|
| 34 | - |
|
| 35 | - public function __key_updated() { |
|
| 36 | - try { |
|
| 37 | - $me_response = $this->api_service->me(); |
|
| 38 | - $has_food_kg = isset( $me_response->networks ) |
|
| 39 | - && array_reduce( $me_response->networks, array( $this, '__has_food_kg' ), false ); |
|
| 40 | - $next_scheduled = wp_next_scheduled( self::RUN_EVENT ); |
|
| 41 | - |
|
| 42 | - // We're connected to the Food KG, but not yet scheduled. |
|
| 43 | - if ( $has_food_kg && ! $next_scheduled ) { |
|
| 44 | - wp_schedule_event( time(), 'daily', self::RUN_EVENT ); |
|
| 45 | - } |
|
| 46 | - |
|
| 47 | - // We're not connected to the Food KG, but we're scheduled. |
|
| 48 | - if ( ! $has_food_kg && $next_scheduled ) { |
|
| 49 | - wp_unschedule_event( $next_scheduled, self::RUN_EVENT ); |
|
| 50 | - } |
|
| 51 | - // phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedCatch |
|
| 52 | - } catch ( \Exception $e ) { |
|
| 53 | - // Do nothing. |
|
| 54 | - } |
|
| 55 | - } |
|
| 56 | - |
|
| 57 | - private function __has_food_kg( $carry, $item ) { |
|
| 58 | - return $carry || 'https://knowledge.cafemedia.com/food/' === $item->dataset_uri; |
|
| 59 | - } |
|
| 9 | + const RUN_EVENT = 'wl_food_kg__run'; |
|
| 10 | + |
|
| 11 | + /** |
|
| 12 | + * @var Api_Service_Ext |
|
| 13 | + */ |
|
| 14 | + private $api_service; |
|
| 15 | + |
|
| 16 | + /** |
|
| 17 | + * @var Recipe_Lift_Strategy |
|
| 18 | + */ |
|
| 19 | + private $recipe_lift_strategies; |
|
| 20 | + |
|
| 21 | + /** |
|
| 22 | + * @param Api_Service_Ext $api_service |
|
| 23 | + * @param Recipe_Lift_Strategy[] $recipe_lift_strategies |
|
| 24 | + */ |
|
| 25 | + public function __construct( Api_Service_Ext $api_service, array $recipe_lift_strategies ) { |
|
| 26 | + $this->api_service = $api_service; |
|
| 27 | + $this->recipe_lift_strategies = $recipe_lift_strategies; |
|
| 28 | + } |
|
| 29 | + |
|
| 30 | + public function register_hooks() { |
|
| 31 | + add_action( 'wl_key_updated', array( $this, '__key_updated' ) ); |
|
| 32 | + add_action( 'wp_ajax_wl_food_kg__run', array( $this, '__run' ) ); |
|
| 33 | + } |
|
| 34 | + |
|
| 35 | + public function __key_updated() { |
|
| 36 | + try { |
|
| 37 | + $me_response = $this->api_service->me(); |
|
| 38 | + $has_food_kg = isset( $me_response->networks ) |
|
| 39 | + && array_reduce( $me_response->networks, array( $this, '__has_food_kg' ), false ); |
|
| 40 | + $next_scheduled = wp_next_scheduled( self::RUN_EVENT ); |
|
| 41 | + |
|
| 42 | + // We're connected to the Food KG, but not yet scheduled. |
|
| 43 | + if ( $has_food_kg && ! $next_scheduled ) { |
|
| 44 | + wp_schedule_event( time(), 'daily', self::RUN_EVENT ); |
|
| 45 | + } |
|
| 46 | + |
|
| 47 | + // We're not connected to the Food KG, but we're scheduled. |
|
| 48 | + if ( ! $has_food_kg && $next_scheduled ) { |
|
| 49 | + wp_unschedule_event( $next_scheduled, self::RUN_EVENT ); |
|
| 50 | + } |
|
| 51 | + // phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedCatch |
|
| 52 | + } catch ( \Exception $e ) { |
|
| 53 | + // Do nothing. |
|
| 54 | + } |
|
| 55 | + } |
|
| 56 | + |
|
| 57 | + private function __has_food_kg( $carry, $item ) { |
|
| 58 | + return $carry || 'https://knowledge.cafemedia.com/food/' === $item->dataset_uri; |
|
| 59 | + } |
|
| 60 | 60 | |
| 61 | 61 | } |
@@ -22,39 +22,39 @@ |
||
| 22 | 22 | * @param Api_Service_Ext $api_service |
| 23 | 23 | * @param Recipe_Lift_Strategy[] $recipe_lift_strategies |
| 24 | 24 | */ |
| 25 | - public function __construct( Api_Service_Ext $api_service, array $recipe_lift_strategies ) { |
|
| 25 | + public function __construct(Api_Service_Ext $api_service, array $recipe_lift_strategies) { |
|
| 26 | 26 | $this->api_service = $api_service; |
| 27 | 27 | $this->recipe_lift_strategies = $recipe_lift_strategies; |
| 28 | 28 | } |
| 29 | 29 | |
| 30 | 30 | public function register_hooks() { |
| 31 | - add_action( 'wl_key_updated', array( $this, '__key_updated' ) ); |
|
| 32 | - add_action( 'wp_ajax_wl_food_kg__run', array( $this, '__run' ) ); |
|
| 31 | + add_action('wl_key_updated', array($this, '__key_updated')); |
|
| 32 | + add_action('wp_ajax_wl_food_kg__run', array($this, '__run')); |
|
| 33 | 33 | } |
| 34 | 34 | |
| 35 | 35 | public function __key_updated() { |
| 36 | 36 | try { |
| 37 | 37 | $me_response = $this->api_service->me(); |
| 38 | - $has_food_kg = isset( $me_response->networks ) |
|
| 39 | - && array_reduce( $me_response->networks, array( $this, '__has_food_kg' ), false ); |
|
| 40 | - $next_scheduled = wp_next_scheduled( self::RUN_EVENT ); |
|
| 38 | + $has_food_kg = isset($me_response->networks) |
|
| 39 | + && array_reduce($me_response->networks, array($this, '__has_food_kg'), false); |
|
| 40 | + $next_scheduled = wp_next_scheduled(self::RUN_EVENT); |
|
| 41 | 41 | |
| 42 | 42 | // We're connected to the Food KG, but not yet scheduled. |
| 43 | - if ( $has_food_kg && ! $next_scheduled ) { |
|
| 44 | - wp_schedule_event( time(), 'daily', self::RUN_EVENT ); |
|
| 43 | + if ($has_food_kg && ! $next_scheduled) { |
|
| 44 | + wp_schedule_event(time(), 'daily', self::RUN_EVENT); |
|
| 45 | 45 | } |
| 46 | 46 | |
| 47 | 47 | // We're not connected to the Food KG, but we're scheduled. |
| 48 | - if ( ! $has_food_kg && $next_scheduled ) { |
|
| 49 | - wp_unschedule_event( $next_scheduled, self::RUN_EVENT ); |
|
| 48 | + if ( ! $has_food_kg && $next_scheduled) { |
|
| 49 | + wp_unschedule_event($next_scheduled, self::RUN_EVENT); |
|
| 50 | 50 | } |
| 51 | 51 | // phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedCatch |
| 52 | - } catch ( \Exception $e ) { |
|
| 52 | + } catch (\Exception $e) { |
|
| 53 | 53 | // Do nothing. |
| 54 | 54 | } |
| 55 | 55 | } |
| 56 | 56 | |
| 57 | - private function __has_food_kg( $carry, $item ) { |
|
| 57 | + private function __has_food_kg($carry, $item) { |
|
| 58 | 58 | return $carry || 'https://knowledge.cafemedia.com/food/' === $item->dataset_uri; |
| 59 | 59 | } |
| 60 | 60 | |