@@ -14,60 +14,60 @@ |
||
| 14 | 14 | */ |
| 15 | 15 | class Background_Task_Factory { |
| 16 | 16 | |
| 17 | - /** |
|
| 18 | - * Create a {@link Background_Task} given the provided task and route. |
|
| 19 | - * |
|
| 20 | - * @param Task $task The target task. |
|
| 21 | - * @param string $route The route name. |
|
| 22 | - * @param string $page_id The page id. |
|
| 23 | - * @param string $page_title The page title. |
|
| 24 | - * |
|
| 25 | - * @return Background_Task |
|
| 26 | - * @throws Exception in case of invalid arguments. |
|
| 27 | - */ |
|
| 28 | - public static function create( $task, $route, $page_id, $page_title ) { |
|
| 29 | - self::assertions( $task, $route ); |
|
| 30 | - $background_task = Background_Task::create( $task ); |
|
| 31 | - $background_task_route = Background_Task_Route::create( $background_task, $route ); |
|
| 32 | - Background_Task_Page::create( $page_title, $page_id, $background_task_route ); |
|
| 33 | - return $background_task; |
|
| 34 | - } |
|
| 17 | + /** |
|
| 18 | + * Create a {@link Background_Task} given the provided task and route. |
|
| 19 | + * |
|
| 20 | + * @param Task $task The target task. |
|
| 21 | + * @param string $route The route name. |
|
| 22 | + * @param string $page_id The page id. |
|
| 23 | + * @param string $page_title The page title. |
|
| 24 | + * |
|
| 25 | + * @return Background_Task |
|
| 26 | + * @throws Exception in case of invalid arguments. |
|
| 27 | + */ |
|
| 28 | + public static function create( $task, $route, $page_id, $page_title ) { |
|
| 29 | + self::assertions( $task, $route ); |
|
| 30 | + $background_task = Background_Task::create( $task ); |
|
| 31 | + $background_task_route = Background_Task_Route::create( $background_task, $route ); |
|
| 32 | + Background_Task_Page::create( $page_title, $page_id, $background_task_route ); |
|
| 33 | + return $background_task; |
|
| 34 | + } |
|
| 35 | 35 | |
| 36 | - /** |
|
| 37 | - * Create a {@link Background_Task} given the provided task and route. |
|
| 38 | - * |
|
| 39 | - * @param Task $task The target task. |
|
| 40 | - * @param string $route The route name. |
|
| 41 | - * @param string $page_id The page id. |
|
| 42 | - * @param string $page_title The page title. |
|
| 43 | - * |
|
| 44 | - * @return \Wordlift\Task\Action_Scheduler\Background_Task |
|
| 45 | - * @throws Exception in case of invalid arguments. |
|
| 46 | - */ |
|
| 47 | - public static function create_action_scheduler_task( $hook, $group, $task, $route, $page_id, $page_title, $batch_size = 5 ) { |
|
| 48 | - self::assertions( $task, $route ); |
|
| 49 | - $background_task = new \Wordlift\Task\Action_Scheduler\Background_Task( |
|
| 50 | - $hook, |
|
| 51 | - $group, |
|
| 52 | - $task, |
|
| 53 | - "_{$hook}_", |
|
| 54 | - $batch_size |
|
| 55 | - ); |
|
| 56 | - $background_task_route = Background_Task_Route::create( $background_task, $route ); |
|
| 57 | - Background_Task_Page::create( $page_title, $page_id, $background_task_route ); |
|
| 58 | - return $background_task; |
|
| 59 | - } |
|
| 36 | + /** |
|
| 37 | + * Create a {@link Background_Task} given the provided task and route. |
|
| 38 | + * |
|
| 39 | + * @param Task $task The target task. |
|
| 40 | + * @param string $route The route name. |
|
| 41 | + * @param string $page_id The page id. |
|
| 42 | + * @param string $page_title The page title. |
|
| 43 | + * |
|
| 44 | + * @return \Wordlift\Task\Action_Scheduler\Background_Task |
|
| 45 | + * @throws Exception in case of invalid arguments. |
|
| 46 | + */ |
|
| 47 | + public static function create_action_scheduler_task( $hook, $group, $task, $route, $page_id, $page_title, $batch_size = 5 ) { |
|
| 48 | + self::assertions( $task, $route ); |
|
| 49 | + $background_task = new \Wordlift\Task\Action_Scheduler\Background_Task( |
|
| 50 | + $hook, |
|
| 51 | + $group, |
|
| 52 | + $task, |
|
| 53 | + "_{$hook}_", |
|
| 54 | + $batch_size |
|
| 55 | + ); |
|
| 56 | + $background_task_route = Background_Task_Route::create( $background_task, $route ); |
|
| 57 | + Background_Task_Page::create( $page_title, $page_id, $background_task_route ); |
|
| 58 | + return $background_task; |
|
| 59 | + } |
|
| 60 | 60 | |
| 61 | - /** |
|
| 62 | - * @param Task $task |
|
| 63 | - * @param $route |
|
| 64 | - * |
|
| 65 | - * @return void |
|
| 66 | - * @throws Exception in case of invalid arguments. |
|
| 67 | - */ |
|
| 68 | - private static function assertions( Task $task, $route ) { |
|
| 69 | - Assertions::is_a( $task, 'Wordlift\Task\Task' ); |
|
| 70 | - Assertions::starts_with( $route, '/', __( 'The route must start with a slash.', 'wordlift' ) ); |
|
| 71 | - } |
|
| 61 | + /** |
|
| 62 | + * @param Task $task |
|
| 63 | + * @param $route |
|
| 64 | + * |
|
| 65 | + * @return void |
|
| 66 | + * @throws Exception in case of invalid arguments. |
|
| 67 | + */ |
|
| 68 | + private static function assertions( Task $task, $route ) { |
|
| 69 | + Assertions::is_a( $task, 'Wordlift\Task\Task' ); |
|
| 70 | + Assertions::starts_with( $route, '/', __( 'The route must start with a slash.', 'wordlift' ) ); |
|
| 71 | + } |
|
| 72 | 72 | |
| 73 | 73 | } |
@@ -19,7 +19,7 @@ discard block |
||
| 19 | 19 | use Wordlift\Task\Single_Call_Task; |
| 20 | 20 | |
| 21 | 21 | if ( ! defined( 'ABSPATH' ) ) { |
| 22 | - exit; |
|
| 22 | + exit; |
|
| 23 | 23 | } |
| 24 | 24 | |
| 25 | 25 | define( 'WL_FOOD_KG_FILE', __FILE__ ); |
@@ -27,94 +27,94 @@ discard block |
||
| 27 | 27 | |
| 28 | 28 | function __wl_foodkg__load() { |
| 29 | 29 | |
| 30 | - // Autoloader for plugin itself. |
|
| 31 | - if ( file_exists( __DIR__ . '/vendor/autoload.php' ) ) { |
|
| 32 | - require __DIR__ . '/vendor/autoload.php'; |
|
| 33 | - } |
|
| 34 | - |
|
| 35 | - $container_builder = new ContainerBuilder(); |
|
| 36 | - $loader = new YamlFileLoader( $container_builder, new FileLocator( __DIR__ ) ); |
|
| 37 | - $loader->load( 'services.yml' ); |
|
| 38 | - $container_builder->compile(); |
|
| 39 | - |
|
| 40 | - $notices = $container_builder->get( 'Wordlift\Modules\Food_Kg\Notices' ); |
|
| 41 | - $notices->register_hooks(); |
|
| 42 | - |
|
| 43 | - /** |
|
| 44 | - * @var Preconditions $preconditions |
|
| 45 | - */ |
|
| 46 | - $preconditions = $container_builder->get( 'Wordlift\Modules\Food_Kg\Preconditions' ); |
|
| 47 | - if ( ! $preconditions->pass() ) { |
|
| 48 | - return; |
|
| 49 | - } |
|
| 50 | - |
|
| 51 | - // Meta Box. |
|
| 52 | - $meta_box = $container_builder->get( 'Wordlift\Modules\Food_Kg\Admin\Meta_Box' ); |
|
| 53 | - $meta_box->register_hooks(); |
|
| 54 | - |
|
| 55 | - $module = $container_builder->get( 'Wordlift\Modules\Food_Kg\Module' ); |
|
| 56 | - $module->register_hooks(); |
|
| 57 | - |
|
| 58 | - /** @var Jsonld $jsonld */ |
|
| 59 | - $jsonld = $container_builder->get( 'Wordlift\Modules\Food_Kg\Jsonld' ); |
|
| 60 | - $jsonld->register_hooks(); |
|
| 61 | - |
|
| 62 | - /** |
|
| 63 | - * Ingredients API. |
|
| 64 | - */ |
|
| 65 | - $ingredients_api = $container_builder->get( 'Wordlift\Modules\Food_Kg\Ingredients_API' ); |
|
| 66 | - $ingredients_api->register_hooks(); |
|
| 67 | - |
|
| 68 | - /** @var Main_Ingredient_Jsonld $jsonld */ |
|
| 69 | - $main_ingredient_jsonld = $container_builder->get( 'Wordlift\Modules\Food_Kg\Main_Ingredient_Jsonld' ); |
|
| 70 | - $main_ingredient_jsonld->register_hooks(); |
|
| 71 | - |
|
| 72 | - // Main Ingredient Background Task. |
|
| 73 | - $main_ingredient_recipe_lift = $container_builder->get( 'Wordlift\Modules\Food_Kg\Main_Ingredient_Recipe_Lift_Strategy' ); |
|
| 74 | - $main_ingredient_background_task = Background_Task_Factory::create_action_scheduler_task( |
|
| 75 | - 'wl_main_ingredient_sync', |
|
| 76 | - 'wordlift', |
|
| 77 | - new All_Posts_Task( |
|
| 78 | - array( $main_ingredient_recipe_lift, 'process' ), |
|
| 79 | - 'wprm_recipe', |
|
| 80 | - 'sync-main-ingredient' |
|
| 81 | - ), |
|
| 82 | - '/main-ingredient', |
|
| 83 | - 'sync-main-ingredient', |
|
| 84 | - __( 'Synchronize Main Ingredient', 'wordlift' ) |
|
| 85 | - ); |
|
| 86 | - |
|
| 87 | - // Ingredients Taxonomy Background Task. |
|
| 88 | - $ingredients_taxonomy_recipe_lift = $container_builder->get( 'Wordlift\Modules\Food_Kg\Ingredients_Taxonomy_Recipe_Lift_Strategy' ); |
|
| 89 | - $ingredients_taxonomy_background_task = Background_Task_Factory::create( |
|
| 90 | - new Single_Call_Task( |
|
| 91 | - array( $ingredients_taxonomy_recipe_lift, 'run' ), |
|
| 92 | - 'sync-ingredients-taxonomy' |
|
| 93 | - ), |
|
| 94 | - '/sync-ingredients-taxonomy', |
|
| 95 | - 'sync-ingredients-taxonomy', |
|
| 96 | - __( 'Synchronize Ingredients Taxonomy', 'wordlift' ) |
|
| 97 | - ); |
|
| 98 | - |
|
| 99 | - add_action( |
|
| 100 | - $module::RUN_EVENT, |
|
| 101 | - function () use ( $main_ingredient_background_task, $ingredients_taxonomy_background_task ) { |
|
| 102 | - $main_ingredient_background_task->start(); |
|
| 103 | - $ingredients_taxonomy_background_task->start(); |
|
| 104 | - } |
|
| 105 | - ); |
|
| 106 | - |
|
| 107 | - if ( is_admin() ) { |
|
| 108 | - $page = $container_builder->get( 'Wordlift\Modules\Food_Kg\Admin\Page' ); |
|
| 109 | - $page->register_hooks(); |
|
| 110 | - |
|
| 111 | - $page = $container_builder->get( 'Wordlift\Modules\Food_Kg\Admin\Ingredients_Taxonomy_Page' ); |
|
| 112 | - $page->register_hooks(); |
|
| 113 | - |
|
| 114 | - // Download Ingredients Data. |
|
| 115 | - $download_ingredients_data = $container_builder->get( 'Wordlift\Modules\Food_Kg\Admin\Download_Ingredients_Data' ); |
|
| 116 | - $download_ingredients_data->register_hooks(); |
|
| 117 | - } |
|
| 30 | + // Autoloader for plugin itself. |
|
| 31 | + if ( file_exists( __DIR__ . '/vendor/autoload.php' ) ) { |
|
| 32 | + require __DIR__ . '/vendor/autoload.php'; |
|
| 33 | + } |
|
| 34 | + |
|
| 35 | + $container_builder = new ContainerBuilder(); |
|
| 36 | + $loader = new YamlFileLoader( $container_builder, new FileLocator( __DIR__ ) ); |
|
| 37 | + $loader->load( 'services.yml' ); |
|
| 38 | + $container_builder->compile(); |
|
| 39 | + |
|
| 40 | + $notices = $container_builder->get( 'Wordlift\Modules\Food_Kg\Notices' ); |
|
| 41 | + $notices->register_hooks(); |
|
| 42 | + |
|
| 43 | + /** |
|
| 44 | + * @var Preconditions $preconditions |
|
| 45 | + */ |
|
| 46 | + $preconditions = $container_builder->get( 'Wordlift\Modules\Food_Kg\Preconditions' ); |
|
| 47 | + if ( ! $preconditions->pass() ) { |
|
| 48 | + return; |
|
| 49 | + } |
|
| 50 | + |
|
| 51 | + // Meta Box. |
|
| 52 | + $meta_box = $container_builder->get( 'Wordlift\Modules\Food_Kg\Admin\Meta_Box' ); |
|
| 53 | + $meta_box->register_hooks(); |
|
| 54 | + |
|
| 55 | + $module = $container_builder->get( 'Wordlift\Modules\Food_Kg\Module' ); |
|
| 56 | + $module->register_hooks(); |
|
| 57 | + |
|
| 58 | + /** @var Jsonld $jsonld */ |
|
| 59 | + $jsonld = $container_builder->get( 'Wordlift\Modules\Food_Kg\Jsonld' ); |
|
| 60 | + $jsonld->register_hooks(); |
|
| 61 | + |
|
| 62 | + /** |
|
| 63 | + * Ingredients API. |
|
| 64 | + */ |
|
| 65 | + $ingredients_api = $container_builder->get( 'Wordlift\Modules\Food_Kg\Ingredients_API' ); |
|
| 66 | + $ingredients_api->register_hooks(); |
|
| 67 | + |
|
| 68 | + /** @var Main_Ingredient_Jsonld $jsonld */ |
|
| 69 | + $main_ingredient_jsonld = $container_builder->get( 'Wordlift\Modules\Food_Kg\Main_Ingredient_Jsonld' ); |
|
| 70 | + $main_ingredient_jsonld->register_hooks(); |
|
| 71 | + |
|
| 72 | + // Main Ingredient Background Task. |
|
| 73 | + $main_ingredient_recipe_lift = $container_builder->get( 'Wordlift\Modules\Food_Kg\Main_Ingredient_Recipe_Lift_Strategy' ); |
|
| 74 | + $main_ingredient_background_task = Background_Task_Factory::create_action_scheduler_task( |
|
| 75 | + 'wl_main_ingredient_sync', |
|
| 76 | + 'wordlift', |
|
| 77 | + new All_Posts_Task( |
|
| 78 | + array( $main_ingredient_recipe_lift, 'process' ), |
|
| 79 | + 'wprm_recipe', |
|
| 80 | + 'sync-main-ingredient' |
|
| 81 | + ), |
|
| 82 | + '/main-ingredient', |
|
| 83 | + 'sync-main-ingredient', |
|
| 84 | + __( 'Synchronize Main Ingredient', 'wordlift' ) |
|
| 85 | + ); |
|
| 86 | + |
|
| 87 | + // Ingredients Taxonomy Background Task. |
|
| 88 | + $ingredients_taxonomy_recipe_lift = $container_builder->get( 'Wordlift\Modules\Food_Kg\Ingredients_Taxonomy_Recipe_Lift_Strategy' ); |
|
| 89 | + $ingredients_taxonomy_background_task = Background_Task_Factory::create( |
|
| 90 | + new Single_Call_Task( |
|
| 91 | + array( $ingredients_taxonomy_recipe_lift, 'run' ), |
|
| 92 | + 'sync-ingredients-taxonomy' |
|
| 93 | + ), |
|
| 94 | + '/sync-ingredients-taxonomy', |
|
| 95 | + 'sync-ingredients-taxonomy', |
|
| 96 | + __( 'Synchronize Ingredients Taxonomy', 'wordlift' ) |
|
| 97 | + ); |
|
| 98 | + |
|
| 99 | + add_action( |
|
| 100 | + $module::RUN_EVENT, |
|
| 101 | + function () use ( $main_ingredient_background_task, $ingredients_taxonomy_background_task ) { |
|
| 102 | + $main_ingredient_background_task->start(); |
|
| 103 | + $ingredients_taxonomy_background_task->start(); |
|
| 104 | + } |
|
| 105 | + ); |
|
| 106 | + |
|
| 107 | + if ( is_admin() ) { |
|
| 108 | + $page = $container_builder->get( 'Wordlift\Modules\Food_Kg\Admin\Page' ); |
|
| 109 | + $page->register_hooks(); |
|
| 110 | + |
|
| 111 | + $page = $container_builder->get( 'Wordlift\Modules\Food_Kg\Admin\Ingredients_Taxonomy_Page' ); |
|
| 112 | + $page->register_hooks(); |
|
| 113 | + |
|
| 114 | + // Download Ingredients Data. |
|
| 115 | + $download_ingredients_data = $container_builder->get( 'Wordlift\Modules\Food_Kg\Admin\Download_Ingredients_Data' ); |
|
| 116 | + $download_ingredients_data->register_hooks(); |
|
| 117 | + } |
|
| 118 | 118 | } |
| 119 | 119 | |
| 120 | 120 | add_action( 'plugins_loaded', '__wl_foodkg__load' ); |
@@ -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 | } |