@@ -4,58 +4,58 @@ |
||
| 4 | 4 | |
| 5 | 5 | class Ingredients_Taxonomy_Recipe_Lift_Strategy implements Recipe_Lift_Strategy { |
| 6 | 6 | |
| 7 | - /** |
|
| 8 | - * @var Ingredients_Client |
|
| 9 | - */ |
|
| 10 | - private $ingredients_client; |
|
| 11 | - |
|
| 12 | - /** |
|
| 13 | - * @var Notices |
|
| 14 | - */ |
|
| 15 | - private $notices; |
|
| 16 | - |
|
| 17 | - public function __construct( Ingredients_Client $ingredients_client, Notices $notices ) { |
|
| 18 | - $this->ingredients_client = $ingredients_client; |
|
| 19 | - $this->notices = $notices; |
|
| 20 | - } |
|
| 21 | - |
|
| 22 | - public function run() { |
|
| 23 | - $this->notices->queue( 'info', __( 'WordLift detected WP Recipe Maker and, it is lifting the ingredients...', 'wordlift' ) ); |
|
| 24 | - |
|
| 25 | - /** |
|
| 26 | - * @var string[] $terms |
|
| 27 | - */ |
|
| 28 | - $terms = get_terms( |
|
| 29 | - array( |
|
| 30 | - 'taxonomy' => 'wprm_ingredient', |
|
| 31 | - 'fields' => 'names', |
|
| 32 | - 'hide_empty' => false, |
|
| 33 | - ) |
|
| 34 | - ); |
|
| 35 | - $ingredients = $this->ingredients_client->ingredients( $terms ); |
|
| 36 | - |
|
| 37 | - foreach ( $ingredients as $key => $value ) { |
|
| 38 | - $term = get_term_by( 'name', $key, 'wprm_ingredient' ); |
|
| 39 | - if ( ! isset( $term ) ) { |
|
| 40 | - continue; |
|
| 41 | - } |
|
| 42 | - update_term_meta( $term->term_id, '_wl_jsonld', wp_slash( $value ) ); |
|
| 43 | - |
|
| 44 | - /** |
|
| 45 | - * @@todo update notification with progress |
|
| 46 | - */ |
|
| 47 | - } |
|
| 48 | - |
|
| 49 | - // Clean up caches. |
|
| 50 | - do_action( 'wl_ttl_cache_cleaner__flush' ); |
|
| 51 | - |
|
| 52 | - /** |
|
| 53 | - * @@todo add notification that procedure is complete, with information about the number of processed items vs |
|
| 54 | - * total items |
|
| 55 | - */ |
|
| 56 | - $count_terms = count( $terms ); |
|
| 57 | - $count_lifted_terms = count( $ingredients ); |
|
| 58 | - /* translators: 1: The number of lifted ingredients, 2: The total number of ingredients. */ |
|
| 59 | - $this->notices->queue( 'info', sprintf( __( 'WordLift detected WP Recipe Maker and, it lifted %1$d of %2$d ingredient(s).', 'wordlift' ), $count_lifted_terms, $count_terms ) ); |
|
| 60 | - } |
|
| 7 | + /** |
|
| 8 | + * @var Ingredients_Client |
|
| 9 | + */ |
|
| 10 | + private $ingredients_client; |
|
| 11 | + |
|
| 12 | + /** |
|
| 13 | + * @var Notices |
|
| 14 | + */ |
|
| 15 | + private $notices; |
|
| 16 | + |
|
| 17 | + public function __construct( Ingredients_Client $ingredients_client, Notices $notices ) { |
|
| 18 | + $this->ingredients_client = $ingredients_client; |
|
| 19 | + $this->notices = $notices; |
|
| 20 | + } |
|
| 21 | + |
|
| 22 | + public function run() { |
|
| 23 | + $this->notices->queue( 'info', __( 'WordLift detected WP Recipe Maker and, it is lifting the ingredients...', 'wordlift' ) ); |
|
| 24 | + |
|
| 25 | + /** |
|
| 26 | + * @var string[] $terms |
|
| 27 | + */ |
|
| 28 | + $terms = get_terms( |
|
| 29 | + array( |
|
| 30 | + 'taxonomy' => 'wprm_ingredient', |
|
| 31 | + 'fields' => 'names', |
|
| 32 | + 'hide_empty' => false, |
|
| 33 | + ) |
|
| 34 | + ); |
|
| 35 | + $ingredients = $this->ingredients_client->ingredients( $terms ); |
|
| 36 | + |
|
| 37 | + foreach ( $ingredients as $key => $value ) { |
|
| 38 | + $term = get_term_by( 'name', $key, 'wprm_ingredient' ); |
|
| 39 | + if ( ! isset( $term ) ) { |
|
| 40 | + continue; |
|
| 41 | + } |
|
| 42 | + update_term_meta( $term->term_id, '_wl_jsonld', wp_slash( $value ) ); |
|
| 43 | + |
|
| 44 | + /** |
|
| 45 | + * @@todo update notification with progress |
|
| 46 | + */ |
|
| 47 | + } |
|
| 48 | + |
|
| 49 | + // Clean up caches. |
|
| 50 | + do_action( 'wl_ttl_cache_cleaner__flush' ); |
|
| 51 | + |
|
| 52 | + /** |
|
| 53 | + * @@todo add notification that procedure is complete, with information about the number of processed items vs |
|
| 54 | + * total items |
|
| 55 | + */ |
|
| 56 | + $count_terms = count( $terms ); |
|
| 57 | + $count_lifted_terms = count( $ingredients ); |
|
| 58 | + /* translators: 1: The number of lifted ingredients, 2: The total number of ingredients. */ |
|
| 59 | + $this->notices->queue( 'info', sprintf( __( 'WordLift detected WP Recipe Maker and, it lifted %1$d of %2$d ingredient(s).', 'wordlift' ), $count_lifted_terms, $count_terms ) ); |
|
| 60 | + } |
|
| 61 | 61 | } |
@@ -4,11 +4,11 @@ |
||
| 4 | 4 | |
| 5 | 5 | class Ingredients_Full_Page_Delegate implements Page_Delegate { |
| 6 | 6 | |
| 7 | - public function render() { |
|
| 8 | - include WL_FOOD_KG_DIR_PATH . '/includes/admin/partials/ingredients.php'; |
|
| 9 | - } |
|
| 7 | + public function render() { |
|
| 8 | + include WL_FOOD_KG_DIR_PATH . '/includes/admin/partials/ingredients.php'; |
|
| 9 | + } |
|
| 10 | 10 | |
| 11 | - public function admin_enqueue_scripts() { |
|
| 12 | - } |
|
| 11 | + public function admin_enqueue_scripts() { |
|
| 12 | + } |
|
| 13 | 13 | |
| 14 | 14 | } |
@@ -3,38 +3,38 @@ discard block |
||
| 3 | 3 | namespace Wordlift\Modules\Food_Kg\Admin; |
| 4 | 4 | |
| 5 | 5 | if ( ! class_exists( 'WP_List_Table' ) ) { |
| 6 | - require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php'; |
|
| 6 | + require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php'; |
|
| 7 | 7 | } |
| 8 | 8 | |
| 9 | 9 | use WP_List_Table; |
| 10 | 10 | |
| 11 | 11 | class Ingredients_List_Table extends WP_List_Table { |
| 12 | 12 | |
| 13 | - public function prepare_items() { |
|
| 14 | - global $wpdb; // This is used only if making any database queries |
|
| 15 | - |
|
| 16 | - /** |
|
| 17 | - * REQUIRED. Now we need to define our column headers. This includes a complete |
|
| 18 | - * array of columns to be displayed (slugs & titles), a list of columns |
|
| 19 | - * to keep hidden, and a list of columns that are sortable. Each of these |
|
| 20 | - * can be defined in another method (as we've done here) before being |
|
| 21 | - * used to build the value for our _column_headers property. |
|
| 22 | - */ |
|
| 23 | - $columns = $this->get_columns(); |
|
| 24 | - $hidden = array(); |
|
| 25 | - $sortable = $this->get_sortable_columns(); |
|
| 26 | - |
|
| 27 | - /** |
|
| 28 | - * REQUIRED. Finally, we build an array to be used by the class for column |
|
| 29 | - * headers. The $this->_column_headers property takes an array which contains |
|
| 30 | - * 3 other arrays. One for all columns, one for hidden columns, and one |
|
| 31 | - * for sortable columns. |
|
| 32 | - */ |
|
| 33 | - $this->_column_headers = array( $columns, $hidden, $sortable ); |
|
| 34 | - |
|
| 35 | - $this->items = $wpdb->get_results( |
|
| 36 | - $wpdb->prepare( |
|
| 37 | - "SELECT t.term_id, t.name, tm.meta_value |
|
| 13 | + public function prepare_items() { |
|
| 14 | + global $wpdb; // This is used only if making any database queries |
|
| 15 | + |
|
| 16 | + /** |
|
| 17 | + * REQUIRED. Now we need to define our column headers. This includes a complete |
|
| 18 | + * array of columns to be displayed (slugs & titles), a list of columns |
|
| 19 | + * to keep hidden, and a list of columns that are sortable. Each of these |
|
| 20 | + * can be defined in another method (as we've done here) before being |
|
| 21 | + * used to build the value for our _column_headers property. |
|
| 22 | + */ |
|
| 23 | + $columns = $this->get_columns(); |
|
| 24 | + $hidden = array(); |
|
| 25 | + $sortable = $this->get_sortable_columns(); |
|
| 26 | + |
|
| 27 | + /** |
|
| 28 | + * REQUIRED. Finally, we build an array to be used by the class for column |
|
| 29 | + * headers. The $this->_column_headers property takes an array which contains |
|
| 30 | + * 3 other arrays. One for all columns, one for hidden columns, and one |
|
| 31 | + * for sortable columns. |
|
| 32 | + */ |
|
| 33 | + $this->_column_headers = array( $columns, $hidden, $sortable ); |
|
| 34 | + |
|
| 35 | + $this->items = $wpdb->get_results( |
|
| 36 | + $wpdb->prepare( |
|
| 37 | + "SELECT t.term_id, t.name, tm.meta_value |
|
| 38 | 38 | FROM $wpdb->term_taxonomy tt |
| 39 | 39 | INNER JOIN $wpdb->terms t |
| 40 | 40 | ON t.term_id = tt.term_id |
@@ -42,44 +42,44 @@ discard block |
||
| 42 | 42 | ON tm.term_id = t.term_id |
| 43 | 43 | AND tm.meta_key = '_wl_jsonld' |
| 44 | 44 | WHERE tt.taxonomy = %s;", |
| 45 | - 'wprm_ingredient' |
|
| 46 | - ) |
|
| 47 | - ); |
|
| 48 | - } |
|
| 49 | - |
|
| 50 | - public function no_items() { |
|
| 51 | - esc_html_e( 'No ingredients found.', 'wordlift' ); |
|
| 52 | - } |
|
| 53 | - |
|
| 54 | - public function get_columns() { |
|
| 55 | - return array( |
|
| 56 | - 'id' => __( 'ID', 'wordlift' ), |
|
| 57 | - 'name' => __( 'Name', 'wordlift' ), |
|
| 58 | - 'actions' => '', |
|
| 59 | - ); |
|
| 60 | - } |
|
| 61 | - |
|
| 62 | - public function column_id( $item ) { |
|
| 63 | - return $item->term_id; |
|
| 64 | - } |
|
| 65 | - |
|
| 66 | - public function column_name( $item ) { |
|
| 67 | - return $item->name; |
|
| 68 | - } |
|
| 69 | - |
|
| 70 | - public function column_actions( $item ) { |
|
| 71 | - |
|
| 72 | - $url = admin_url( |
|
| 73 | - sprintf( 'admin.php?page=wl_ingredients&modal_window=true&term_id=%d&TB_iframe=true', $item->term_id ) |
|
| 74 | - ); |
|
| 75 | - |
|
| 76 | - return sprintf( |
|
| 77 | - '<a href="%s" class="button alignright thickbox open-plugin-details-modal" data-title="%s" type="button">%s</a>', |
|
| 78 | - $url, |
|
| 79 | - esc_attr( $item->name ), |
|
| 80 | - esc_html__( 'JSON-LD', 'wordlift' ) |
|
| 81 | - ); |
|
| 82 | - } |
|
| 45 | + 'wprm_ingredient' |
|
| 46 | + ) |
|
| 47 | + ); |
|
| 48 | + } |
|
| 49 | + |
|
| 50 | + public function no_items() { |
|
| 51 | + esc_html_e( 'No ingredients found.', 'wordlift' ); |
|
| 52 | + } |
|
| 53 | + |
|
| 54 | + public function get_columns() { |
|
| 55 | + return array( |
|
| 56 | + 'id' => __( 'ID', 'wordlift' ), |
|
| 57 | + 'name' => __( 'Name', 'wordlift' ), |
|
| 58 | + 'actions' => '', |
|
| 59 | + ); |
|
| 60 | + } |
|
| 61 | + |
|
| 62 | + public function column_id( $item ) { |
|
| 63 | + return $item->term_id; |
|
| 64 | + } |
|
| 65 | + |
|
| 66 | + public function column_name( $item ) { |
|
| 67 | + return $item->name; |
|
| 68 | + } |
|
| 69 | + |
|
| 70 | + public function column_actions( $item ) { |
|
| 71 | + |
|
| 72 | + $url = admin_url( |
|
| 73 | + sprintf( 'admin.php?page=wl_ingredients&modal_window=true&term_id=%d&TB_iframe=true', $item->term_id ) |
|
| 74 | + ); |
|
| 75 | + |
|
| 76 | + return sprintf( |
|
| 77 | + '<a href="%s" class="button alignright thickbox open-plugin-details-modal" data-title="%s" type="button">%s</a>', |
|
| 78 | + $url, |
|
| 79 | + esc_attr( $item->name ), |
|
| 80 | + esc_html__( 'JSON-LD', 'wordlift' ) |
|
| 81 | + ); |
|
| 82 | + } |
|
| 83 | 83 | |
| 84 | 84 | } |
| 85 | 85 | |
@@ -4,61 +4,61 @@ |
||
| 4 | 4 | |
| 5 | 5 | class Page { |
| 6 | 6 | |
| 7 | - /** |
|
| 8 | - * @var Page_Delegate |
|
| 9 | - */ |
|
| 10 | - private $delegate; |
|
| 11 | - private $menu_slug; |
|
| 12 | - private $page_title; |
|
| 13 | - private $menu_title; |
|
| 7 | + /** |
|
| 8 | + * @var Page_Delegate |
|
| 9 | + */ |
|
| 10 | + private $delegate; |
|
| 11 | + private $menu_slug; |
|
| 12 | + private $page_title; |
|
| 13 | + private $menu_title; |
|
| 14 | 14 | |
| 15 | - /** |
|
| 16 | - * @param Page_Delegate $full_page_delegate |
|
| 17 | - * @param Page_Delegate $modal_page_delegate |
|
| 18 | - */ |
|
| 19 | - public function __construct( $full_page_delegate, $modal_page_delegate, $menu_slug, $page_title, $menu_title ) { |
|
| 20 | - $this->delegate = isset( $_GET['modal_window'] ) ? $modal_page_delegate : $full_page_delegate; //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
| 21 | - $this->menu_slug = $menu_slug; |
|
| 22 | - $this->page_title = $page_title; |
|
| 23 | - $this->menu_title = $menu_title; |
|
| 24 | - } |
|
| 15 | + /** |
|
| 16 | + * @param Page_Delegate $full_page_delegate |
|
| 17 | + * @param Page_Delegate $modal_page_delegate |
|
| 18 | + */ |
|
| 19 | + public function __construct( $full_page_delegate, $modal_page_delegate, $menu_slug, $page_title, $menu_title ) { |
|
| 20 | + $this->delegate = isset( $_GET['modal_window'] ) ? $modal_page_delegate : $full_page_delegate; //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
| 21 | + $this->menu_slug = $menu_slug; |
|
| 22 | + $this->page_title = $page_title; |
|
| 23 | + $this->menu_title = $menu_title; |
|
| 24 | + } |
|
| 25 | 25 | |
| 26 | - public function register_hooks() { |
|
| 27 | - add_action( 'admin_menu', array( $this, 'admin_menu' ) ); |
|
| 28 | - add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) ); |
|
| 29 | - if ( isset( $_GET['modal_window'] ) ) { //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
| 30 | - add_action( 'in_admin_header', array( $this, 'remove_notices' ), 99 ); |
|
| 31 | - } |
|
| 32 | - } |
|
| 26 | + public function register_hooks() { |
|
| 27 | + add_action( 'admin_menu', array( $this, 'admin_menu' ) ); |
|
| 28 | + add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) ); |
|
| 29 | + if ( isset( $_GET['modal_window'] ) ) { //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
|
| 30 | + add_action( 'in_admin_header', array( $this, 'remove_notices' ), 99 ); |
|
| 31 | + } |
|
| 32 | + } |
|
| 33 | 33 | |
| 34 | - /** |
|
| 35 | - * Remove notices from ingredients modal. |
|
| 36 | - */ |
|
| 37 | - public function remove_notices() { |
|
| 38 | - remove_all_actions( 'admin_notices' ); |
|
| 39 | - remove_all_actions( 'all_admin_notices' ); |
|
| 40 | - } |
|
| 34 | + /** |
|
| 35 | + * Remove notices from ingredients modal. |
|
| 36 | + */ |
|
| 37 | + public function remove_notices() { |
|
| 38 | + remove_all_actions( 'admin_notices' ); |
|
| 39 | + remove_all_actions( 'all_admin_notices' ); |
|
| 40 | + } |
|
| 41 | 41 | |
| 42 | - public function admin_menu() { |
|
| 43 | - add_submenu_page( |
|
| 44 | - 'wl_admin_menu', |
|
| 45 | - $this->page_title, |
|
| 46 | - $this->menu_title, |
|
| 47 | - 'manage_options', |
|
| 48 | - $this->menu_slug, |
|
| 49 | - array( $this, 'render' ) |
|
| 50 | - ); |
|
| 51 | - } |
|
| 42 | + public function admin_menu() { |
|
| 43 | + add_submenu_page( |
|
| 44 | + 'wl_admin_menu', |
|
| 45 | + $this->page_title, |
|
| 46 | + $this->menu_title, |
|
| 47 | + 'manage_options', |
|
| 48 | + $this->menu_slug, |
|
| 49 | + array( $this, 'render' ) |
|
| 50 | + ); |
|
| 51 | + } |
|
| 52 | 52 | |
| 53 | - public function render() { |
|
| 54 | - $this->delegate->render(); |
|
| 55 | - } |
|
| 53 | + public function render() { |
|
| 54 | + $this->delegate->render(); |
|
| 55 | + } |
|
| 56 | 56 | |
| 57 | - public function admin_enqueue_scripts() { |
|
| 58 | - // Check that we are on the right screen |
|
| 59 | - if ( get_current_screen()->id === 'wordlift_page_wl_ingredients' ) { |
|
| 60 | - $this->delegate->admin_enqueue_scripts(); |
|
| 61 | - } |
|
| 62 | - } |
|
| 57 | + public function admin_enqueue_scripts() { |
|
| 58 | + // Check that we are on the right screen |
|
| 59 | + if ( get_current_screen()->id === 'wordlift_page_wl_ingredients' ) { |
|
| 60 | + $this->delegate->admin_enqueue_scripts(); |
|
| 61 | + } |
|
| 62 | + } |
|
| 63 | 63 | |
| 64 | 64 | } |
@@ -14,24 +14,24 @@ |
||
| 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 void |
|
| 26 | - * @throws Exception in case of invalid arguments. |
|
| 27 | - */ |
|
| 28 | - public static function create( $task, $route, $page_id, $page_title ) { |
|
| 29 | - Assertions::is_a( $task, 'Wordlift\Task\Task' ); |
|
| 30 | - Assertions::starts_with( $route, '/', __( 'The route must start with a slash.', 'wordlift' ) ); |
|
| 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 void |
|
| 26 | + * @throws Exception in case of invalid arguments. |
|
| 27 | + */ |
|
| 28 | + public static function create( $task, $route, $page_id, $page_title ) { |
|
| 29 | + Assertions::is_a( $task, 'Wordlift\Task\Task' ); |
|
| 30 | + Assertions::starts_with( $route, '/', __( 'The route must start with a slash.', 'wordlift' ) ); |
|
| 31 | 31 | |
| 32 | - $background_task = Background_Task::create( $task ); |
|
| 33 | - $background_task_route = Background_Task_Route::create( $background_task, $route ); |
|
| 34 | - Background_Task_Page::create( $page_title, $page_id, $background_task_route ); |
|
| 35 | - } |
|
| 32 | + $background_task = Background_Task::create( $task ); |
|
| 33 | + $background_task_route = Background_Task_Route::create( $background_task, $route ); |
|
| 34 | + Background_Task_Page::create( $page_title, $page_id, $background_task_route ); |
|
| 35 | + } |
|
| 36 | 36 | |
| 37 | 37 | } |
@@ -4,42 +4,42 @@ |
||
| 4 | 4 | |
| 5 | 5 | class Single_Call_Task implements Task { |
| 6 | 6 | |
| 7 | - private $callable; |
|
| 8 | - |
|
| 9 | - /** |
|
| 10 | - * @var string|null |
|
| 11 | - */ |
|
| 12 | - private $id; |
|
| 13 | - |
|
| 14 | - public function __construct( $callable, $id = null ) { |
|
| 15 | - $this->callable = $callable; |
|
| 16 | - $this->id = $id; |
|
| 17 | - } |
|
| 18 | - |
|
| 19 | - public function get_id() { |
|
| 20 | - return isset( $this->id ) ? $this->id : sha1( get_class( $this ) ); |
|
| 21 | - } |
|
| 22 | - |
|
| 23 | - public function starting() { |
|
| 24 | - return 1; |
|
| 25 | - } |
|
| 26 | - |
|
| 27 | - /** |
|
| 28 | - * @param $value mixed The incoming value. |
|
| 29 | - * @param $args { |
|
| 30 | - * |
|
| 31 | - * @type int $offset The index of the current item. |
|
| 32 | - * @type int $count The total number of items as provided by the `starting` function. |
|
| 33 | - * @type int $batch_size The number of items to process within this call. |
|
| 34 | - * } |
|
| 35 | - * |
|
| 36 | - * @return void |
|
| 37 | - */ |
|
| 38 | - // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
|
| 39 | - public function tick( $value, $args ) { |
|
| 40 | - |
|
| 41 | - call_user_func( $this->callable, 1 ); |
|
| 42 | - |
|
| 43 | - } |
|
| 7 | + private $callable; |
|
| 8 | + |
|
| 9 | + /** |
|
| 10 | + * @var string|null |
|
| 11 | + */ |
|
| 12 | + private $id; |
|
| 13 | + |
|
| 14 | + public function __construct( $callable, $id = null ) { |
|
| 15 | + $this->callable = $callable; |
|
| 16 | + $this->id = $id; |
|
| 17 | + } |
|
| 18 | + |
|
| 19 | + public function get_id() { |
|
| 20 | + return isset( $this->id ) ? $this->id : sha1( get_class( $this ) ); |
|
| 21 | + } |
|
| 22 | + |
|
| 23 | + public function starting() { |
|
| 24 | + return 1; |
|
| 25 | + } |
|
| 26 | + |
|
| 27 | + /** |
|
| 28 | + * @param $value mixed The incoming value. |
|
| 29 | + * @param $args { |
|
| 30 | + * |
|
| 31 | + * @type int $offset The index of the current item. |
|
| 32 | + * @type int $count The total number of items as provided by the `starting` function. |
|
| 33 | + * @type int $batch_size The number of items to process within this call. |
|
| 34 | + * } |
|
| 35 | + * |
|
| 36 | + * @return void |
|
| 37 | + */ |
|
| 38 | + // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
|
| 39 | + public function tick( $value, $args ) { |
|
| 40 | + |
|
| 41 | + call_user_func( $this->callable, 1 ); |
|
| 42 | + |
|
| 43 | + } |
|
| 44 | 44 | |
| 45 | 45 | } |
@@ -19,96 +19,96 @@ |
||
| 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__ ); |
| 26 | 26 | define( 'WL_FOOD_KG_DIR_PATH', dirname( WL_FOOD_KG_FILE ) ); |
| 27 | 27 | |
| 28 | 28 | function __wl_foodkg__load() { |
| 29 | - // Autoloader for dependencies. |
|
| 30 | - if ( file_exists( WL_FOOD_KG_DIR_PATH . '/third-party/vendor/scoper-autoload.php' ) ) { |
|
| 31 | - require WL_FOOD_KG_DIR_PATH . '/third-party/vendor/scoper-autoload.php'; |
|
| 32 | - } |
|
| 33 | - |
|
| 34 | - // Autoloader for plugin itself. |
|
| 35 | - if ( file_exists( WL_FOOD_KG_DIR_PATH . '/includes/vendor/autoload.php' ) ) { |
|
| 36 | - require WL_FOOD_KG_DIR_PATH . '/includes/vendor/autoload.php'; |
|
| 37 | - } |
|
| 38 | - |
|
| 39 | - $container_builder = new ContainerBuilder(); |
|
| 40 | - $loader = new YamlFileLoader( $container_builder, new FileLocator( __DIR__ ) ); |
|
| 41 | - $loader->load( 'services.yml' ); |
|
| 42 | - $container_builder->compile(); |
|
| 43 | - |
|
| 44 | - $notices = $container_builder->get( 'Wordlift\Modules\Food_Kg\Notices' ); |
|
| 45 | - $notices->register_hooks(); |
|
| 46 | - |
|
| 47 | - /** |
|
| 48 | - * @var Preconditions $preconditions |
|
| 49 | - */ |
|
| 50 | - $preconditions = $container_builder->get( 'Wordlift\Modules\Food_Kg\Preconditions' ); |
|
| 51 | - if ( ! $preconditions->pass() ) { |
|
| 52 | - return; |
|
| 53 | - } |
|
| 54 | - |
|
| 55 | - // Meta Box. |
|
| 56 | - $meta_box = $container_builder->get( 'Wordlift\Modules\Food_Kg\Admin\Meta_Box' ); |
|
| 57 | - $meta_box->register_hooks(); |
|
| 58 | - |
|
| 59 | - $module = $container_builder->get( 'Wordlift\Modules\Food_Kg\Module' ); |
|
| 60 | - $module->register_hooks(); |
|
| 61 | - |
|
| 62 | - /** @var Jsonld $jsonld */ |
|
| 63 | - $jsonld = $container_builder->get( 'Wordlift\Modules\Food_Kg\Jsonld' ); |
|
| 64 | - $jsonld->register_hooks(); |
|
| 65 | - |
|
| 66 | - /** |
|
| 67 | - * Ingredients API. |
|
| 68 | - */ |
|
| 69 | - $ingredients_api = $container_builder->get( 'Wordlift\Modules\Food_Kg\Ingredients_API' ); |
|
| 70 | - $ingredients_api->register_hooks(); |
|
| 71 | - |
|
| 72 | - /** @var Main_Ingredient_Jsonld $jsonld */ |
|
| 73 | - $main_ingredient_jsonld = $container_builder->get( 'Wordlift\Modules\Food_Kg\Main_Ingredient_Jsonld' ); |
|
| 74 | - $main_ingredient_jsonld->register_hooks(); |
|
| 75 | - |
|
| 76 | - // Main Ingredient Background Task. |
|
| 77 | - $main_ingredient_recipe_lift = $container_builder->get( 'Wordlift\Modules\Food_Kg\Main_Ingredient_Recipe_Lift_Strategy' ); |
|
| 78 | - Background_Task_Factory::create( |
|
| 79 | - new All_Posts_Task( |
|
| 80 | - array( $main_ingredient_recipe_lift, 'process' ), |
|
| 81 | - 'wprm_recipe', |
|
| 82 | - 'sync-main-ingredient' |
|
| 83 | - ), |
|
| 84 | - '/main-ingredient', |
|
| 85 | - 'sync-main-ingredient', |
|
| 86 | - __( 'Synchronize Main Ingredient', 'wordlift' ) |
|
| 87 | - ); |
|
| 88 | - |
|
| 89 | - // Ingredients Taxonomy Background Task. |
|
| 90 | - $ingredients_taxonomy_recipe_lift = $container_builder->get( 'Wordlift\Modules\Food_Kg\Ingredients_Taxonomy_Recipe_Lift_Strategy' ); |
|
| 91 | - Background_Task_Factory::create( |
|
| 92 | - new Single_Call_Task( |
|
| 93 | - array( $ingredients_taxonomy_recipe_lift, 'run' ), |
|
| 94 | - 'sync-ingredients-taxonomy' |
|
| 95 | - ), |
|
| 96 | - '/sync-ingredients-taxonomy', |
|
| 97 | - 'sync-ingredients-taxonomy', |
|
| 98 | - __( 'Synchronize Ingredients Taxonomy', 'wordlift' ) |
|
| 99 | - ); |
|
| 100 | - |
|
| 101 | - if ( is_admin() ) { |
|
| 102 | - $page = $container_builder->get( 'Wordlift\Modules\Food_Kg\Admin\Page' ); |
|
| 103 | - $page->register_hooks(); |
|
| 104 | - |
|
| 105 | - $page = $container_builder->get( 'Wordlift\Modules\Food_Kg\Admin\Ingredients_Taxonomy_Page' ); |
|
| 106 | - $page->register_hooks(); |
|
| 107 | - |
|
| 108 | - // Download Ingredients Data. |
|
| 109 | - $download_ingredients_data = $container_builder->get( 'Wordlift\Modules\Food_Kg\Admin\Download_Ingredients_Data' ); |
|
| 110 | - $download_ingredients_data->register_hooks(); |
|
| 111 | - } |
|
| 29 | + // Autoloader for dependencies. |
|
| 30 | + if ( file_exists( WL_FOOD_KG_DIR_PATH . '/third-party/vendor/scoper-autoload.php' ) ) { |
|
| 31 | + require WL_FOOD_KG_DIR_PATH . '/third-party/vendor/scoper-autoload.php'; |
|
| 32 | + } |
|
| 33 | + |
|
| 34 | + // Autoloader for plugin itself. |
|
| 35 | + if ( file_exists( WL_FOOD_KG_DIR_PATH . '/includes/vendor/autoload.php' ) ) { |
|
| 36 | + require WL_FOOD_KG_DIR_PATH . '/includes/vendor/autoload.php'; |
|
| 37 | + } |
|
| 38 | + |
|
| 39 | + $container_builder = new ContainerBuilder(); |
|
| 40 | + $loader = new YamlFileLoader( $container_builder, new FileLocator( __DIR__ ) ); |
|
| 41 | + $loader->load( 'services.yml' ); |
|
| 42 | + $container_builder->compile(); |
|
| 43 | + |
|
| 44 | + $notices = $container_builder->get( 'Wordlift\Modules\Food_Kg\Notices' ); |
|
| 45 | + $notices->register_hooks(); |
|
| 46 | + |
|
| 47 | + /** |
|
| 48 | + * @var Preconditions $preconditions |
|
| 49 | + */ |
|
| 50 | + $preconditions = $container_builder->get( 'Wordlift\Modules\Food_Kg\Preconditions' ); |
|
| 51 | + if ( ! $preconditions->pass() ) { |
|
| 52 | + return; |
|
| 53 | + } |
|
| 54 | + |
|
| 55 | + // Meta Box. |
|
| 56 | + $meta_box = $container_builder->get( 'Wordlift\Modules\Food_Kg\Admin\Meta_Box' ); |
|
| 57 | + $meta_box->register_hooks(); |
|
| 58 | + |
|
| 59 | + $module = $container_builder->get( 'Wordlift\Modules\Food_Kg\Module' ); |
|
| 60 | + $module->register_hooks(); |
|
| 61 | + |
|
| 62 | + /** @var Jsonld $jsonld */ |
|
| 63 | + $jsonld = $container_builder->get( 'Wordlift\Modules\Food_Kg\Jsonld' ); |
|
| 64 | + $jsonld->register_hooks(); |
|
| 65 | + |
|
| 66 | + /** |
|
| 67 | + * Ingredients API. |
|
| 68 | + */ |
|
| 69 | + $ingredients_api = $container_builder->get( 'Wordlift\Modules\Food_Kg\Ingredients_API' ); |
|
| 70 | + $ingredients_api->register_hooks(); |
|
| 71 | + |
|
| 72 | + /** @var Main_Ingredient_Jsonld $jsonld */ |
|
| 73 | + $main_ingredient_jsonld = $container_builder->get( 'Wordlift\Modules\Food_Kg\Main_Ingredient_Jsonld' ); |
|
| 74 | + $main_ingredient_jsonld->register_hooks(); |
|
| 75 | + |
|
| 76 | + // Main Ingredient Background Task. |
|
| 77 | + $main_ingredient_recipe_lift = $container_builder->get( 'Wordlift\Modules\Food_Kg\Main_Ingredient_Recipe_Lift_Strategy' ); |
|
| 78 | + Background_Task_Factory::create( |
|
| 79 | + new All_Posts_Task( |
|
| 80 | + array( $main_ingredient_recipe_lift, 'process' ), |
|
| 81 | + 'wprm_recipe', |
|
| 82 | + 'sync-main-ingredient' |
|
| 83 | + ), |
|
| 84 | + '/main-ingredient', |
|
| 85 | + 'sync-main-ingredient', |
|
| 86 | + __( 'Synchronize Main Ingredient', 'wordlift' ) |
|
| 87 | + ); |
|
| 88 | + |
|
| 89 | + // Ingredients Taxonomy Background Task. |
|
| 90 | + $ingredients_taxonomy_recipe_lift = $container_builder->get( 'Wordlift\Modules\Food_Kg\Ingredients_Taxonomy_Recipe_Lift_Strategy' ); |
|
| 91 | + Background_Task_Factory::create( |
|
| 92 | + new Single_Call_Task( |
|
| 93 | + array( $ingredients_taxonomy_recipe_lift, 'run' ), |
|
| 94 | + 'sync-ingredients-taxonomy' |
|
| 95 | + ), |
|
| 96 | + '/sync-ingredients-taxonomy', |
|
| 97 | + 'sync-ingredients-taxonomy', |
|
| 98 | + __( 'Synchronize Ingredients Taxonomy', 'wordlift' ) |
|
| 99 | + ); |
|
| 100 | + |
|
| 101 | + if ( is_admin() ) { |
|
| 102 | + $page = $container_builder->get( 'Wordlift\Modules\Food_Kg\Admin\Page' ); |
|
| 103 | + $page->register_hooks(); |
|
| 104 | + |
|
| 105 | + $page = $container_builder->get( 'Wordlift\Modules\Food_Kg\Admin\Ingredients_Taxonomy_Page' ); |
|
| 106 | + $page->register_hooks(); |
|
| 107 | + |
|
| 108 | + // Download Ingredients Data. |
|
| 109 | + $download_ingredients_data = $container_builder->get( 'Wordlift\Modules\Food_Kg\Admin\Download_Ingredients_Data' ); |
|
| 110 | + $download_ingredients_data->register_hooks(); |
|
| 111 | + } |
|
| 112 | 112 | } |
| 113 | 113 | |
| 114 | 114 | add_action( 'plugins_loaded', '__wl_foodkg__load' ); |
@@ -7,40 +7,40 @@ |
||
| 7 | 7 | |
| 8 | 8 | class Jsonld { |
| 9 | 9 | |
| 10 | - public function register_hooks() { |
|
| 11 | - add_action( 'wprm_recipe_metadata', array( $this, '__recipe_metadata' ), 10, 2 ); |
|
| 12 | - } |
|
| 13 | - |
|
| 14 | - /** |
|
| 15 | - * @param array $metadata |
|
| 16 | - * @param WPRM_Recipe $recipe |
|
| 17 | - * |
|
| 18 | - * @return array |
|
| 19 | - */ |
|
| 20 | - public function __recipe_metadata( $metadata, $recipe ) { |
|
| 21 | - |
|
| 22 | - $ingredients = get_the_terms( $recipe->id(), 'wprm_ingredient' ); |
|
| 23 | - $jsonlds = array_filter( array_map( array( $this, '__term_id_to_jsonld' ), $ingredients ) ); |
|
| 24 | - |
|
| 25 | - if ( empty( $jsonlds ) ) { |
|
| 26 | - return $metadata; |
|
| 27 | - } |
|
| 28 | - |
|
| 29 | - // We're embedding the full json-ld here because WL doesn't output its own markup, so it makes no sense |
|
| 30 | - // to hook to wl_after_json_ld. |
|
| 31 | - $metadata['mentions'] = isset( $metadata['mentions'] ) ? $metadata['mentions'] : array(); |
|
| 32 | - $metadata['mentions'] = array_merge( $metadata['mentions'], $jsonlds ); |
|
| 33 | - |
|
| 34 | - return $metadata; |
|
| 35 | - } |
|
| 36 | - |
|
| 37 | - /** |
|
| 38 | - * @param WP_Term $ingredient |
|
| 39 | - * |
|
| 40 | - * @return array void |
|
| 41 | - */ |
|
| 42 | - private function __term_id_to_jsonld( $ingredient ) { |
|
| 43 | - return json_decode( get_term_meta( $ingredient->term_id, '_wl_jsonld', true ), true ); |
|
| 44 | - } |
|
| 10 | + public function register_hooks() { |
|
| 11 | + add_action( 'wprm_recipe_metadata', array( $this, '__recipe_metadata' ), 10, 2 ); |
|
| 12 | + } |
|
| 13 | + |
|
| 14 | + /** |
|
| 15 | + * @param array $metadata |
|
| 16 | + * @param WPRM_Recipe $recipe |
|
| 17 | + * |
|
| 18 | + * @return array |
|
| 19 | + */ |
|
| 20 | + public function __recipe_metadata( $metadata, $recipe ) { |
|
| 21 | + |
|
| 22 | + $ingredients = get_the_terms( $recipe->id(), 'wprm_ingredient' ); |
|
| 23 | + $jsonlds = array_filter( array_map( array( $this, '__term_id_to_jsonld' ), $ingredients ) ); |
|
| 24 | + |
|
| 25 | + if ( empty( $jsonlds ) ) { |
|
| 26 | + return $metadata; |
|
| 27 | + } |
|
| 28 | + |
|
| 29 | + // We're embedding the full json-ld here because WL doesn't output its own markup, so it makes no sense |
|
| 30 | + // to hook to wl_after_json_ld. |
|
| 31 | + $metadata['mentions'] = isset( $metadata['mentions'] ) ? $metadata['mentions'] : array(); |
|
| 32 | + $metadata['mentions'] = array_merge( $metadata['mentions'], $jsonlds ); |
|
| 33 | + |
|
| 34 | + return $metadata; |
|
| 35 | + } |
|
| 36 | + |
|
| 37 | + /** |
|
| 38 | + * @param WP_Term $ingredient |
|
| 39 | + * |
|
| 40 | + * @return array void |
|
| 41 | + */ |
|
| 42 | + private function __term_id_to_jsonld( $ingredient ) { |
|
| 43 | + return json_decode( get_term_meta( $ingredient->term_id, '_wl_jsonld', true ), true ); |
|
| 44 | + } |
|
| 45 | 45 | |
| 46 | 46 | } |
@@ -6,63 +6,63 @@ |
||
| 6 | 6 | |
| 7 | 7 | class Module { |
| 8 | 8 | |
| 9 | - const RUN_EVENT = 'wl_food_kg__run'; |
|
| 9 | + const RUN_EVENT = 'wl_food_kg__run'; |
|
| 10 | 10 | |
| 11 | - /** |
|
| 12 | - * @var Api_Service_Ext |
|
| 13 | - */ |
|
| 14 | - private $api_service; |
|
| 11 | + /** |
|
| 12 | + * @var Api_Service_Ext |
|
| 13 | + */ |
|
| 14 | + private $api_service; |
|
| 15 | 15 | |
| 16 | - /** |
|
| 17 | - * @var Recipe_Lift_Strategy |
|
| 18 | - */ |
|
| 19 | - private $recipe_lift_strategies; |
|
| 16 | + /** |
|
| 17 | + * @var Recipe_Lift_Strategy |
|
| 18 | + */ |
|
| 19 | + private $recipe_lift_strategies; |
|
| 20 | 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 | - } |
|
| 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 | 29 | |
| 30 | - public function register_hooks() { |
|
| 31 | - add_action( 'wl_key_updated', array( $this, '__key_updated' ) ); |
|
| 32 | - add_action( self::RUN_EVENT, array( $this, '__run' ) ); |
|
| 33 | - add_action( 'wp_ajax_wl_food_kg__run', array( $this, '__run' ) ); |
|
| 34 | - } |
|
| 30 | + public function register_hooks() { |
|
| 31 | + add_action( 'wl_key_updated', array( $this, '__key_updated' ) ); |
|
| 32 | + add_action( self::RUN_EVENT, array( $this, '__run' ) ); |
|
| 33 | + add_action( 'wp_ajax_wl_food_kg__run', array( $this, '__run' ) ); |
|
| 34 | + } |
|
| 35 | 35 | |
| 36 | - public function __key_updated() { |
|
| 37 | - try { |
|
| 38 | - $me_response = $this->api_service->me(); |
|
| 39 | - $has_food_kg = isset( $me_response->networks ) |
|
| 40 | - && array_reduce( $me_response->networks, array( $this, '__has_food_kg' ), false ); |
|
| 41 | - $next_scheduled = wp_next_scheduled( self::RUN_EVENT ); |
|
| 36 | + public function __key_updated() { |
|
| 37 | + try { |
|
| 38 | + $me_response = $this->api_service->me(); |
|
| 39 | + $has_food_kg = isset( $me_response->networks ) |
|
| 40 | + && array_reduce( $me_response->networks, array( $this, '__has_food_kg' ), false ); |
|
| 41 | + $next_scheduled = wp_next_scheduled( self::RUN_EVENT ); |
|
| 42 | 42 | |
| 43 | - // We're connected to the Food KG, but not yet scheduled. |
|
| 44 | - if ( $has_food_kg && ! $next_scheduled ) { |
|
| 45 | - wp_schedule_event( time(), 'daily', self::RUN_EVENT ); |
|
| 46 | - } |
|
| 43 | + // We're connected to the Food KG, but not yet scheduled. |
|
| 44 | + if ( $has_food_kg && ! $next_scheduled ) { |
|
| 45 | + wp_schedule_event( time(), 'daily', self::RUN_EVENT ); |
|
| 46 | + } |
|
| 47 | 47 | |
| 48 | - // We're not connected to the Food KG, but we're scheduled. |
|
| 49 | - if ( ! $has_food_kg && $next_scheduled ) { |
|
| 50 | - wp_unschedule_event( $next_scheduled, self::RUN_EVENT ); |
|
| 51 | - } |
|
| 52 | - // phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedCatch |
|
| 53 | - } catch ( \Exception $e ) { |
|
| 54 | - // Do nothing. |
|
| 55 | - } |
|
| 56 | - } |
|
| 48 | + // We're not connected to the Food KG, but we're scheduled. |
|
| 49 | + if ( ! $has_food_kg && $next_scheduled ) { |
|
| 50 | + wp_unschedule_event( $next_scheduled, self::RUN_EVENT ); |
|
| 51 | + } |
|
| 52 | + // phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedCatch |
|
| 53 | + } catch ( \Exception $e ) { |
|
| 54 | + // Do nothing. |
|
| 55 | + } |
|
| 56 | + } |
|
| 57 | 57 | |
| 58 | - public function __run() { |
|
| 59 | - foreach ( $this->recipe_lift_strategies as $recipe_lift_strategy ) { |
|
| 60 | - $recipe_lift_strategy->run(); |
|
| 61 | - } |
|
| 62 | - } |
|
| 58 | + public function __run() { |
|
| 59 | + foreach ( $this->recipe_lift_strategies as $recipe_lift_strategy ) { |
|
| 60 | + $recipe_lift_strategy->run(); |
|
| 61 | + } |
|
| 62 | + } |
|
| 63 | 63 | |
| 64 | - private function __has_food_kg( $carry, $item ) { |
|
| 65 | - return $carry || 'https://knowledge.cafemedia.com/food/' === $item->dataset_uri; |
|
| 66 | - } |
|
| 64 | + private function __has_food_kg( $carry, $item ) { |
|
| 65 | + return $carry || 'https://knowledge.cafemedia.com/food/' === $item->dataset_uri; |
|
| 66 | + } |
|
| 67 | 67 | |
| 68 | 68 | } |