@@ -10,89 +10,89 @@ |
||
| 10 | 10 | |
| 11 | 11 | class Wordpress_Content implements Content { |
| 12 | 12 | |
| 13 | - /** |
|
| 14 | - * The actual content. |
|
| 15 | - * |
|
| 16 | - * @var WP_Post|WP_Term|WP_User $bag |
|
| 17 | - */ |
|
| 18 | - private $bag; |
|
| 19 | - |
|
| 20 | - /** |
|
| 21 | - * Creates a WordPress content instance. |
|
| 22 | - * |
|
| 23 | - * @param WP_Post|WP_Term|WP_User $bag |
|
| 24 | - */ |
|
| 25 | - public function __construct( $bag ) { |
|
| 26 | - $this->bag = $bag; |
|
| 27 | - } |
|
| 28 | - |
|
| 29 | - /** |
|
| 30 | - * The actual content. |
|
| 31 | - * |
|
| 32 | - * @return WP_Post|WP_Term|WP_User |
|
| 33 | - */ |
|
| 34 | - function get_bag() { |
|
| 35 | - return $this->bag; |
|
| 36 | - } |
|
| 37 | - |
|
| 38 | - function get_id() { |
|
| 39 | - if ( ! is_object( $this->bag ) ) { |
|
| 40 | - return null; |
|
| 41 | - } |
|
| 42 | - |
|
| 43 | - switch ( get_class( $this->bag ) ) { |
|
| 44 | - case 'WP_Post': |
|
| 45 | - case 'WP_User': |
|
| 46 | - return $this->bag->ID; |
|
| 47 | - case 'WP_Term': |
|
| 48 | - return $this->bag->term_id; |
|
| 49 | - } |
|
| 50 | - |
|
| 51 | - return null; |
|
| 52 | - } |
|
| 53 | - |
|
| 54 | - function get_object_type_enum() { |
|
| 55 | - if ( ! is_object( $this->bag ) ) { |
|
| 56 | - return null; |
|
| 57 | - } |
|
| 58 | - |
|
| 59 | - return Object_Type_Enum::from_wordpress_instance( $this->bag ); |
|
| 60 | - } |
|
| 61 | - |
|
| 62 | - function get_permalink() { |
|
| 63 | - if ( ! is_object( $this->bag ) ) { |
|
| 64 | - return null; |
|
| 65 | - } |
|
| 66 | - |
|
| 67 | - switch ( get_class( $this->bag ) ) { |
|
| 68 | - case 'WP_Post': |
|
| 69 | - return get_permalink( $this->get_bag()->ID ); |
|
| 70 | - case 'WP_User': |
|
| 71 | - return get_author_posts_url( $this->get_bag()->ID ); |
|
| 72 | - case 'WP_Term': |
|
| 73 | - return get_term_link( $this->bag->term_id ); |
|
| 74 | - } |
|
| 75 | - |
|
| 76 | - return null; |
|
| 77 | - } |
|
| 78 | - |
|
| 79 | - function get_edit_link() { |
|
| 80 | - if ( ! is_object( $this->bag ) ) { |
|
| 81 | - return null; |
|
| 82 | - } |
|
| 83 | - |
|
| 84 | - switch ( get_class( $this->bag ) ) { |
|
| 85 | - case 'WP_Post': |
|
| 86 | - // We need to return & character as &, by default context is set to display. |
|
| 87 | - // so & will be returned as & breaking header location redirects. |
|
| 88 | - // By setting context to none we prevent this issue. |
|
| 89 | - return get_edit_post_link( $this->get_bag()->ID, 'none' ); |
|
| 90 | - case 'WP_User': |
|
| 91 | - return get_edit_user_link( $this->get_bag()->ID ); |
|
| 92 | - case 'WP_Term': |
|
| 93 | - return get_edit_term_link( $this->bag->term_id ); |
|
| 94 | - } |
|
| 95 | - |
|
| 96 | - return null; |
|
| 97 | - } |
|
| 13 | + /** |
|
| 14 | + * The actual content. |
|
| 15 | + * |
|
| 16 | + * @var WP_Post|WP_Term|WP_User $bag |
|
| 17 | + */ |
|
| 18 | + private $bag; |
|
| 19 | + |
|
| 20 | + /** |
|
| 21 | + * Creates a WordPress content instance. |
|
| 22 | + * |
|
| 23 | + * @param WP_Post|WP_Term|WP_User $bag |
|
| 24 | + */ |
|
| 25 | + public function __construct( $bag ) { |
|
| 26 | + $this->bag = $bag; |
|
| 27 | + } |
|
| 28 | + |
|
| 29 | + /** |
|
| 30 | + * The actual content. |
|
| 31 | + * |
|
| 32 | + * @return WP_Post|WP_Term|WP_User |
|
| 33 | + */ |
|
| 34 | + function get_bag() { |
|
| 35 | + return $this->bag; |
|
| 36 | + } |
|
| 37 | + |
|
| 38 | + function get_id() { |
|
| 39 | + if ( ! is_object( $this->bag ) ) { |
|
| 40 | + return null; |
|
| 41 | + } |
|
| 42 | + |
|
| 43 | + switch ( get_class( $this->bag ) ) { |
|
| 44 | + case 'WP_Post': |
|
| 45 | + case 'WP_User': |
|
| 46 | + return $this->bag->ID; |
|
| 47 | + case 'WP_Term': |
|
| 48 | + return $this->bag->term_id; |
|
| 49 | + } |
|
| 50 | + |
|
| 51 | + return null; |
|
| 52 | + } |
|
| 53 | + |
|
| 54 | + function get_object_type_enum() { |
|
| 55 | + if ( ! is_object( $this->bag ) ) { |
|
| 56 | + return null; |
|
| 57 | + } |
|
| 58 | + |
|
| 59 | + return Object_Type_Enum::from_wordpress_instance( $this->bag ); |
|
| 60 | + } |
|
| 61 | + |
|
| 62 | + function get_permalink() { |
|
| 63 | + if ( ! is_object( $this->bag ) ) { |
|
| 64 | + return null; |
|
| 65 | + } |
|
| 66 | + |
|
| 67 | + switch ( get_class( $this->bag ) ) { |
|
| 68 | + case 'WP_Post': |
|
| 69 | + return get_permalink( $this->get_bag()->ID ); |
|
| 70 | + case 'WP_User': |
|
| 71 | + return get_author_posts_url( $this->get_bag()->ID ); |
|
| 72 | + case 'WP_Term': |
|
| 73 | + return get_term_link( $this->bag->term_id ); |
|
| 74 | + } |
|
| 75 | + |
|
| 76 | + return null; |
|
| 77 | + } |
|
| 78 | + |
|
| 79 | + function get_edit_link() { |
|
| 80 | + if ( ! is_object( $this->bag ) ) { |
|
| 81 | + return null; |
|
| 82 | + } |
|
| 83 | + |
|
| 84 | + switch ( get_class( $this->bag ) ) { |
|
| 85 | + case 'WP_Post': |
|
| 86 | + // We need to return & character as &, by default context is set to display. |
|
| 87 | + // so & will be returned as & breaking header location redirects. |
|
| 88 | + // By setting context to none we prevent this issue. |
|
| 89 | + return get_edit_post_link( $this->get_bag()->ID, 'none' ); |
|
| 90 | + case 'WP_User': |
|
| 91 | + return get_edit_user_link( $this->get_bag()->ID ); |
|
| 92 | + case 'WP_Term': |
|
| 93 | + return get_edit_term_link( $this->bag->term_id ); |
|
| 94 | + } |
|
| 95 | + |
|
| 96 | + return null; |
|
| 97 | + } |
|
| 98 | 98 | } |
| 99 | 99 | \ No newline at end of file |
@@ -22,7 +22,7 @@ discard block |
||
| 22 | 22 | * |
| 23 | 23 | * @param WP_Post|WP_Term|WP_User $bag |
| 24 | 24 | */ |
| 25 | - public function __construct( $bag ) { |
|
| 25 | + public function __construct($bag) { |
|
| 26 | 26 | $this->bag = $bag; |
| 27 | 27 | } |
| 28 | 28 | |
@@ -36,11 +36,11 @@ discard block |
||
| 36 | 36 | } |
| 37 | 37 | |
| 38 | 38 | function get_id() { |
| 39 | - if ( ! is_object( $this->bag ) ) { |
|
| 39 | + if ( ! is_object($this->bag)) { |
|
| 40 | 40 | return null; |
| 41 | 41 | } |
| 42 | 42 | |
| 43 | - switch ( get_class( $this->bag ) ) { |
|
| 43 | + switch (get_class($this->bag)) { |
|
| 44 | 44 | case 'WP_Post': |
| 45 | 45 | case 'WP_User': |
| 46 | 46 | return $this->bag->ID; |
@@ -52,45 +52,45 @@ discard block |
||
| 52 | 52 | } |
| 53 | 53 | |
| 54 | 54 | function get_object_type_enum() { |
| 55 | - if ( ! is_object( $this->bag ) ) { |
|
| 55 | + if ( ! is_object($this->bag)) { |
|
| 56 | 56 | return null; |
| 57 | 57 | } |
| 58 | 58 | |
| 59 | - return Object_Type_Enum::from_wordpress_instance( $this->bag ); |
|
| 59 | + return Object_Type_Enum::from_wordpress_instance($this->bag); |
|
| 60 | 60 | } |
| 61 | 61 | |
| 62 | 62 | function get_permalink() { |
| 63 | - if ( ! is_object( $this->bag ) ) { |
|
| 63 | + if ( ! is_object($this->bag)) { |
|
| 64 | 64 | return null; |
| 65 | 65 | } |
| 66 | 66 | |
| 67 | - switch ( get_class( $this->bag ) ) { |
|
| 67 | + switch (get_class($this->bag)) { |
|
| 68 | 68 | case 'WP_Post': |
| 69 | - return get_permalink( $this->get_bag()->ID ); |
|
| 69 | + return get_permalink($this->get_bag()->ID); |
|
| 70 | 70 | case 'WP_User': |
| 71 | - return get_author_posts_url( $this->get_bag()->ID ); |
|
| 71 | + return get_author_posts_url($this->get_bag()->ID); |
|
| 72 | 72 | case 'WP_Term': |
| 73 | - return get_term_link( $this->bag->term_id ); |
|
| 73 | + return get_term_link($this->bag->term_id); |
|
| 74 | 74 | } |
| 75 | 75 | |
| 76 | 76 | return null; |
| 77 | 77 | } |
| 78 | 78 | |
| 79 | 79 | function get_edit_link() { |
| 80 | - if ( ! is_object( $this->bag ) ) { |
|
| 80 | + if ( ! is_object($this->bag)) { |
|
| 81 | 81 | return null; |
| 82 | 82 | } |
| 83 | 83 | |
| 84 | - switch ( get_class( $this->bag ) ) { |
|
| 84 | + switch (get_class($this->bag)) { |
|
| 85 | 85 | case 'WP_Post': |
| 86 | 86 | // We need to return & character as &, by default context is set to display. |
| 87 | 87 | // so & will be returned as & breaking header location redirects. |
| 88 | 88 | // By setting context to none we prevent this issue. |
| 89 | - return get_edit_post_link( $this->get_bag()->ID, 'none' ); |
|
| 89 | + return get_edit_post_link($this->get_bag()->ID, 'none'); |
|
| 90 | 90 | case 'WP_User': |
| 91 | - return get_edit_user_link( $this->get_bag()->ID ); |
|
| 91 | + return get_edit_user_link($this->get_bag()->ID); |
|
| 92 | 92 | case 'WP_Term': |
| 93 | - return get_edit_term_link( $this->bag->term_id ); |
|
| 93 | + return get_edit_term_link($this->bag->term_id); |
|
| 94 | 94 | } |
| 95 | 95 | |
| 96 | 96 | return null; |
@@ -23,261 +23,261 @@ |
||
| 23 | 23 | */ |
| 24 | 24 | class Wordlift_Content_Filter_Service { |
| 25 | 25 | |
| 26 | - /** |
|
| 27 | - * The pattern to find entities in text. |
|
| 28 | - * |
|
| 29 | - * @since 3.8.0 |
|
| 30 | - */ |
|
| 31 | - const PATTERN = '/<(\\w+)[^<]*class="([^"]*)"\\sitemid=\"([^"]+)\"[^>]*>([^<]*)<\\/\\1>/i'; |
|
| 32 | - |
|
| 33 | - /** |
|
| 34 | - * A {@link Wordlift_Entity_Service} instance. |
|
| 35 | - * |
|
| 36 | - * @since 3.8.0 |
|
| 37 | - * @access private |
|
| 38 | - * @var \Wordlift_Entity_Service $entity_service A {@link Wordlift_Entity_Service} instance. |
|
| 39 | - */ |
|
| 40 | - private $entity_service; |
|
| 41 | - |
|
| 42 | - /** |
|
| 43 | - * The `link by default` setting. |
|
| 44 | - * |
|
| 45 | - * @since 3.13.0 |
|
| 46 | - * @access private |
|
| 47 | - * @var bool True if link by default is enabled otherwise false. |
|
| 48 | - */ |
|
| 49 | - private $is_link_by_default; |
|
| 50 | - |
|
| 51 | - private $linked_entity_uris = array(); |
|
| 52 | - |
|
| 53 | - /** |
|
| 54 | - * The {@link Wordlift_Entity_Uri_Service} instance. |
|
| 55 | - * |
|
| 56 | - * @since 3.16.3 |
|
| 57 | - * @access private |
|
| 58 | - * @var \Wordlift_Entity_Uri_Service $entity_uri_service The {@link Wordlift_Entity_Uri_Service} instance. |
|
| 59 | - */ |
|
| 60 | - private $entity_uri_service; |
|
| 61 | - |
|
| 62 | - /** |
|
| 63 | - * A {@link Wordlift_Log_Service} instance. |
|
| 64 | - * |
|
| 65 | - * @since 3.16.0 |
|
| 66 | - * |
|
| 67 | - * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
| 68 | - */ |
|
| 69 | - private $log; |
|
| 70 | - /** |
|
| 71 | - * @var Object_Link_Provider |
|
| 72 | - */ |
|
| 73 | - private $object_link_provider; |
|
| 74 | - |
|
| 75 | - /** |
|
| 76 | - * Create a {@link Wordlift_Content_Filter_Service} instance. |
|
| 77 | - * |
|
| 78 | - * @param \Wordlift_Entity_Service $entity_service The {@link Wordlift_Entity_Service} instance. |
|
| 79 | - * @param \Wordlift_Entity_Uri_Service $entity_uri_service The {@link Wordlift_Entity_Uri_Service} instance. |
|
| 80 | - * |
|
| 81 | - * @since 3.8.0 |
|
| 82 | - * |
|
| 83 | - */ |
|
| 84 | - protected function __construct( $entity_service, $entity_uri_service ) { |
|
| 85 | - |
|
| 86 | - $this->log = Wordlift_Log_Service::get_logger( get_class() ); |
|
| 87 | - |
|
| 88 | - $this->entity_service = $entity_service; |
|
| 89 | - $this->entity_uri_service = $entity_uri_service; |
|
| 90 | - $this->object_link_provider = Object_Link_Provider::get_instance(); |
|
| 91 | - |
|
| 92 | - } |
|
| 93 | - |
|
| 94 | - private static $instance = null; |
|
| 95 | - |
|
| 96 | - /** |
|
| 97 | - * Get the {@link Wordlift_Content_Filter_Service} singleton instance. |
|
| 98 | - * |
|
| 99 | - * @return \Wordlift_Content_Filter_Service The {@link Wordlift_Content_Filter_Service} singleton instance. |
|
| 100 | - * @since 3.14.2 |
|
| 101 | - */ |
|
| 102 | - public static function get_instance() { |
|
| 103 | - |
|
| 104 | - if ( ! isset( self::$instance ) ) { |
|
| 105 | - self::$instance = new self( Wordlift_Entity_Service::get_instance(), Wordlift_Entity_Uri_Service::get_instance() ); |
|
| 106 | - } |
|
| 107 | - |
|
| 108 | - return self::$instance; |
|
| 109 | - } |
|
| 110 | - |
|
| 111 | - /** |
|
| 112 | - * Mangle the content by adding links to the entity pages. This function is |
|
| 113 | - * hooked to the 'the_content' WP's filter. |
|
| 114 | - * |
|
| 115 | - * @param string $content The content being filtered. |
|
| 116 | - * |
|
| 117 | - * @return string The filtered content. |
|
| 118 | - * @since 3.8.0 |
|
| 119 | - * |
|
| 120 | - */ |
|
| 121 | - public function the_content( $content ) { |
|
| 122 | - $this->log->trace( "Filtering content [ " . ( is_singular() ? 'yes' : 'no' ) . " ]..." ); |
|
| 123 | - |
|
| 124 | - // Links should be added only on the front end and not for RSS. |
|
| 125 | - if ( is_feed() || is_admin() || is_search() ) { |
|
| 126 | - return $content; |
|
| 127 | - } |
|
| 128 | - |
|
| 129 | - // Preload the `link by default` setting. |
|
| 130 | - $this->is_link_by_default = Wordlift_Configuration_Service::get_instance()->is_link_by_default(); |
|
| 131 | - |
|
| 132 | - // Reset the array of of entity post ids linked from the post content. |
|
| 133 | - // This is used to avoid linking more the once the same post. |
|
| 134 | - $this->linked_entity_uris = array(); |
|
| 135 | - |
|
| 136 | - // Preload URIs. |
|
| 137 | - $matches = array(); |
|
| 138 | - preg_match_all( self::PATTERN, $content, $matches ); |
|
| 139 | - |
|
| 140 | - // Bail out if there are no URIs. |
|
| 141 | - if ( empty( $matches[3] ) ) { |
|
| 142 | - return $content; |
|
| 143 | - } |
|
| 144 | - |
|
| 145 | - // Replace each match of the entity tag with the entity link. If an error |
|
| 146 | - // occurs fail silently returning the original content. |
|
| 147 | - return preg_replace_callback( self::PATTERN, array( |
|
| 148 | - $this, |
|
| 149 | - 'link', |
|
| 150 | - ), $content ) ?: $content; |
|
| 151 | - } |
|
| 152 | - |
|
| 153 | - /** |
|
| 154 | - * Get the entity match and replace it with a page link. |
|
| 155 | - * |
|
| 156 | - * @param array $matches An array of matches. |
|
| 157 | - * |
|
| 158 | - * @return string The replaced text with the link to the entity page. |
|
| 159 | - * @since 3.8.0 |
|
| 160 | - * |
|
| 161 | - */ |
|
| 162 | - private function link( $matches ) { |
|
| 163 | - |
|
| 164 | - // Get the entity itemid URI and label. |
|
| 165 | - $css_class = $matches[2]; |
|
| 166 | - $uri = $matches[3]; |
|
| 167 | - $label = $matches[4]; |
|
| 168 | - |
|
| 169 | - |
|
| 170 | - /** |
|
| 171 | - * If the entity is already linked, dont send query to the db. |
|
| 172 | - */ |
|
| 173 | - if ( $this->is_already_linked( $uri ) ) { |
|
| 174 | - return $label; |
|
| 175 | - } |
|
| 176 | - |
|
| 177 | - |
|
| 178 | - $link = - 1 < strpos( $css_class, 'wl-link' ); |
|
| 179 | - |
|
| 180 | - // If the entity should not be linked and link by default is also disabled, |
|
| 181 | - // then don't lookup the entity on the table. |
|
| 182 | - if ( ! $this->is_link_by_default && ! $link ) { |
|
| 183 | - return $label; |
|
| 184 | - } |
|
| 185 | - |
|
| 186 | - $content_service = Wordpress_Content_Service::get_instance(); |
|
| 187 | - $content = $content_service->get_by_entity_id_or_same_as( $uri ); |
|
| 188 | - |
|
| 189 | - // If no content is found, return the label, that is _remove the annotation_. |
|
| 190 | - if ( ! is_object( $content ) ) { |
|
| 191 | - return $label; |
|
| 192 | - } |
|
| 193 | - |
|
| 194 | - $object_id = $content->get_id(); |
|
| 195 | - $object_type = $content->get_object_type_enum(); |
|
| 196 | - |
|
| 197 | - |
|
| 198 | - $no_link = - 1 < strpos( $css_class, 'wl-no-link' ); |
|
| 199 | - |
|
| 200 | - // Don't link if links are disabled and the entity is not link or the |
|
| 201 | - // entity is do not link. |
|
| 202 | - $dont_link = ( ! $this->is_link_by_default && ! $link ) || $no_link; |
|
| 203 | - |
|
| 204 | - // Return the label if it's don't link. |
|
| 205 | - if ( $dont_link ) { |
|
| 206 | - return $label; |
|
| 207 | - } |
|
| 208 | - |
|
| 209 | - /** |
|
| 210 | - * @since 3.32.0 |
|
| 211 | - * Object_ids are prefixed with object_type to prevent conflicts. |
|
| 212 | - */ |
|
| 213 | - $this->linked_entity_uris[] = $uri; |
|
| 214 | - |
|
| 215 | - // Get the link. |
|
| 216 | - $href = Wordlift_Post_Adapter::get_production_permalink( $object_id, $object_type ); |
|
| 217 | - |
|
| 218 | - // Bail out if the `$href` has been reset. |
|
| 219 | - if ( empty( $href ) ) { |
|
| 220 | - return $label; |
|
| 221 | - } |
|
| 222 | - |
|
| 223 | - return Link_Builder::create( $uri, $object_id ) |
|
| 224 | - ->label( $label ) |
|
| 225 | - ->href( $href ) |
|
| 226 | - ->generate_link(); |
|
| 227 | - } |
|
| 228 | - |
|
| 229 | - |
|
| 230 | - /** |
|
| 231 | - * Get a string to be used as a title attribute in links to a post |
|
| 232 | - * |
|
| 233 | - * @param int $post_id The post id of the post being linked. |
|
| 234 | - * @param string $ignore_label A label to ignore. |
|
| 235 | - * |
|
| 236 | - * @return string The title to be used in the link. An empty string when |
|
| 237 | - * there is no alternative that is not the $ignore_label. |
|
| 238 | - * @deprecated 3.32.0 Use object link provider to get the link title for getting link |
|
| 239 | - * title for different types. |
|
| 240 | - * @since 3.15.0 |
|
| 241 | - * |
|
| 242 | - * As of 3.32.0 this method is not used anywhere in the core, this should be removed |
|
| 243 | - * from tests and companion plugins. |
|
| 244 | - * |
|
| 245 | - */ |
|
| 246 | - function get_link_title( $post_id, $ignore_label, $object_type = Object_Type_Enum::POST ) { |
|
| 247 | - return $this->object_link_provider->get_link_title( $post_id, $ignore_label, $object_type ); |
|
| 248 | - } |
|
| 249 | - |
|
| 250 | - /** |
|
| 251 | - * Get the entity URIs (configured in the `itemid` attribute) contained in |
|
| 252 | - * the provided content. |
|
| 253 | - * |
|
| 254 | - * @param string $content The content. |
|
| 255 | - * |
|
| 256 | - * @return array An array of URIs. |
|
| 257 | - * @since 3.14.2 |
|
| 258 | - * |
|
| 259 | - */ |
|
| 260 | - public function get_entity_uris( $content ) { |
|
| 261 | - |
|
| 262 | - $matches = array(); |
|
| 263 | - preg_match_all( Wordlift_Content_Filter_Service::PATTERN, $content, $matches ); |
|
| 264 | - |
|
| 265 | - // We need to use `array_values` here in order to avoid further `json_encode` |
|
| 266 | - // to turn it into an object (since if the 3rd match isn't found the index |
|
| 267 | - // is not sequential. |
|
| 268 | - // |
|
| 269 | - // See https://github.com/insideout10/wordlift-plugin/issues/646. |
|
| 270 | - return array_values( array_unique( $matches[3] ) ); |
|
| 271 | - } |
|
| 272 | - |
|
| 273 | - |
|
| 274 | - /** |
|
| 275 | - * @param $entity_uri |
|
| 276 | - * |
|
| 277 | - * @return bool |
|
| 278 | - */ |
|
| 279 | - private function is_already_linked( $entity_uri ) { |
|
| 280 | - return in_array( $entity_uri, $this->linked_entity_uris ); |
|
| 281 | - } |
|
| 26 | + /** |
|
| 27 | + * The pattern to find entities in text. |
|
| 28 | + * |
|
| 29 | + * @since 3.8.0 |
|
| 30 | + */ |
|
| 31 | + const PATTERN = '/<(\\w+)[^<]*class="([^"]*)"\\sitemid=\"([^"]+)\"[^>]*>([^<]*)<\\/\\1>/i'; |
|
| 32 | + |
|
| 33 | + /** |
|
| 34 | + * A {@link Wordlift_Entity_Service} instance. |
|
| 35 | + * |
|
| 36 | + * @since 3.8.0 |
|
| 37 | + * @access private |
|
| 38 | + * @var \Wordlift_Entity_Service $entity_service A {@link Wordlift_Entity_Service} instance. |
|
| 39 | + */ |
|
| 40 | + private $entity_service; |
|
| 41 | + |
|
| 42 | + /** |
|
| 43 | + * The `link by default` setting. |
|
| 44 | + * |
|
| 45 | + * @since 3.13.0 |
|
| 46 | + * @access private |
|
| 47 | + * @var bool True if link by default is enabled otherwise false. |
|
| 48 | + */ |
|
| 49 | + private $is_link_by_default; |
|
| 50 | + |
|
| 51 | + private $linked_entity_uris = array(); |
|
| 52 | + |
|
| 53 | + /** |
|
| 54 | + * The {@link Wordlift_Entity_Uri_Service} instance. |
|
| 55 | + * |
|
| 56 | + * @since 3.16.3 |
|
| 57 | + * @access private |
|
| 58 | + * @var \Wordlift_Entity_Uri_Service $entity_uri_service The {@link Wordlift_Entity_Uri_Service} instance. |
|
| 59 | + */ |
|
| 60 | + private $entity_uri_service; |
|
| 61 | + |
|
| 62 | + /** |
|
| 63 | + * A {@link Wordlift_Log_Service} instance. |
|
| 64 | + * |
|
| 65 | + * @since 3.16.0 |
|
| 66 | + * |
|
| 67 | + * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
| 68 | + */ |
|
| 69 | + private $log; |
|
| 70 | + /** |
|
| 71 | + * @var Object_Link_Provider |
|
| 72 | + */ |
|
| 73 | + private $object_link_provider; |
|
| 74 | + |
|
| 75 | + /** |
|
| 76 | + * Create a {@link Wordlift_Content_Filter_Service} instance. |
|
| 77 | + * |
|
| 78 | + * @param \Wordlift_Entity_Service $entity_service The {@link Wordlift_Entity_Service} instance. |
|
| 79 | + * @param \Wordlift_Entity_Uri_Service $entity_uri_service The {@link Wordlift_Entity_Uri_Service} instance. |
|
| 80 | + * |
|
| 81 | + * @since 3.8.0 |
|
| 82 | + * |
|
| 83 | + */ |
|
| 84 | + protected function __construct( $entity_service, $entity_uri_service ) { |
|
| 85 | + |
|
| 86 | + $this->log = Wordlift_Log_Service::get_logger( get_class() ); |
|
| 87 | + |
|
| 88 | + $this->entity_service = $entity_service; |
|
| 89 | + $this->entity_uri_service = $entity_uri_service; |
|
| 90 | + $this->object_link_provider = Object_Link_Provider::get_instance(); |
|
| 91 | + |
|
| 92 | + } |
|
| 93 | + |
|
| 94 | + private static $instance = null; |
|
| 95 | + |
|
| 96 | + /** |
|
| 97 | + * Get the {@link Wordlift_Content_Filter_Service} singleton instance. |
|
| 98 | + * |
|
| 99 | + * @return \Wordlift_Content_Filter_Service The {@link Wordlift_Content_Filter_Service} singleton instance. |
|
| 100 | + * @since 3.14.2 |
|
| 101 | + */ |
|
| 102 | + public static function get_instance() { |
|
| 103 | + |
|
| 104 | + if ( ! isset( self::$instance ) ) { |
|
| 105 | + self::$instance = new self( Wordlift_Entity_Service::get_instance(), Wordlift_Entity_Uri_Service::get_instance() ); |
|
| 106 | + } |
|
| 107 | + |
|
| 108 | + return self::$instance; |
|
| 109 | + } |
|
| 110 | + |
|
| 111 | + /** |
|
| 112 | + * Mangle the content by adding links to the entity pages. This function is |
|
| 113 | + * hooked to the 'the_content' WP's filter. |
|
| 114 | + * |
|
| 115 | + * @param string $content The content being filtered. |
|
| 116 | + * |
|
| 117 | + * @return string The filtered content. |
|
| 118 | + * @since 3.8.0 |
|
| 119 | + * |
|
| 120 | + */ |
|
| 121 | + public function the_content( $content ) { |
|
| 122 | + $this->log->trace( "Filtering content [ " . ( is_singular() ? 'yes' : 'no' ) . " ]..." ); |
|
| 123 | + |
|
| 124 | + // Links should be added only on the front end and not for RSS. |
|
| 125 | + if ( is_feed() || is_admin() || is_search() ) { |
|
| 126 | + return $content; |
|
| 127 | + } |
|
| 128 | + |
|
| 129 | + // Preload the `link by default` setting. |
|
| 130 | + $this->is_link_by_default = Wordlift_Configuration_Service::get_instance()->is_link_by_default(); |
|
| 131 | + |
|
| 132 | + // Reset the array of of entity post ids linked from the post content. |
|
| 133 | + // This is used to avoid linking more the once the same post. |
|
| 134 | + $this->linked_entity_uris = array(); |
|
| 135 | + |
|
| 136 | + // Preload URIs. |
|
| 137 | + $matches = array(); |
|
| 138 | + preg_match_all( self::PATTERN, $content, $matches ); |
|
| 139 | + |
|
| 140 | + // Bail out if there are no URIs. |
|
| 141 | + if ( empty( $matches[3] ) ) { |
|
| 142 | + return $content; |
|
| 143 | + } |
|
| 144 | + |
|
| 145 | + // Replace each match of the entity tag with the entity link. If an error |
|
| 146 | + // occurs fail silently returning the original content. |
|
| 147 | + return preg_replace_callback( self::PATTERN, array( |
|
| 148 | + $this, |
|
| 149 | + 'link', |
|
| 150 | + ), $content ) ?: $content; |
|
| 151 | + } |
|
| 152 | + |
|
| 153 | + /** |
|
| 154 | + * Get the entity match and replace it with a page link. |
|
| 155 | + * |
|
| 156 | + * @param array $matches An array of matches. |
|
| 157 | + * |
|
| 158 | + * @return string The replaced text with the link to the entity page. |
|
| 159 | + * @since 3.8.0 |
|
| 160 | + * |
|
| 161 | + */ |
|
| 162 | + private function link( $matches ) { |
|
| 163 | + |
|
| 164 | + // Get the entity itemid URI and label. |
|
| 165 | + $css_class = $matches[2]; |
|
| 166 | + $uri = $matches[3]; |
|
| 167 | + $label = $matches[4]; |
|
| 168 | + |
|
| 169 | + |
|
| 170 | + /** |
|
| 171 | + * If the entity is already linked, dont send query to the db. |
|
| 172 | + */ |
|
| 173 | + if ( $this->is_already_linked( $uri ) ) { |
|
| 174 | + return $label; |
|
| 175 | + } |
|
| 176 | + |
|
| 177 | + |
|
| 178 | + $link = - 1 < strpos( $css_class, 'wl-link' ); |
|
| 179 | + |
|
| 180 | + // If the entity should not be linked and link by default is also disabled, |
|
| 181 | + // then don't lookup the entity on the table. |
|
| 182 | + if ( ! $this->is_link_by_default && ! $link ) { |
|
| 183 | + return $label; |
|
| 184 | + } |
|
| 185 | + |
|
| 186 | + $content_service = Wordpress_Content_Service::get_instance(); |
|
| 187 | + $content = $content_service->get_by_entity_id_or_same_as( $uri ); |
|
| 188 | + |
|
| 189 | + // If no content is found, return the label, that is _remove the annotation_. |
|
| 190 | + if ( ! is_object( $content ) ) { |
|
| 191 | + return $label; |
|
| 192 | + } |
|
| 193 | + |
|
| 194 | + $object_id = $content->get_id(); |
|
| 195 | + $object_type = $content->get_object_type_enum(); |
|
| 196 | + |
|
| 197 | + |
|
| 198 | + $no_link = - 1 < strpos( $css_class, 'wl-no-link' ); |
|
| 199 | + |
|
| 200 | + // Don't link if links are disabled and the entity is not link or the |
|
| 201 | + // entity is do not link. |
|
| 202 | + $dont_link = ( ! $this->is_link_by_default && ! $link ) || $no_link; |
|
| 203 | + |
|
| 204 | + // Return the label if it's don't link. |
|
| 205 | + if ( $dont_link ) { |
|
| 206 | + return $label; |
|
| 207 | + } |
|
| 208 | + |
|
| 209 | + /** |
|
| 210 | + * @since 3.32.0 |
|
| 211 | + * Object_ids are prefixed with object_type to prevent conflicts. |
|
| 212 | + */ |
|
| 213 | + $this->linked_entity_uris[] = $uri; |
|
| 214 | + |
|
| 215 | + // Get the link. |
|
| 216 | + $href = Wordlift_Post_Adapter::get_production_permalink( $object_id, $object_type ); |
|
| 217 | + |
|
| 218 | + // Bail out if the `$href` has been reset. |
|
| 219 | + if ( empty( $href ) ) { |
|
| 220 | + return $label; |
|
| 221 | + } |
|
| 222 | + |
|
| 223 | + return Link_Builder::create( $uri, $object_id ) |
|
| 224 | + ->label( $label ) |
|
| 225 | + ->href( $href ) |
|
| 226 | + ->generate_link(); |
|
| 227 | + } |
|
| 228 | + |
|
| 229 | + |
|
| 230 | + /** |
|
| 231 | + * Get a string to be used as a title attribute in links to a post |
|
| 232 | + * |
|
| 233 | + * @param int $post_id The post id of the post being linked. |
|
| 234 | + * @param string $ignore_label A label to ignore. |
|
| 235 | + * |
|
| 236 | + * @return string The title to be used in the link. An empty string when |
|
| 237 | + * there is no alternative that is not the $ignore_label. |
|
| 238 | + * @deprecated 3.32.0 Use object link provider to get the link title for getting link |
|
| 239 | + * title for different types. |
|
| 240 | + * @since 3.15.0 |
|
| 241 | + * |
|
| 242 | + * As of 3.32.0 this method is not used anywhere in the core, this should be removed |
|
| 243 | + * from tests and companion plugins. |
|
| 244 | + * |
|
| 245 | + */ |
|
| 246 | + function get_link_title( $post_id, $ignore_label, $object_type = Object_Type_Enum::POST ) { |
|
| 247 | + return $this->object_link_provider->get_link_title( $post_id, $ignore_label, $object_type ); |
|
| 248 | + } |
|
| 249 | + |
|
| 250 | + /** |
|
| 251 | + * Get the entity URIs (configured in the `itemid` attribute) contained in |
|
| 252 | + * the provided content. |
|
| 253 | + * |
|
| 254 | + * @param string $content The content. |
|
| 255 | + * |
|
| 256 | + * @return array An array of URIs. |
|
| 257 | + * @since 3.14.2 |
|
| 258 | + * |
|
| 259 | + */ |
|
| 260 | + public function get_entity_uris( $content ) { |
|
| 261 | + |
|
| 262 | + $matches = array(); |
|
| 263 | + preg_match_all( Wordlift_Content_Filter_Service::PATTERN, $content, $matches ); |
|
| 264 | + |
|
| 265 | + // We need to use `array_values` here in order to avoid further `json_encode` |
|
| 266 | + // to turn it into an object (since if the 3rd match isn't found the index |
|
| 267 | + // is not sequential. |
|
| 268 | + // |
|
| 269 | + // See https://github.com/insideout10/wordlift-plugin/issues/646. |
|
| 270 | + return array_values( array_unique( $matches[3] ) ); |
|
| 271 | + } |
|
| 272 | + |
|
| 273 | + |
|
| 274 | + /** |
|
| 275 | + * @param $entity_uri |
|
| 276 | + * |
|
| 277 | + * @return bool |
|
| 278 | + */ |
|
| 279 | + private function is_already_linked( $entity_uri ) { |
|
| 280 | + return in_array( $entity_uri, $this->linked_entity_uris ); |
|
| 281 | + } |
|
| 282 | 282 | |
| 283 | 283 | } |
@@ -81,9 +81,9 @@ discard block |
||
| 81 | 81 | * @since 3.8.0 |
| 82 | 82 | * |
| 83 | 83 | */ |
| 84 | - protected function __construct( $entity_service, $entity_uri_service ) { |
|
| 84 | + protected function __construct($entity_service, $entity_uri_service) { |
|
| 85 | 85 | |
| 86 | - $this->log = Wordlift_Log_Service::get_logger( get_class() ); |
|
| 86 | + $this->log = Wordlift_Log_Service::get_logger(get_class()); |
|
| 87 | 87 | |
| 88 | 88 | $this->entity_service = $entity_service; |
| 89 | 89 | $this->entity_uri_service = $entity_uri_service; |
@@ -101,8 +101,8 @@ discard block |
||
| 101 | 101 | */ |
| 102 | 102 | public static function get_instance() { |
| 103 | 103 | |
| 104 | - if ( ! isset( self::$instance ) ) { |
|
| 105 | - self::$instance = new self( Wordlift_Entity_Service::get_instance(), Wordlift_Entity_Uri_Service::get_instance() ); |
|
| 104 | + if ( ! isset(self::$instance)) { |
|
| 105 | + self::$instance = new self(Wordlift_Entity_Service::get_instance(), Wordlift_Entity_Uri_Service::get_instance()); |
|
| 106 | 106 | } |
| 107 | 107 | |
| 108 | 108 | return self::$instance; |
@@ -118,11 +118,11 @@ discard block |
||
| 118 | 118 | * @since 3.8.0 |
| 119 | 119 | * |
| 120 | 120 | */ |
| 121 | - public function the_content( $content ) { |
|
| 122 | - $this->log->trace( "Filtering content [ " . ( is_singular() ? 'yes' : 'no' ) . " ]..." ); |
|
| 121 | + public function the_content($content) { |
|
| 122 | + $this->log->trace("Filtering content [ ".(is_singular() ? 'yes' : 'no')." ]..."); |
|
| 123 | 123 | |
| 124 | 124 | // Links should be added only on the front end and not for RSS. |
| 125 | - if ( is_feed() || is_admin() || is_search() ) { |
|
| 125 | + if (is_feed() || is_admin() || is_search()) { |
|
| 126 | 126 | return $content; |
| 127 | 127 | } |
| 128 | 128 | |
@@ -135,19 +135,19 @@ discard block |
||
| 135 | 135 | |
| 136 | 136 | // Preload URIs. |
| 137 | 137 | $matches = array(); |
| 138 | - preg_match_all( self::PATTERN, $content, $matches ); |
|
| 138 | + preg_match_all(self::PATTERN, $content, $matches); |
|
| 139 | 139 | |
| 140 | 140 | // Bail out if there are no URIs. |
| 141 | - if ( empty( $matches[3] ) ) { |
|
| 141 | + if (empty($matches[3])) { |
|
| 142 | 142 | return $content; |
| 143 | 143 | } |
| 144 | 144 | |
| 145 | 145 | // Replace each match of the entity tag with the entity link. If an error |
| 146 | 146 | // occurs fail silently returning the original content. |
| 147 | - return preg_replace_callback( self::PATTERN, array( |
|
| 147 | + return preg_replace_callback(self::PATTERN, array( |
|
| 148 | 148 | $this, |
| 149 | 149 | 'link', |
| 150 | - ), $content ) ?: $content; |
|
| 150 | + ), $content) ?: $content; |
|
| 151 | 151 | } |
| 152 | 152 | |
| 153 | 153 | /** |
@@ -159,7 +159,7 @@ discard block |
||
| 159 | 159 | * @since 3.8.0 |
| 160 | 160 | * |
| 161 | 161 | */ |
| 162 | - private function link( $matches ) { |
|
| 162 | + private function link($matches) { |
|
| 163 | 163 | |
| 164 | 164 | // Get the entity itemid URI and label. |
| 165 | 165 | $css_class = $matches[2]; |
@@ -170,24 +170,24 @@ discard block |
||
| 170 | 170 | /** |
| 171 | 171 | * If the entity is already linked, dont send query to the db. |
| 172 | 172 | */ |
| 173 | - if ( $this->is_already_linked( $uri ) ) { |
|
| 173 | + if ($this->is_already_linked($uri)) { |
|
| 174 | 174 | return $label; |
| 175 | 175 | } |
| 176 | 176 | |
| 177 | 177 | |
| 178 | - $link = - 1 < strpos( $css_class, 'wl-link' ); |
|
| 178 | + $link = - 1 < strpos($css_class, 'wl-link'); |
|
| 179 | 179 | |
| 180 | 180 | // If the entity should not be linked and link by default is also disabled, |
| 181 | 181 | // then don't lookup the entity on the table. |
| 182 | - if ( ! $this->is_link_by_default && ! $link ) { |
|
| 182 | + if ( ! $this->is_link_by_default && ! $link) { |
|
| 183 | 183 | return $label; |
| 184 | 184 | } |
| 185 | 185 | |
| 186 | 186 | $content_service = Wordpress_Content_Service::get_instance(); |
| 187 | - $content = $content_service->get_by_entity_id_or_same_as( $uri ); |
|
| 187 | + $content = $content_service->get_by_entity_id_or_same_as($uri); |
|
| 188 | 188 | |
| 189 | 189 | // If no content is found, return the label, that is _remove the annotation_. |
| 190 | - if ( ! is_object( $content ) ) { |
|
| 190 | + if ( ! is_object($content)) { |
|
| 191 | 191 | return $label; |
| 192 | 192 | } |
| 193 | 193 | |
@@ -195,14 +195,14 @@ discard block |
||
| 195 | 195 | $object_type = $content->get_object_type_enum(); |
| 196 | 196 | |
| 197 | 197 | |
| 198 | - $no_link = - 1 < strpos( $css_class, 'wl-no-link' ); |
|
| 198 | + $no_link = - 1 < strpos($css_class, 'wl-no-link'); |
|
| 199 | 199 | |
| 200 | 200 | // Don't link if links are disabled and the entity is not link or the |
| 201 | 201 | // entity is do not link. |
| 202 | - $dont_link = ( ! $this->is_link_by_default && ! $link ) || $no_link; |
|
| 202 | + $dont_link = ( ! $this->is_link_by_default && ! $link) || $no_link; |
|
| 203 | 203 | |
| 204 | 204 | // Return the label if it's don't link. |
| 205 | - if ( $dont_link ) { |
|
| 205 | + if ($dont_link) { |
|
| 206 | 206 | return $label; |
| 207 | 207 | } |
| 208 | 208 | |
@@ -213,16 +213,16 @@ discard block |
||
| 213 | 213 | $this->linked_entity_uris[] = $uri; |
| 214 | 214 | |
| 215 | 215 | // Get the link. |
| 216 | - $href = Wordlift_Post_Adapter::get_production_permalink( $object_id, $object_type ); |
|
| 216 | + $href = Wordlift_Post_Adapter::get_production_permalink($object_id, $object_type); |
|
| 217 | 217 | |
| 218 | 218 | // Bail out if the `$href` has been reset. |
| 219 | - if ( empty( $href ) ) { |
|
| 219 | + if (empty($href)) { |
|
| 220 | 220 | return $label; |
| 221 | 221 | } |
| 222 | 222 | |
| 223 | - return Link_Builder::create( $uri, $object_id ) |
|
| 224 | - ->label( $label ) |
|
| 225 | - ->href( $href ) |
|
| 223 | + return Link_Builder::create($uri, $object_id) |
|
| 224 | + ->label($label) |
|
| 225 | + ->href($href) |
|
| 226 | 226 | ->generate_link(); |
| 227 | 227 | } |
| 228 | 228 | |
@@ -243,8 +243,8 @@ discard block |
||
| 243 | 243 | * from tests and companion plugins. |
| 244 | 244 | * |
| 245 | 245 | */ |
| 246 | - function get_link_title( $post_id, $ignore_label, $object_type = Object_Type_Enum::POST ) { |
|
| 247 | - return $this->object_link_provider->get_link_title( $post_id, $ignore_label, $object_type ); |
|
| 246 | + function get_link_title($post_id, $ignore_label, $object_type = Object_Type_Enum::POST) { |
|
| 247 | + return $this->object_link_provider->get_link_title($post_id, $ignore_label, $object_type); |
|
| 248 | 248 | } |
| 249 | 249 | |
| 250 | 250 | /** |
@@ -257,17 +257,17 @@ discard block |
||
| 257 | 257 | * @since 3.14.2 |
| 258 | 258 | * |
| 259 | 259 | */ |
| 260 | - public function get_entity_uris( $content ) { |
|
| 260 | + public function get_entity_uris($content) { |
|
| 261 | 261 | |
| 262 | 262 | $matches = array(); |
| 263 | - preg_match_all( Wordlift_Content_Filter_Service::PATTERN, $content, $matches ); |
|
| 263 | + preg_match_all(Wordlift_Content_Filter_Service::PATTERN, $content, $matches); |
|
| 264 | 264 | |
| 265 | 265 | // We need to use `array_values` here in order to avoid further `json_encode` |
| 266 | 266 | // to turn it into an object (since if the 3rd match isn't found the index |
| 267 | 267 | // is not sequential. |
| 268 | 268 | // |
| 269 | 269 | // See https://github.com/insideout10/wordlift-plugin/issues/646. |
| 270 | - return array_values( array_unique( $matches[3] ) ); |
|
| 270 | + return array_values(array_unique($matches[3])); |
|
| 271 | 271 | } |
| 272 | 272 | |
| 273 | 273 | |
@@ -276,8 +276,8 @@ discard block |
||
| 276 | 276 | * |
| 277 | 277 | * @return bool |
| 278 | 278 | */ |
| 279 | - private function is_already_linked( $entity_uri ) { |
|
| 280 | - return in_array( $entity_uri, $this->linked_entity_uris ); |
|
| 279 | + private function is_already_linked($entity_uri) { |
|
| 280 | + return in_array($entity_uri, $this->linked_entity_uris); |
|
| 281 | 281 | } |
| 282 | 282 | |
| 283 | 283 | } |
@@ -4,62 +4,62 @@ |
||
| 4 | 4 | |
| 5 | 5 | class Object_Type_Enum { |
| 6 | 6 | |
| 7 | - const POST = 0; |
|
| 8 | - const TERM = 1; |
|
| 9 | - const HOMEPAGE = 2; |
|
| 10 | - const USER = 3; |
|
| 7 | + const POST = 0; |
|
| 8 | + const TERM = 1; |
|
| 9 | + const HOMEPAGE = 2; |
|
| 10 | + const USER = 3; |
|
| 11 | 11 | |
| 12 | - // Enum constant to represent currently unknown |
|
| 13 | - // value. |
|
| 14 | - const UNKNOWN = 4; |
|
| 12 | + // Enum constant to represent currently unknown |
|
| 13 | + // value. |
|
| 14 | + const UNKNOWN = 4; |
|
| 15 | 15 | |
| 16 | - public static function to_string( $object_type_enum ) { |
|
| 17 | - switch ( $object_type_enum ) { |
|
| 18 | - case 0: |
|
| 19 | - return 'post'; |
|
| 20 | - case 1: |
|
| 21 | - return 'term'; |
|
| 22 | - case 2: |
|
| 23 | - return 'home'; |
|
| 24 | - case 3: |
|
| 25 | - return 'user'; |
|
| 26 | - case 4: |
|
| 27 | - return 'unkn'; |
|
| 28 | - } |
|
| 16 | + public static function to_string( $object_type_enum ) { |
|
| 17 | + switch ( $object_type_enum ) { |
|
| 18 | + case 0: |
|
| 19 | + return 'post'; |
|
| 20 | + case 1: |
|
| 21 | + return 'term'; |
|
| 22 | + case 2: |
|
| 23 | + return 'home'; |
|
| 24 | + case 3: |
|
| 25 | + return 'user'; |
|
| 26 | + case 4: |
|
| 27 | + return 'unkn'; |
|
| 28 | + } |
|
| 29 | 29 | |
| 30 | - return null; |
|
| 31 | - } |
|
| 30 | + return null; |
|
| 31 | + } |
|
| 32 | 32 | |
| 33 | - public static function from_string( $object_type_name ) { |
|
| 34 | - switch ( $object_type_name ) { |
|
| 35 | - case 'post': |
|
| 36 | - return 0; |
|
| 37 | - case 'term': |
|
| 38 | - return 1; |
|
| 39 | - case 'home': |
|
| 40 | - return 2; |
|
| 41 | - case 'user': |
|
| 42 | - return 3; |
|
| 43 | - } |
|
| 33 | + public static function from_string( $object_type_name ) { |
|
| 34 | + switch ( $object_type_name ) { |
|
| 35 | + case 'post': |
|
| 36 | + return 0; |
|
| 37 | + case 'term': |
|
| 38 | + return 1; |
|
| 39 | + case 'home': |
|
| 40 | + return 2; |
|
| 41 | + case 'user': |
|
| 42 | + return 3; |
|
| 43 | + } |
|
| 44 | 44 | |
| 45 | - return 4; |
|
| 46 | - } |
|
| 45 | + return 4; |
|
| 46 | + } |
|
| 47 | 47 | |
| 48 | - public static function from_wordpress_instance( $instance ) { |
|
| 49 | - if ( ! is_object( $instance ) ) { |
|
| 50 | - return null; |
|
| 51 | - } |
|
| 48 | + public static function from_wordpress_instance( $instance ) { |
|
| 49 | + if ( ! is_object( $instance ) ) { |
|
| 50 | + return null; |
|
| 51 | + } |
|
| 52 | 52 | |
| 53 | - switch ( get_class( $instance ) ) { |
|
| 54 | - case 'WP_Post': |
|
| 55 | - return self::POST; |
|
| 56 | - case 'WP_Term': |
|
| 57 | - return self::TERM; |
|
| 58 | - case 'WP_User': |
|
| 59 | - return self::USER; |
|
| 60 | - } |
|
| 53 | + switch ( get_class( $instance ) ) { |
|
| 54 | + case 'WP_Post': |
|
| 55 | + return self::POST; |
|
| 56 | + case 'WP_Term': |
|
| 57 | + return self::TERM; |
|
| 58 | + case 'WP_User': |
|
| 59 | + return self::USER; |
|
| 60 | + } |
|
| 61 | 61 | |
| 62 | - return null; |
|
| 63 | - } |
|
| 62 | + return null; |
|
| 63 | + } |
|
| 64 | 64 | |
| 65 | 65 | } |
@@ -13,8 +13,8 @@ discard block |
||
| 13 | 13 | // value. |
| 14 | 14 | const UNKNOWN = 4; |
| 15 | 15 | |
| 16 | - public static function to_string( $object_type_enum ) { |
|
| 17 | - switch ( $object_type_enum ) { |
|
| 16 | + public static function to_string($object_type_enum) { |
|
| 17 | + switch ($object_type_enum) { |
|
| 18 | 18 | case 0: |
| 19 | 19 | return 'post'; |
| 20 | 20 | case 1: |
@@ -30,8 +30,8 @@ discard block |
||
| 30 | 30 | return null; |
| 31 | 31 | } |
| 32 | 32 | |
| 33 | - public static function from_string( $object_type_name ) { |
|
| 34 | - switch ( $object_type_name ) { |
|
| 33 | + public static function from_string($object_type_name) { |
|
| 34 | + switch ($object_type_name) { |
|
| 35 | 35 | case 'post': |
| 36 | 36 | return 0; |
| 37 | 37 | case 'term': |
@@ -45,12 +45,12 @@ discard block |
||
| 45 | 45 | return 4; |
| 46 | 46 | } |
| 47 | 47 | |
| 48 | - public static function from_wordpress_instance( $instance ) { |
|
| 49 | - if ( ! is_object( $instance ) ) { |
|
| 48 | + public static function from_wordpress_instance($instance) { |
|
| 49 | + if ( ! is_object($instance)) { |
|
| 50 | 50 | return null; |
| 51 | 51 | } |
| 52 | 52 | |
| 53 | - switch ( get_class( $instance ) ) { |
|
| 53 | + switch (get_class($instance)) { |
|
| 54 | 54 | case 'WP_Post': |
| 55 | 55 | return self::POST; |
| 56 | 56 | case 'WP_Term': |
@@ -3,7 +3,7 @@ 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; |
@@ -18,35 +18,35 @@ discard block |
||
| 18 | 18 | // 'ajax' => false, |
| 19 | 19 | // ] ); |
| 20 | 20 | // } |
| 21 | - public function prepare_items() { |
|
| 22 | - global $wpdb; //This is used only if making any database queries |
|
| 23 | - |
|
| 24 | - /** |
|
| 25 | - * First, lets decide how many records per page to show |
|
| 26 | - */ |
|
| 27 | - $per_page = 5; |
|
| 28 | - |
|
| 29 | - |
|
| 30 | - /** |
|
| 31 | - * REQUIRED. Now we need to define our column headers. This includes a complete |
|
| 32 | - * array of columns to be displayed (slugs & titles), a list of columns |
|
| 33 | - * to keep hidden, and a list of columns that are sortable. Each of these |
|
| 34 | - * can be defined in another method (as we've done here) before being |
|
| 35 | - * used to build the value for our _column_headers property. |
|
| 36 | - */ |
|
| 37 | - $columns = $this->get_columns(); |
|
| 38 | - $hidden = array(); |
|
| 39 | - $sortable = $this->get_sortable_columns(); |
|
| 40 | - |
|
| 41 | - /** |
|
| 42 | - * REQUIRED. Finally, we build an array to be used by the class for column |
|
| 43 | - * headers. The $this->_column_headers property takes an array which contains |
|
| 44 | - * 3 other arrays. One for all columns, one for hidden columns, and one |
|
| 45 | - * for sortable columns. |
|
| 46 | - */ |
|
| 47 | - $this->_column_headers = array( $columns, $hidden, $sortable ); |
|
| 48 | - |
|
| 49 | - $sql = " |
|
| 21 | + public function prepare_items() { |
|
| 22 | + global $wpdb; //This is used only if making any database queries |
|
| 23 | + |
|
| 24 | + /** |
|
| 25 | + * First, lets decide how many records per page to show |
|
| 26 | + */ |
|
| 27 | + $per_page = 5; |
|
| 28 | + |
|
| 29 | + |
|
| 30 | + /** |
|
| 31 | + * REQUIRED. Now we need to define our column headers. This includes a complete |
|
| 32 | + * array of columns to be displayed (slugs & titles), a list of columns |
|
| 33 | + * to keep hidden, and a list of columns that are sortable. Each of these |
|
| 34 | + * can be defined in another method (as we've done here) before being |
|
| 35 | + * used to build the value for our _column_headers property. |
|
| 36 | + */ |
|
| 37 | + $columns = $this->get_columns(); |
|
| 38 | + $hidden = array(); |
|
| 39 | + $sortable = $this->get_sortable_columns(); |
|
| 40 | + |
|
| 41 | + /** |
|
| 42 | + * REQUIRED. Finally, we build an array to be used by the class for column |
|
| 43 | + * headers. The $this->_column_headers property takes an array which contains |
|
| 44 | + * 3 other arrays. One for all columns, one for hidden columns, and one |
|
| 45 | + * for sortable columns. |
|
| 46 | + */ |
|
| 47 | + $this->_column_headers = array( $columns, $hidden, $sortable ); |
|
| 48 | + |
|
| 49 | + $sql = " |
|
| 50 | 50 | SELECT t.term_id, t.name, tm.meta_value |
| 51 | 51 | FROM $wpdb->term_taxonomy tt |
| 52 | 52 | INNER JOIN $wpdb->terms t |
@@ -57,33 +57,33 @@ discard block |
||
| 57 | 57 | WHERE tt.taxonomy = %s; |
| 58 | 58 | "; |
| 59 | 59 | |
| 60 | - $this->items = $wpdb->get_results( $wpdb->prepare( $sql, 'wprm_ingredient' ) ); |
|
| 61 | - } |
|
| 60 | + $this->items = $wpdb->get_results( $wpdb->prepare( $sql, 'wprm_ingredient' ) ); |
|
| 61 | + } |
|
| 62 | 62 | |
| 63 | 63 | |
| 64 | - public function no_items() { |
|
| 65 | - _e( 'No ingredients found.', 'wordlift' ); |
|
| 66 | - } |
|
| 64 | + public function no_items() { |
|
| 65 | + _e( 'No ingredients found.', 'wordlift' ); |
|
| 66 | + } |
|
| 67 | 67 | |
| 68 | - public function get_columns() { |
|
| 69 | - return [ |
|
| 70 | - 'name' => __( 'Name', 'wordlift' ), |
|
| 71 | - 'actions' => '', |
|
| 72 | - ]; |
|
| 73 | - } |
|
| 68 | + public function get_columns() { |
|
| 69 | + return [ |
|
| 70 | + 'name' => __( 'Name', 'wordlift' ), |
|
| 71 | + 'actions' => '', |
|
| 72 | + ]; |
|
| 73 | + } |
|
| 74 | 74 | |
| 75 | - public function column_name( $item ) { |
|
| 76 | - return sprintf( '<a href="%s">%s</a>', get_edit_term_link( $item->term_id ), $item->name ); |
|
| 77 | - } |
|
| 75 | + public function column_name( $item ) { |
|
| 76 | + return sprintf( '<a href="%s">%s</a>', get_edit_term_link( $item->term_id ), $item->name ); |
|
| 77 | + } |
|
| 78 | 78 | |
| 79 | - public function column_actions( $item ) { |
|
| 79 | + public function column_actions( $item ) { |
|
| 80 | 80 | |
| 81 | - $url = admin_url( |
|
| 82 | - sprintf( 'admin.php?page=wl_ingredients&modal_window=true&term_id=%d&TB_iframe=true', $item->term_id ) ); |
|
| 81 | + $url = admin_url( |
|
| 82 | + sprintf( 'admin.php?page=wl_ingredients&modal_window=true&term_id=%d&TB_iframe=true', $item->term_id ) ); |
|
| 83 | 83 | |
| 84 | - return sprintf( '<a href="%s" class="button alignright thickbox open-plugin-details-modal" data-title="%s" type="button">%s</a>', |
|
| 85 | - $url, esc_attr( $item->name ), esc_html__( 'JSON-LD', 'wordlift' ) ); |
|
| 86 | - } |
|
| 84 | + return sprintf( '<a href="%s" class="button alignright thickbox open-plugin-details-modal" data-title="%s" type="button">%s</a>', |
|
| 85 | + $url, esc_attr( $item->name ), esc_html__( 'JSON-LD', 'wordlift' ) ); |
|
| 86 | + } |
|
| 87 | 87 | |
| 88 | 88 | } |
| 89 | 89 | |
@@ -2,8 +2,8 @@ discard block |
||
| 2 | 2 | |
| 3 | 3 | namespace Wordlift\Modules\Food_Kg\Admin; |
| 4 | 4 | |
| 5 | -if ( ! class_exists( 'WP_List_Table' ) ) { |
|
| 6 | - require_once( ABSPATH . 'wp-admin/includes/class-wp-list-table.php' ); |
|
| 5 | +if ( ! class_exists('WP_List_Table')) { |
|
| 6 | + require_once(ABSPATH.'wp-admin/includes/class-wp-list-table.php'); |
|
| 7 | 7 | } |
| 8 | 8 | |
| 9 | 9 | use WP_List_Table; |
@@ -44,7 +44,7 @@ discard block |
||
| 44 | 44 | * 3 other arrays. One for all columns, one for hidden columns, and one |
| 45 | 45 | * for sortable columns. |
| 46 | 46 | */ |
| 47 | - $this->_column_headers = array( $columns, $hidden, $sortable ); |
|
| 47 | + $this->_column_headers = array($columns, $hidden, $sortable); |
|
| 48 | 48 | |
| 49 | 49 | $sql = " |
| 50 | 50 | SELECT t.term_id, t.name, tm.meta_value |
@@ -57,32 +57,32 @@ discard block |
||
| 57 | 57 | WHERE tt.taxonomy = %s; |
| 58 | 58 | "; |
| 59 | 59 | |
| 60 | - $this->items = $wpdb->get_results( $wpdb->prepare( $sql, 'wprm_ingredient' ) ); |
|
| 60 | + $this->items = $wpdb->get_results($wpdb->prepare($sql, 'wprm_ingredient')); |
|
| 61 | 61 | } |
| 62 | 62 | |
| 63 | 63 | |
| 64 | 64 | public function no_items() { |
| 65 | - _e( 'No ingredients found.', 'wordlift' ); |
|
| 65 | + _e('No ingredients found.', 'wordlift'); |
|
| 66 | 66 | } |
| 67 | 67 | |
| 68 | 68 | public function get_columns() { |
| 69 | 69 | return [ |
| 70 | - 'name' => __( 'Name', 'wordlift' ), |
|
| 70 | + 'name' => __('Name', 'wordlift'), |
|
| 71 | 71 | 'actions' => '', |
| 72 | 72 | ]; |
| 73 | 73 | } |
| 74 | 74 | |
| 75 | - public function column_name( $item ) { |
|
| 76 | - return sprintf( '<a href="%s">%s</a>', get_edit_term_link( $item->term_id ), $item->name ); |
|
| 75 | + public function column_name($item) { |
|
| 76 | + return sprintf('<a href="%s">%s</a>', get_edit_term_link($item->term_id), $item->name); |
|
| 77 | 77 | } |
| 78 | 78 | |
| 79 | - public function column_actions( $item ) { |
|
| 79 | + public function column_actions($item) { |
|
| 80 | 80 | |
| 81 | 81 | $url = admin_url( |
| 82 | - sprintf( 'admin.php?page=wl_ingredients&modal_window=true&term_id=%d&TB_iframe=true', $item->term_id ) ); |
|
| 82 | + sprintf('admin.php?page=wl_ingredients&modal_window=true&term_id=%d&TB_iframe=true', $item->term_id) ); |
|
| 83 | 83 | |
| 84 | - return sprintf( '<a href="%s" class="button alignright thickbox open-plugin-details-modal" data-title="%s" type="button">%s</a>', |
|
| 85 | - $url, esc_attr( $item->name ), esc_html__( 'JSON-LD', 'wordlift' ) ); |
|
| 84 | + return sprintf('<a href="%s" class="button alignright thickbox open-plugin-details-modal" data-title="%s" type="button">%s</a>', |
|
| 85 | + $url, esc_attr($item->name), esc_html__('JSON-LD', 'wordlift')); |
|
| 86 | 86 | } |
| 87 | 87 | |
| 88 | 88 | } |
@@ -7,9 +7,9 @@ |
||
| 7 | 7 | <h1><?php esc_attr_e( 'Ingredients', 'wordlift' ); ?></h1> |
| 8 | 8 | <?php |
| 9 | 9 | |
| 10 | - //Prepare Table of elements |
|
| 11 | - $wp_list_table = new Ingredients_List_Table(); |
|
| 12 | - $wp_list_table->prepare_items(); |
|
| 13 | - $wp_list_table->display(); |
|
| 14 | - ?> |
|
| 10 | + //Prepare Table of elements |
|
| 11 | + $wp_list_table = new Ingredients_List_Table(); |
|
| 12 | + $wp_list_table->prepare_items(); |
|
| 13 | + $wp_list_table->display(); |
|
| 14 | + ?> |
|
| 15 | 15 | </div> |
@@ -4,7 +4,7 @@ |
||
| 4 | 4 | |
| 5 | 5 | ?> |
| 6 | 6 | <div class="wrap"> |
| 7 | - <h1><?php esc_attr_e( 'Ingredients', 'wordlift' ); ?></h1> |
|
| 7 | + <h1><?php esc_attr_e('Ingredients', 'wordlift'); ?></h1> |
|
| 8 | 8 | <?php |
| 9 | 9 | |
| 10 | 10 | //Prepare Table of elements |
@@ -2,8 +2,8 @@ |
||
| 2 | 2 | <?php |
| 3 | 3 | |
| 4 | 4 | /** @var string $value */ |
| 5 | - $json = wp_json_encode( json_decode( $value ), JSON_PRETTY_PRINT ); |
|
| 6 | - echo esc_html( $json ); |
|
| 5 | + $json = wp_json_encode(json_decode($value), JSON_PRETTY_PRINT); |
|
| 6 | + echo esc_html($json); |
|
| 7 | 7 | |
| 8 | 8 | ?> |
| 9 | 9 | </textarea> |
| 10 | 10 | \ No newline at end of file |
@@ -7,9 +7,9 @@ |
||
| 7 | 7 | <h1><?php esc_attr_e( 'Main Ingredients', 'wordlift' ); ?></h1> |
| 8 | 8 | <?php |
| 9 | 9 | |
| 10 | - //Prepare Table of elements |
|
| 11 | - $wp_list_table = new Main_Ingredient_List_Table(); |
|
| 12 | - $wp_list_table->prepare_items(); |
|
| 13 | - $wp_list_table->display(); |
|
| 14 | - ?> |
|
| 10 | + //Prepare Table of elements |
|
| 11 | + $wp_list_table = new Main_Ingredient_List_Table(); |
|
| 12 | + $wp_list_table->prepare_items(); |
|
| 13 | + $wp_list_table->display(); |
|
| 14 | + ?> |
|
| 15 | 15 | </div> |
@@ -4,7 +4,7 @@ |
||
| 4 | 4 | |
| 5 | 5 | ?> |
| 6 | 6 | <div class="wrap"> |
| 7 | - <h1><?php esc_attr_e( 'Main Ingredients', 'wordlift' ); ?></h1> |
|
| 7 | + <h1><?php esc_attr_e('Main Ingredients', 'wordlift'); ?></h1> |
|
| 8 | 8 | <?php |
| 9 | 9 | |
| 10 | 10 | //Prepare Table of elements |
@@ -4,38 +4,38 @@ |
||
| 4 | 4 | |
| 5 | 5 | class Page { |
| 6 | 6 | |
| 7 | - /** |
|
| 8 | - * @var Page_Delegate |
|
| 9 | - */ |
|
| 10 | - private $delegate; |
|
| 11 | - |
|
| 12 | - /** |
|
| 13 | - * @param Page_Delegate $full_page_delegate |
|
| 14 | - * @param Page_Delegate $modal_page_delegate |
|
| 15 | - */ |
|
| 16 | - public function __construct( $full_page_delegate, $modal_page_delegate ) { |
|
| 17 | - $this->delegate = isset( $_GET['modal_window'] ) ? $modal_page_delegate : $full_page_delegate; |
|
| 18 | - } |
|
| 19 | - |
|
| 20 | - public function register_hooks() { |
|
| 21 | - add_action( 'admin_menu', [ $this, 'admin_menu' ] ); |
|
| 22 | - add_action( 'admin_enqueue_scripts', [ $this, 'admin_enqueue_scripts' ] ); |
|
| 23 | - } |
|
| 24 | - |
|
| 25 | - public function admin_menu() { |
|
| 26 | - add_submenu_page( 'wl_admin_menu', __( 'Ingredients', 'wordlift' ), __( 'Ingredients', 'wordlift' ), |
|
| 27 | - 'manage_options', 'wl_ingredients', [ $this, 'render' ] ); |
|
| 28 | - } |
|
| 29 | - |
|
| 30 | - public function render() { |
|
| 31 | - $this->delegate->render(); |
|
| 32 | - } |
|
| 33 | - |
|
| 34 | - public function admin_enqueue_scripts() { |
|
| 35 | - // Check that we are on the right screen |
|
| 36 | - if ( get_current_screen()->id === 'wordlift_page_wl_ingredients' ) { |
|
| 37 | - $this->delegate->admin_enqueue_scripts(); |
|
| 38 | - } |
|
| 39 | - } |
|
| 7 | + /** |
|
| 8 | + * @var Page_Delegate |
|
| 9 | + */ |
|
| 10 | + private $delegate; |
|
| 11 | + |
|
| 12 | + /** |
|
| 13 | + * @param Page_Delegate $full_page_delegate |
|
| 14 | + * @param Page_Delegate $modal_page_delegate |
|
| 15 | + */ |
|
| 16 | + public function __construct( $full_page_delegate, $modal_page_delegate ) { |
|
| 17 | + $this->delegate = isset( $_GET['modal_window'] ) ? $modal_page_delegate : $full_page_delegate; |
|
| 18 | + } |
|
| 19 | + |
|
| 20 | + public function register_hooks() { |
|
| 21 | + add_action( 'admin_menu', [ $this, 'admin_menu' ] ); |
|
| 22 | + add_action( 'admin_enqueue_scripts', [ $this, 'admin_enqueue_scripts' ] ); |
|
| 23 | + } |
|
| 24 | + |
|
| 25 | + public function admin_menu() { |
|
| 26 | + add_submenu_page( 'wl_admin_menu', __( 'Ingredients', 'wordlift' ), __( 'Ingredients', 'wordlift' ), |
|
| 27 | + 'manage_options', 'wl_ingredients', [ $this, 'render' ] ); |
|
| 28 | + } |
|
| 29 | + |
|
| 30 | + public function render() { |
|
| 31 | + $this->delegate->render(); |
|
| 32 | + } |
|
| 33 | + |
|
| 34 | + public function admin_enqueue_scripts() { |
|
| 35 | + // Check that we are on the right screen |
|
| 36 | + if ( get_current_screen()->id === 'wordlift_page_wl_ingredients' ) { |
|
| 37 | + $this->delegate->admin_enqueue_scripts(); |
|
| 38 | + } |
|
| 39 | + } |
|
| 40 | 40 | |
| 41 | 41 | } |
@@ -13,18 +13,18 @@ discard block |
||
| 13 | 13 | * @param Page_Delegate $full_page_delegate |
| 14 | 14 | * @param Page_Delegate $modal_page_delegate |
| 15 | 15 | */ |
| 16 | - public function __construct( $full_page_delegate, $modal_page_delegate ) { |
|
| 17 | - $this->delegate = isset( $_GET['modal_window'] ) ? $modal_page_delegate : $full_page_delegate; |
|
| 16 | + public function __construct($full_page_delegate, $modal_page_delegate) { |
|
| 17 | + $this->delegate = isset($_GET['modal_window']) ? $modal_page_delegate : $full_page_delegate; |
|
| 18 | 18 | } |
| 19 | 19 | |
| 20 | 20 | public function register_hooks() { |
| 21 | - add_action( 'admin_menu', [ $this, 'admin_menu' ] ); |
|
| 22 | - add_action( 'admin_enqueue_scripts', [ $this, 'admin_enqueue_scripts' ] ); |
|
| 21 | + add_action('admin_menu', [$this, 'admin_menu']); |
|
| 22 | + add_action('admin_enqueue_scripts', [$this, 'admin_enqueue_scripts']); |
|
| 23 | 23 | } |
| 24 | 24 | |
| 25 | 25 | public function admin_menu() { |
| 26 | - add_submenu_page( 'wl_admin_menu', __( 'Ingredients', 'wordlift' ), __( 'Ingredients', 'wordlift' ), |
|
| 27 | - 'manage_options', 'wl_ingredients', [ $this, 'render' ] ); |
|
| 26 | + add_submenu_page('wl_admin_menu', __('Ingredients', 'wordlift'), __('Ingredients', 'wordlift'), |
|
| 27 | + 'manage_options', 'wl_ingredients', [$this, 'render']); |
|
| 28 | 28 | } |
| 29 | 29 | |
| 30 | 30 | public function render() { |
@@ -33,7 +33,7 @@ discard block |
||
| 33 | 33 | |
| 34 | 34 | public function admin_enqueue_scripts() { |
| 35 | 35 | // Check that we are on the right screen |
| 36 | - if ( get_current_screen()->id === 'wordlift_page_wl_ingredients' ) { |
|
| 36 | + if (get_current_screen()->id === 'wordlift_page_wl_ingredients') { |
|
| 37 | 37 | $this->delegate->admin_enqueue_scripts(); |
| 38 | 38 | } |
| 39 | 39 | } |
@@ -3,7 +3,7 @@ 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; |
@@ -11,35 +11,35 @@ discard block |
||
| 11 | 11 | |
| 12 | 12 | class Main_Ingredient_List_Table extends WP_List_Table { |
| 13 | 13 | |
| 14 | - public function prepare_items() { |
|
| 15 | - global $wpdb; //This is used only if making any database queries |
|
| 16 | - |
|
| 17 | - /** |
|
| 18 | - * First, lets decide how many records per page to show |
|
| 19 | - */ |
|
| 20 | - $per_page = 5; |
|
| 21 | - |
|
| 22 | - |
|
| 23 | - /** |
|
| 24 | - * REQUIRED. Now we need to define our column headers. This includes a complete |
|
| 25 | - * array of columns to be displayed (slugs & titles), a list of columns |
|
| 26 | - * to keep hidden, and a list of columns that are sortable. Each of these |
|
| 27 | - * can be defined in another method (as we've done here) before being |
|
| 28 | - * used to build the value for our _column_headers property. |
|
| 29 | - */ |
|
| 30 | - $columns = $this->get_columns(); |
|
| 31 | - $hidden = array(); |
|
| 32 | - $sortable = $this->get_sortable_columns(); |
|
| 33 | - |
|
| 34 | - /** |
|
| 35 | - * REQUIRED. Finally, we build an array to be used by the class for column |
|
| 36 | - * headers. The $this->_column_headers property takes an array which contains |
|
| 37 | - * 3 other arrays. One for all columns, one for hidden columns, and one |
|
| 38 | - * for sortable columns. |
|
| 39 | - */ |
|
| 40 | - $this->_column_headers = array( $columns, $hidden, $sortable ); |
|
| 41 | - |
|
| 42 | - $sql = " |
|
| 14 | + public function prepare_items() { |
|
| 15 | + global $wpdb; //This is used only if making any database queries |
|
| 16 | + |
|
| 17 | + /** |
|
| 18 | + * First, lets decide how many records per page to show |
|
| 19 | + */ |
|
| 20 | + $per_page = 5; |
|
| 21 | + |
|
| 22 | + |
|
| 23 | + /** |
|
| 24 | + * REQUIRED. Now we need to define our column headers. This includes a complete |
|
| 25 | + * array of columns to be displayed (slugs & titles), a list of columns |
|
| 26 | + * to keep hidden, and a list of columns that are sortable. Each of these |
|
| 27 | + * can be defined in another method (as we've done here) before being |
|
| 28 | + * used to build the value for our _column_headers property. |
|
| 29 | + */ |
|
| 30 | + $columns = $this->get_columns(); |
|
| 31 | + $hidden = array(); |
|
| 32 | + $sortable = $this->get_sortable_columns(); |
|
| 33 | + |
|
| 34 | + /** |
|
| 35 | + * REQUIRED. Finally, we build an array to be used by the class for column |
|
| 36 | + * headers. The $this->_column_headers property takes an array which contains |
|
| 37 | + * 3 other arrays. One for all columns, one for hidden columns, and one |
|
| 38 | + * for sortable columns. |
|
| 39 | + */ |
|
| 40 | + $this->_column_headers = array( $columns, $hidden, $sortable ); |
|
| 41 | + |
|
| 42 | + $sql = " |
|
| 43 | 43 | SELECT p.ID, p.post_title, pm.meta_value |
| 44 | 44 | FROM $wpdb->posts p |
| 45 | 45 | INNER JOIN $wpdb->postmeta pm |
@@ -47,36 +47,36 @@ discard block |
||
| 47 | 47 | AND pm.meta_key = '_wl_main_ingredient_jsonld'; |
| 48 | 48 | "; |
| 49 | 49 | |
| 50 | - $this->items = $wpdb->get_results( $wpdb->prepare( $sql ) ); |
|
| 51 | - } |
|
| 50 | + $this->items = $wpdb->get_results( $wpdb->prepare( $sql ) ); |
|
| 51 | + } |
|
| 52 | 52 | |
| 53 | 53 | |
| 54 | - public function no_items() { |
|
| 55 | - _e( 'No main ingredients found.', 'wordlift' ); |
|
| 56 | - } |
|
| 54 | + public function no_items() { |
|
| 55 | + _e( 'No main ingredients found.', 'wordlift' ); |
|
| 56 | + } |
|
| 57 | 57 | |
| 58 | - public function get_columns() { |
|
| 59 | - return [ |
|
| 60 | - 'post_title' => __( 'Name', 'wordlift' ), |
|
| 61 | - 'actions' => '', |
|
| 62 | - ]; |
|
| 63 | - } |
|
| 58 | + public function get_columns() { |
|
| 59 | + return [ |
|
| 60 | + 'post_title' => __( 'Name', 'wordlift' ), |
|
| 61 | + 'actions' => '', |
|
| 62 | + ]; |
|
| 63 | + } |
|
| 64 | 64 | |
| 65 | - public function column_post_title( $item ) { |
|
| 66 | - $recipe = WPRM_Recipe_Manager::get_recipe( $item->ID ); |
|
| 67 | - $edit_url = $recipe->parent_edit_url(); |
|
| 65 | + public function column_post_title( $item ) { |
|
| 66 | + $recipe = WPRM_Recipe_Manager::get_recipe( $item->ID ); |
|
| 67 | + $edit_url = $recipe->parent_edit_url(); |
|
| 68 | 68 | |
| 69 | - return empty( $edit_url ) ? $item->post_title : sprintf( '<a href="%s">%s</a>', $recipe->parent_edit_url(), $item->post_title ); |
|
| 70 | - } |
|
| 69 | + return empty( $edit_url ) ? $item->post_title : sprintf( '<a href="%s">%s</a>', $recipe->parent_edit_url(), $item->post_title ); |
|
| 70 | + } |
|
| 71 | 71 | |
| 72 | - public function column_actions( $item ) { |
|
| 72 | + public function column_actions( $item ) { |
|
| 73 | 73 | |
| 74 | - $url = admin_url( |
|
| 75 | - sprintf( 'admin.php?page=wl_ingredients&modal_window=true&id=%d&TB_iframe=true', $item->ID ) ); |
|
| 74 | + $url = admin_url( |
|
| 75 | + sprintf( 'admin.php?page=wl_ingredients&modal_window=true&id=%d&TB_iframe=true', $item->ID ) ); |
|
| 76 | 76 | |
| 77 | - return sprintf( '<a href="%s" class="button alignright thickbox open-plugin-details-modal" data-title="%s" type="button">%s</a>', |
|
| 78 | - $url, esc_attr( $item->post_title ), esc_html__( 'JSON-LD', 'wordlift' ) ); |
|
| 79 | - } |
|
| 77 | + return sprintf( '<a href="%s" class="button alignright thickbox open-plugin-details-modal" data-title="%s" type="button">%s</a>', |
|
| 78 | + $url, esc_attr( $item->post_title ), esc_html__( 'JSON-LD', 'wordlift' ) ); |
|
| 79 | + } |
|
| 80 | 80 | |
| 81 | 81 | } |
| 82 | 82 | |
@@ -2,8 +2,8 @@ discard block |
||
| 2 | 2 | |
| 3 | 3 | namespace Wordlift\Modules\Food_Kg\Admin; |
| 4 | 4 | |
| 5 | -if ( ! class_exists( 'WP_List_Table' ) ) { |
|
| 6 | - require_once( ABSPATH . 'wp-admin/includes/class-wp-list-table.php' ); |
|
| 5 | +if ( ! class_exists('WP_List_Table')) { |
|
| 6 | + require_once(ABSPATH.'wp-admin/includes/class-wp-list-table.php'); |
|
| 7 | 7 | } |
| 8 | 8 | |
| 9 | 9 | use WP_List_Table; |
@@ -37,7 +37,7 @@ discard block |
||
| 37 | 37 | * 3 other arrays. One for all columns, one for hidden columns, and one |
| 38 | 38 | * for sortable columns. |
| 39 | 39 | */ |
| 40 | - $this->_column_headers = array( $columns, $hidden, $sortable ); |
|
| 40 | + $this->_column_headers = array($columns, $hidden, $sortable); |
|
| 41 | 41 | |
| 42 | 42 | $sql = " |
| 43 | 43 | SELECT p.ID, p.post_title, pm.meta_value |
@@ -47,35 +47,35 @@ discard block |
||
| 47 | 47 | AND pm.meta_key = '_wl_main_ingredient_jsonld'; |
| 48 | 48 | "; |
| 49 | 49 | |
| 50 | - $this->items = $wpdb->get_results( $wpdb->prepare( $sql ) ); |
|
| 50 | + $this->items = $wpdb->get_results($wpdb->prepare($sql)); |
|
| 51 | 51 | } |
| 52 | 52 | |
| 53 | 53 | |
| 54 | 54 | public function no_items() { |
| 55 | - _e( 'No main ingredients found.', 'wordlift' ); |
|
| 55 | + _e('No main ingredients found.', 'wordlift'); |
|
| 56 | 56 | } |
| 57 | 57 | |
| 58 | 58 | public function get_columns() { |
| 59 | 59 | return [ |
| 60 | - 'post_title' => __( 'Name', 'wordlift' ), |
|
| 60 | + 'post_title' => __('Name', 'wordlift'), |
|
| 61 | 61 | 'actions' => '', |
| 62 | 62 | ]; |
| 63 | 63 | } |
| 64 | 64 | |
| 65 | - public function column_post_title( $item ) { |
|
| 66 | - $recipe = WPRM_Recipe_Manager::get_recipe( $item->ID ); |
|
| 65 | + public function column_post_title($item) { |
|
| 66 | + $recipe = WPRM_Recipe_Manager::get_recipe($item->ID); |
|
| 67 | 67 | $edit_url = $recipe->parent_edit_url(); |
| 68 | 68 | |
| 69 | - return empty( $edit_url ) ? $item->post_title : sprintf( '<a href="%s">%s</a>', $recipe->parent_edit_url(), $item->post_title ); |
|
| 69 | + return empty($edit_url) ? $item->post_title : sprintf('<a href="%s">%s</a>', $recipe->parent_edit_url(), $item->post_title); |
|
| 70 | 70 | } |
| 71 | 71 | |
| 72 | - public function column_actions( $item ) { |
|
| 72 | + public function column_actions($item) { |
|
| 73 | 73 | |
| 74 | 74 | $url = admin_url( |
| 75 | - sprintf( 'admin.php?page=wl_ingredients&modal_window=true&id=%d&TB_iframe=true', $item->ID ) ); |
|
| 75 | + sprintf('admin.php?page=wl_ingredients&modal_window=true&id=%d&TB_iframe=true', $item->ID) ); |
|
| 76 | 76 | |
| 77 | - return sprintf( '<a href="%s" class="button alignright thickbox open-plugin-details-modal" data-title="%s" type="button">%s</a>', |
|
| 78 | - $url, esc_attr( $item->post_title ), esc_html__( 'JSON-LD', 'wordlift' ) ); |
|
| 77 | + return sprintf('<a href="%s" class="button alignright thickbox open-plugin-details-modal" data-title="%s" type="button">%s</a>', |
|
| 78 | + $url, esc_attr($item->post_title), esc_html__('JSON-LD', 'wordlift')); |
|
| 79 | 79 | } |
| 80 | 80 | |
| 81 | 81 | } |