@@ -15,47 +15,47 @@ discard block |
||
| 15 | 15 | * @return mixed |
| 16 | 16 | */ |
| 17 | 17 | function wl_shortcode_chord_most_referenced_entity_id() { |
| 18 | - // Get the last 20 articles by post date. |
|
| 19 | - // For each article get the entities they reference. |
|
| 20 | - $post_ids = get_posts( |
|
| 21 | - array( |
|
| 22 | - 'numberposts' => 20, |
|
| 23 | - 'post_type' => Wordlift_Entity_Service::valid_entity_post_types(), |
|
| 24 | - 'fields' => 'ids', // Only get post IDs. |
|
| 25 | - 'post_status' => 'publish', |
|
| 26 | - 'tax_query' => array( |
|
| 27 | - 'relation' => 'OR', |
|
| 28 | - array( |
|
| 29 | - 'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
| 30 | - 'operator' => 'NOT EXISTS', |
|
| 31 | - ), |
|
| 32 | - array( |
|
| 33 | - 'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
| 34 | - 'field' => 'slug', |
|
| 35 | - 'terms' => 'article', |
|
| 36 | - ), |
|
| 37 | - ), |
|
| 38 | - 'orderby' => 'post_date', |
|
| 39 | - 'order' => 'DESC', |
|
| 40 | - ) |
|
| 41 | - ); |
|
| 42 | - |
|
| 43 | - if ( empty( $post_ids ) ) { |
|
| 44 | - return null; |
|
| 45 | - } |
|
| 46 | - |
|
| 47 | - $entities = array(); |
|
| 48 | - foreach ( $post_ids as $id ) { |
|
| 49 | - $entities = array_merge( $entities, wl_core_get_related_entity_ids( $id ) ); |
|
| 50 | - } |
|
| 51 | - |
|
| 52 | - $famous_entities = array_count_values( $entities ); |
|
| 53 | - arsort( $famous_entities ); |
|
| 54 | - if ( count( $famous_entities ) >= 1 ) { |
|
| 55 | - return key( $famous_entities ); |
|
| 56 | - } else { |
|
| 57 | - return $post_ids[0]; |
|
| 58 | - } |
|
| 18 | + // Get the last 20 articles by post date. |
|
| 19 | + // For each article get the entities they reference. |
|
| 20 | + $post_ids = get_posts( |
|
| 21 | + array( |
|
| 22 | + 'numberposts' => 20, |
|
| 23 | + 'post_type' => Wordlift_Entity_Service::valid_entity_post_types(), |
|
| 24 | + 'fields' => 'ids', // Only get post IDs. |
|
| 25 | + 'post_status' => 'publish', |
|
| 26 | + 'tax_query' => array( |
|
| 27 | + 'relation' => 'OR', |
|
| 28 | + array( |
|
| 29 | + 'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
| 30 | + 'operator' => 'NOT EXISTS', |
|
| 31 | + ), |
|
| 32 | + array( |
|
| 33 | + 'taxonomy' => Wordlift_Entity_Type_Taxonomy_Service::TAXONOMY_NAME, |
|
| 34 | + 'field' => 'slug', |
|
| 35 | + 'terms' => 'article', |
|
| 36 | + ), |
|
| 37 | + ), |
|
| 38 | + 'orderby' => 'post_date', |
|
| 39 | + 'order' => 'DESC', |
|
| 40 | + ) |
|
| 41 | + ); |
|
| 42 | + |
|
| 43 | + if ( empty( $post_ids ) ) { |
|
| 44 | + return null; |
|
| 45 | + } |
|
| 46 | + |
|
| 47 | + $entities = array(); |
|
| 48 | + foreach ( $post_ids as $id ) { |
|
| 49 | + $entities = array_merge( $entities, wl_core_get_related_entity_ids( $id ) ); |
|
| 50 | + } |
|
| 51 | + |
|
| 52 | + $famous_entities = array_count_values( $entities ); |
|
| 53 | + arsort( $famous_entities ); |
|
| 54 | + if ( count( $famous_entities ) >= 1 ) { |
|
| 55 | + return key( $famous_entities ); |
|
| 56 | + } else { |
|
| 57 | + return $post_ids[0]; |
|
| 58 | + } |
|
| 59 | 59 | |
| 60 | 60 | } |
| 61 | 61 | |
@@ -72,66 +72,66 @@ discard block |
||
| 72 | 72 | */ |
| 73 | 73 | function wl_shortcode_chord_get_relations( $entity_id, $depth = 2, $related = null, $max_size = 9 ) { |
| 74 | 74 | |
| 75 | - if ( $related !== null ) { |
|
| 76 | - if ( 0 === $depth ) { |
|
| 77 | - return $related; |
|
| 78 | - } |
|
| 79 | - } |
|
| 80 | - |
|
| 81 | - wl_write_log( "wl_shortcode_chord_get_relations [ post id :: $entity_id ][ depth :: $depth ][ related? :: " . ( $related === null ? 'yes' : 'no' ) . ' ]' ); |
|
| 82 | - |
|
| 83 | - // Create a related array which will hold entities and relations. |
|
| 84 | - if ( $related === null ) { |
|
| 85 | - $related = array( |
|
| 86 | - 'entities' => array( $entity_id ), |
|
| 87 | - 'relations' => array(), |
|
| 88 | - ); |
|
| 89 | - } |
|
| 90 | - |
|
| 91 | - // Get related entities |
|
| 92 | - $related_entity_ids = wl_core_get_related_entity_ids( |
|
| 93 | - $entity_id, |
|
| 94 | - array( |
|
| 95 | - 'status' => 'publish', |
|
| 96 | - ) |
|
| 97 | - ); |
|
| 98 | - |
|
| 99 | - // If the current node is an entity, add related posts too |
|
| 100 | - $related_post_ids = ( Wordlift_Entity_Service::get_instance() |
|
| 101 | - ->is_entity( $entity_id ) ) ? |
|
| 102 | - wl_core_get_related_post_ids( |
|
| 103 | - $entity_id, |
|
| 104 | - array( |
|
| 105 | - 'status' => 'publish', |
|
| 106 | - ) |
|
| 107 | - ) : |
|
| 108 | - array(); |
|
| 109 | - |
|
| 110 | - // Merge results and remove duplicated entries |
|
| 111 | - $related_ids = array_unique( array_merge( $related_post_ids, $related_entity_ids ) ); |
|
| 112 | - |
|
| 113 | - // TODO: List of entities ($rel) should be ordered by interest factors. |
|
| 114 | - shuffle( $related_ids ); |
|
| 115 | - |
|
| 116 | - // Now we have all the related IDs. |
|
| 117 | - foreach ( $related_ids as $related_id ) { |
|
| 118 | - |
|
| 119 | - if ( count( $related['entities'] ) >= $max_size ) { |
|
| 120 | - return $related; |
|
| 121 | - } |
|
| 122 | - |
|
| 123 | - $related['relations'][] = array( $entity_id, $related_id ); |
|
| 124 | - |
|
| 125 | - if ( ! in_array( $related_id, $related['entities'], true ) ) { |
|
| 126 | - // Found new related entity! |
|
| 127 | - $related['entities'][] = $related_id; |
|
| 128 | - |
|
| 129 | - $related = wl_shortcode_chord_get_relations( $related_id, ( $depth - 1 ), $related, $max_size ); |
|
| 130 | - } |
|
| 131 | - } |
|
| 132 | - |
|
| 133 | - // End condition 2: no more entities to search for. |
|
| 134 | - return $related; |
|
| 75 | + if ( $related !== null ) { |
|
| 76 | + if ( 0 === $depth ) { |
|
| 77 | + return $related; |
|
| 78 | + } |
|
| 79 | + } |
|
| 80 | + |
|
| 81 | + wl_write_log( "wl_shortcode_chord_get_relations [ post id :: $entity_id ][ depth :: $depth ][ related? :: " . ( $related === null ? 'yes' : 'no' ) . ' ]' ); |
|
| 82 | + |
|
| 83 | + // Create a related array which will hold entities and relations. |
|
| 84 | + if ( $related === null ) { |
|
| 85 | + $related = array( |
|
| 86 | + 'entities' => array( $entity_id ), |
|
| 87 | + 'relations' => array(), |
|
| 88 | + ); |
|
| 89 | + } |
|
| 90 | + |
|
| 91 | + // Get related entities |
|
| 92 | + $related_entity_ids = wl_core_get_related_entity_ids( |
|
| 93 | + $entity_id, |
|
| 94 | + array( |
|
| 95 | + 'status' => 'publish', |
|
| 96 | + ) |
|
| 97 | + ); |
|
| 98 | + |
|
| 99 | + // If the current node is an entity, add related posts too |
|
| 100 | + $related_post_ids = ( Wordlift_Entity_Service::get_instance() |
|
| 101 | + ->is_entity( $entity_id ) ) ? |
|
| 102 | + wl_core_get_related_post_ids( |
|
| 103 | + $entity_id, |
|
| 104 | + array( |
|
| 105 | + 'status' => 'publish', |
|
| 106 | + ) |
|
| 107 | + ) : |
|
| 108 | + array(); |
|
| 109 | + |
|
| 110 | + // Merge results and remove duplicated entries |
|
| 111 | + $related_ids = array_unique( array_merge( $related_post_ids, $related_entity_ids ) ); |
|
| 112 | + |
|
| 113 | + // TODO: List of entities ($rel) should be ordered by interest factors. |
|
| 114 | + shuffle( $related_ids ); |
|
| 115 | + |
|
| 116 | + // Now we have all the related IDs. |
|
| 117 | + foreach ( $related_ids as $related_id ) { |
|
| 118 | + |
|
| 119 | + if ( count( $related['entities'] ) >= $max_size ) { |
|
| 120 | + return $related; |
|
| 121 | + } |
|
| 122 | + |
|
| 123 | + $related['relations'][] = array( $entity_id, $related_id ); |
|
| 124 | + |
|
| 125 | + if ( ! in_array( $related_id, $related['entities'], true ) ) { |
|
| 126 | + // Found new related entity! |
|
| 127 | + $related['entities'][] = $related_id; |
|
| 128 | + |
|
| 129 | + $related = wl_shortcode_chord_get_relations( $related_id, ( $depth - 1 ), $related, $max_size ); |
|
| 130 | + } |
|
| 131 | + } |
|
| 132 | + |
|
| 133 | + // End condition 2: no more entities to search for. |
|
| 134 | + return $related; |
|
| 135 | 135 | } |
| 136 | 136 | |
| 137 | 137 | /** |
@@ -145,63 +145,63 @@ discard block |
||
| 145 | 145 | */ |
| 146 | 146 | function wl_shortcode_chord_get_graph( $data ) { |
| 147 | 147 | |
| 148 | - // Refactor the entities array in order to provide entities relevant data (uri, url, label, type, css_class). |
|
| 149 | - array_walk( |
|
| 150 | - $data['entities'], |
|
| 151 | - function ( &$item ) { |
|
| 152 | - $post = get_post( $item ); |
|
| 153 | - |
|
| 154 | - // Skip non-existing posts. |
|
| 155 | - if ( $post === null ) { |
|
| 156 | - wl_write_log( "wl_shortcode_chord_get_graph : post not found [ post id :: $item ]" ); |
|
| 157 | - |
|
| 158 | - return $item; |
|
| 159 | - } |
|
| 160 | - |
|
| 161 | - // Get the entity taxonomy bound to this post (if there's no taxonomy, no stylesheet will be set). |
|
| 162 | - $term = Wordlift_Entity_Type_Service::get_instance()->get( $item ); |
|
| 163 | - |
|
| 164 | - // The following log may create a circular loop. |
|
| 165 | - // wl_write_log( "wl_shortcode_chord_get_graph [ post id :: $post->ID ][ term :: " . var_export( $term, true ) . " ]" ); |
|
| 166 | - |
|
| 167 | - // TODO: get all images |
|
| 168 | - $thumbnail = null; |
|
| 169 | - $thumbnail_id = get_post_thumbnail_id( $post->ID ); |
|
| 170 | - if ( '' !== $thumbnail_id && 0 !== $thumbnail_id ) { |
|
| 171 | - $attachment = wp_get_attachment_image_src( $thumbnail_id ); |
|
| 172 | - if ( false !== $attachment ) { |
|
| 173 | - $thumbnail = esc_attr( $attachment[0] ); |
|
| 174 | - } |
|
| 175 | - } |
|
| 176 | - |
|
| 177 | - $entity = array( |
|
| 178 | - 'uri' => wl_get_entity_uri( $item ), |
|
| 179 | - 'url' => get_permalink( $item ), |
|
| 180 | - 'label' => $post->post_title, |
|
| 181 | - 'type' => $post->post_type, |
|
| 182 | - 'thumbnails' => array( $thumbnail ), |
|
| 183 | - 'css_class' => ( isset( $term['css_class'] ) ? $term['css_class'] : '' ), |
|
| 184 | - ); |
|
| 185 | - |
|
| 186 | - $item = $entity; |
|
| 187 | - } |
|
| 188 | - ); |
|
| 189 | - |
|
| 190 | - // Refactor the relations. |
|
| 191 | - array_walk( |
|
| 192 | - $data['relations'], |
|
| 193 | - function ( &$item ) { |
|
| 194 | - $relation = array( |
|
| 195 | - 's' => wl_get_entity_uri( $item[0] ), |
|
| 196 | - 'o' => wl_get_entity_uri( $item[1] ), |
|
| 197 | - ); |
|
| 198 | - |
|
| 199 | - $item = $relation; |
|
| 200 | - } |
|
| 201 | - ); |
|
| 202 | - |
|
| 203 | - // Return the JSON representation. |
|
| 204 | - return $data; |
|
| 148 | + // Refactor the entities array in order to provide entities relevant data (uri, url, label, type, css_class). |
|
| 149 | + array_walk( |
|
| 150 | + $data['entities'], |
|
| 151 | + function ( &$item ) { |
|
| 152 | + $post = get_post( $item ); |
|
| 153 | + |
|
| 154 | + // Skip non-existing posts. |
|
| 155 | + if ( $post === null ) { |
|
| 156 | + wl_write_log( "wl_shortcode_chord_get_graph : post not found [ post id :: $item ]" ); |
|
| 157 | + |
|
| 158 | + return $item; |
|
| 159 | + } |
|
| 160 | + |
|
| 161 | + // Get the entity taxonomy bound to this post (if there's no taxonomy, no stylesheet will be set). |
|
| 162 | + $term = Wordlift_Entity_Type_Service::get_instance()->get( $item ); |
|
| 163 | + |
|
| 164 | + // The following log may create a circular loop. |
|
| 165 | + // wl_write_log( "wl_shortcode_chord_get_graph [ post id :: $post->ID ][ term :: " . var_export( $term, true ) . " ]" ); |
|
| 166 | + |
|
| 167 | + // TODO: get all images |
|
| 168 | + $thumbnail = null; |
|
| 169 | + $thumbnail_id = get_post_thumbnail_id( $post->ID ); |
|
| 170 | + if ( '' !== $thumbnail_id && 0 !== $thumbnail_id ) { |
|
| 171 | + $attachment = wp_get_attachment_image_src( $thumbnail_id ); |
|
| 172 | + if ( false !== $attachment ) { |
|
| 173 | + $thumbnail = esc_attr( $attachment[0] ); |
|
| 174 | + } |
|
| 175 | + } |
|
| 176 | + |
|
| 177 | + $entity = array( |
|
| 178 | + 'uri' => wl_get_entity_uri( $item ), |
|
| 179 | + 'url' => get_permalink( $item ), |
|
| 180 | + 'label' => $post->post_title, |
|
| 181 | + 'type' => $post->post_type, |
|
| 182 | + 'thumbnails' => array( $thumbnail ), |
|
| 183 | + 'css_class' => ( isset( $term['css_class'] ) ? $term['css_class'] : '' ), |
|
| 184 | + ); |
|
| 185 | + |
|
| 186 | + $item = $entity; |
|
| 187 | + } |
|
| 188 | + ); |
|
| 189 | + |
|
| 190 | + // Refactor the relations. |
|
| 191 | + array_walk( |
|
| 192 | + $data['relations'], |
|
| 193 | + function ( &$item ) { |
|
| 194 | + $relation = array( |
|
| 195 | + 's' => wl_get_entity_uri( $item[0] ), |
|
| 196 | + 'o' => wl_get_entity_uri( $item[1] ), |
|
| 197 | + ); |
|
| 198 | + |
|
| 199 | + $item = $relation; |
|
| 200 | + } |
|
| 201 | + ); |
|
| 202 | + |
|
| 203 | + // Return the JSON representation. |
|
| 204 | + return $data; |
|
| 205 | 205 | } |
| 206 | 206 | |
| 207 | 207 | /** |
@@ -212,15 +212,15 @@ discard block |
||
| 212 | 212 | */ |
| 213 | 213 | function wl_shortcode_chord_ajax() { |
| 214 | 214 | |
| 215 | - check_ajax_referer( 'wl_chord', 'wl_chord_nonce' ); |
|
| 215 | + check_ajax_referer( 'wl_chord', 'wl_chord_nonce' ); |
|
| 216 | 216 | |
| 217 | - $post_id = isset( $_REQUEST['post_id'] ) ? (int) $_REQUEST['post_id'] : 0; |
|
| 218 | - $depth = isset( $_REQUEST['depth'] ) ? (int) $_REQUEST['depth'] : 2; |
|
| 217 | + $post_id = isset( $_REQUEST['post_id'] ) ? (int) $_REQUEST['post_id'] : 0; |
|
| 218 | + $depth = isset( $_REQUEST['depth'] ) ? (int) $_REQUEST['depth'] : 2; |
|
| 219 | 219 | |
| 220 | - $relations = wl_shortcode_chord_get_relations( $post_id, $depth ); |
|
| 221 | - $graph = wl_shortcode_chord_get_graph( $relations ); |
|
| 220 | + $relations = wl_shortcode_chord_get_relations( $post_id, $depth ); |
|
| 221 | + $graph = wl_shortcode_chord_get_graph( $relations ); |
|
| 222 | 222 | |
| 223 | - wl_core_send_json( $graph ); |
|
| 223 | + wl_core_send_json( $graph ); |
|
| 224 | 224 | } |
| 225 | 225 | |
| 226 | 226 | add_action( 'wp_ajax_wl_chord', 'wl_shortcode_chord_ajax' ); |
@@ -40,19 +40,19 @@ discard block |
||
| 40 | 40 | ) |
| 41 | 41 | ); |
| 42 | 42 | |
| 43 | - if ( empty( $post_ids ) ) { |
|
| 43 | + if (empty($post_ids)) { |
|
| 44 | 44 | return null; |
| 45 | 45 | } |
| 46 | 46 | |
| 47 | 47 | $entities = array(); |
| 48 | - foreach ( $post_ids as $id ) { |
|
| 49 | - $entities = array_merge( $entities, wl_core_get_related_entity_ids( $id ) ); |
|
| 48 | + foreach ($post_ids as $id) { |
|
| 49 | + $entities = array_merge($entities, wl_core_get_related_entity_ids($id)); |
|
| 50 | 50 | } |
| 51 | 51 | |
| 52 | - $famous_entities = array_count_values( $entities ); |
|
| 53 | - arsort( $famous_entities ); |
|
| 54 | - if ( count( $famous_entities ) >= 1 ) { |
|
| 55 | - return key( $famous_entities ); |
|
| 52 | + $famous_entities = array_count_values($entities); |
|
| 53 | + arsort($famous_entities); |
|
| 54 | + if (count($famous_entities) >= 1) { |
|
| 55 | + return key($famous_entities); |
|
| 56 | 56 | } else { |
| 57 | 57 | return $post_ids[0]; |
| 58 | 58 | } |
@@ -70,20 +70,20 @@ discard block |
||
| 70 | 70 | * @return array |
| 71 | 71 | * @uses wl_core_get_related_post_ids() to get the list of post ids that reference an entity. |
| 72 | 72 | */ |
| 73 | -function wl_shortcode_chord_get_relations( $entity_id, $depth = 2, $related = null, $max_size = 9 ) { |
|
| 73 | +function wl_shortcode_chord_get_relations($entity_id, $depth = 2, $related = null, $max_size = 9) { |
|
| 74 | 74 | |
| 75 | - if ( $related !== null ) { |
|
| 76 | - if ( 0 === $depth ) { |
|
| 75 | + if ($related !== null) { |
|
| 76 | + if (0 === $depth) { |
|
| 77 | 77 | return $related; |
| 78 | 78 | } |
| 79 | 79 | } |
| 80 | 80 | |
| 81 | - wl_write_log( "wl_shortcode_chord_get_relations [ post id :: $entity_id ][ depth :: $depth ][ related? :: " . ( $related === null ? 'yes' : 'no' ) . ' ]' ); |
|
| 81 | + wl_write_log("wl_shortcode_chord_get_relations [ post id :: $entity_id ][ depth :: $depth ][ related? :: ".($related === null ? 'yes' : 'no').' ]'); |
|
| 82 | 82 | |
| 83 | 83 | // Create a related array which will hold entities and relations. |
| 84 | - if ( $related === null ) { |
|
| 84 | + if ($related === null) { |
|
| 85 | 85 | $related = array( |
| 86 | - 'entities' => array( $entity_id ), |
|
| 86 | + 'entities' => array($entity_id), |
|
| 87 | 87 | 'relations' => array(), |
| 88 | 88 | ); |
| 89 | 89 | } |
@@ -97,36 +97,35 @@ discard block |
||
| 97 | 97 | ); |
| 98 | 98 | |
| 99 | 99 | // If the current node is an entity, add related posts too |
| 100 | - $related_post_ids = ( Wordlift_Entity_Service::get_instance() |
|
| 101 | - ->is_entity( $entity_id ) ) ? |
|
| 100 | + $related_post_ids = (Wordlift_Entity_Service::get_instance() |
|
| 101 | + ->is_entity($entity_id)) ? |
|
| 102 | 102 | wl_core_get_related_post_ids( |
| 103 | 103 | $entity_id, |
| 104 | 104 | array( |
| 105 | 105 | 'status' => 'publish', |
| 106 | 106 | ) |
| 107 | - ) : |
|
| 108 | - array(); |
|
| 107 | + ) : array(); |
|
| 109 | 108 | |
| 110 | 109 | // Merge results and remove duplicated entries |
| 111 | - $related_ids = array_unique( array_merge( $related_post_ids, $related_entity_ids ) ); |
|
| 110 | + $related_ids = array_unique(array_merge($related_post_ids, $related_entity_ids)); |
|
| 112 | 111 | |
| 113 | 112 | // TODO: List of entities ($rel) should be ordered by interest factors. |
| 114 | - shuffle( $related_ids ); |
|
| 113 | + shuffle($related_ids); |
|
| 115 | 114 | |
| 116 | 115 | // Now we have all the related IDs. |
| 117 | - foreach ( $related_ids as $related_id ) { |
|
| 116 | + foreach ($related_ids as $related_id) { |
|
| 118 | 117 | |
| 119 | - if ( count( $related['entities'] ) >= $max_size ) { |
|
| 118 | + if (count($related['entities']) >= $max_size) { |
|
| 120 | 119 | return $related; |
| 121 | 120 | } |
| 122 | 121 | |
| 123 | - $related['relations'][] = array( $entity_id, $related_id ); |
|
| 122 | + $related['relations'][] = array($entity_id, $related_id); |
|
| 124 | 123 | |
| 125 | - if ( ! in_array( $related_id, $related['entities'], true ) ) { |
|
| 124 | + if ( ! in_array($related_id, $related['entities'], true)) { |
|
| 126 | 125 | // Found new related entity! |
| 127 | 126 | $related['entities'][] = $related_id; |
| 128 | 127 | |
| 129 | - $related = wl_shortcode_chord_get_relations( $related_id, ( $depth - 1 ), $related, $max_size ); |
|
| 128 | + $related = wl_shortcode_chord_get_relations($related_id, ($depth - 1), $related, $max_size); |
|
| 130 | 129 | } |
| 131 | 130 | } |
| 132 | 131 | |
@@ -143,44 +142,44 @@ discard block |
||
| 143 | 142 | * |
| 144 | 143 | * @return mixed|string |
| 145 | 144 | */ |
| 146 | -function wl_shortcode_chord_get_graph( $data ) { |
|
| 145 | +function wl_shortcode_chord_get_graph($data) { |
|
| 147 | 146 | |
| 148 | 147 | // Refactor the entities array in order to provide entities relevant data (uri, url, label, type, css_class). |
| 149 | 148 | array_walk( |
| 150 | 149 | $data['entities'], |
| 151 | - function ( &$item ) { |
|
| 152 | - $post = get_post( $item ); |
|
| 150 | + function(&$item) { |
|
| 151 | + $post = get_post($item); |
|
| 153 | 152 | |
| 154 | 153 | // Skip non-existing posts. |
| 155 | - if ( $post === null ) { |
|
| 156 | - wl_write_log( "wl_shortcode_chord_get_graph : post not found [ post id :: $item ]" ); |
|
| 154 | + if ($post === null) { |
|
| 155 | + wl_write_log("wl_shortcode_chord_get_graph : post not found [ post id :: $item ]"); |
|
| 157 | 156 | |
| 158 | 157 | return $item; |
| 159 | 158 | } |
| 160 | 159 | |
| 161 | 160 | // Get the entity taxonomy bound to this post (if there's no taxonomy, no stylesheet will be set). |
| 162 | - $term = Wordlift_Entity_Type_Service::get_instance()->get( $item ); |
|
| 161 | + $term = Wordlift_Entity_Type_Service::get_instance()->get($item); |
|
| 163 | 162 | |
| 164 | 163 | // The following log may create a circular loop. |
| 165 | 164 | // wl_write_log( "wl_shortcode_chord_get_graph [ post id :: $post->ID ][ term :: " . var_export( $term, true ) . " ]" ); |
| 166 | 165 | |
| 167 | 166 | // TODO: get all images |
| 168 | 167 | $thumbnail = null; |
| 169 | - $thumbnail_id = get_post_thumbnail_id( $post->ID ); |
|
| 170 | - if ( '' !== $thumbnail_id && 0 !== $thumbnail_id ) { |
|
| 171 | - $attachment = wp_get_attachment_image_src( $thumbnail_id ); |
|
| 172 | - if ( false !== $attachment ) { |
|
| 173 | - $thumbnail = esc_attr( $attachment[0] ); |
|
| 168 | + $thumbnail_id = get_post_thumbnail_id($post->ID); |
|
| 169 | + if ('' !== $thumbnail_id && 0 !== $thumbnail_id) { |
|
| 170 | + $attachment = wp_get_attachment_image_src($thumbnail_id); |
|
| 171 | + if (false !== $attachment) { |
|
| 172 | + $thumbnail = esc_attr($attachment[0]); |
|
| 174 | 173 | } |
| 175 | 174 | } |
| 176 | 175 | |
| 177 | 176 | $entity = array( |
| 178 | - 'uri' => wl_get_entity_uri( $item ), |
|
| 179 | - 'url' => get_permalink( $item ), |
|
| 177 | + 'uri' => wl_get_entity_uri($item), |
|
| 178 | + 'url' => get_permalink($item), |
|
| 180 | 179 | 'label' => $post->post_title, |
| 181 | 180 | 'type' => $post->post_type, |
| 182 | - 'thumbnails' => array( $thumbnail ), |
|
| 183 | - 'css_class' => ( isset( $term['css_class'] ) ? $term['css_class'] : '' ), |
|
| 181 | + 'thumbnails' => array($thumbnail), |
|
| 182 | + 'css_class' => (isset($term['css_class']) ? $term['css_class'] : ''), |
|
| 184 | 183 | ); |
| 185 | 184 | |
| 186 | 185 | $item = $entity; |
@@ -190,10 +189,10 @@ discard block |
||
| 190 | 189 | // Refactor the relations. |
| 191 | 190 | array_walk( |
| 192 | 191 | $data['relations'], |
| 193 | - function ( &$item ) { |
|
| 192 | + function(&$item) { |
|
| 194 | 193 | $relation = array( |
| 195 | - 's' => wl_get_entity_uri( $item[0] ), |
|
| 196 | - 'o' => wl_get_entity_uri( $item[1] ), |
|
| 194 | + 's' => wl_get_entity_uri($item[0]), |
|
| 195 | + 'o' => wl_get_entity_uri($item[1]), |
|
| 197 | 196 | ); |
| 198 | 197 | |
| 199 | 198 | $item = $relation; |
@@ -212,16 +211,16 @@ discard block |
||
| 212 | 211 | */ |
| 213 | 212 | function wl_shortcode_chord_ajax() { |
| 214 | 213 | |
| 215 | - check_ajax_referer( 'wl_chord', 'wl_chord_nonce' ); |
|
| 214 | + check_ajax_referer('wl_chord', 'wl_chord_nonce'); |
|
| 216 | 215 | |
| 217 | - $post_id = isset( $_REQUEST['post_id'] ) ? (int) $_REQUEST['post_id'] : 0; |
|
| 218 | - $depth = isset( $_REQUEST['depth'] ) ? (int) $_REQUEST['depth'] : 2; |
|
| 216 | + $post_id = isset($_REQUEST['post_id']) ? (int) $_REQUEST['post_id'] : 0; |
|
| 217 | + $depth = isset($_REQUEST['depth']) ? (int) $_REQUEST['depth'] : 2; |
|
| 219 | 218 | |
| 220 | - $relations = wl_shortcode_chord_get_relations( $post_id, $depth ); |
|
| 221 | - $graph = wl_shortcode_chord_get_graph( $relations ); |
|
| 219 | + $relations = wl_shortcode_chord_get_relations($post_id, $depth); |
|
| 220 | + $graph = wl_shortcode_chord_get_graph($relations); |
|
| 222 | 221 | |
| 223 | - wl_core_send_json( $graph ); |
|
| 222 | + wl_core_send_json($graph); |
|
| 224 | 223 | } |
| 225 | 224 | |
| 226 | -add_action( 'wp_ajax_wl_chord', 'wl_shortcode_chord_ajax' ); |
|
| 227 | -add_action( 'wp_ajax_nopriv_wl_chord', 'wl_shortcode_chord_ajax' ); |
|
| 225 | +add_action('wp_ajax_wl_chord', 'wl_shortcode_chord_ajax'); |
|
| 226 | +add_action('wp_ajax_nopriv_wl_chord', 'wl_shortcode_chord_ajax'); |
|
@@ -10,48 +10,48 @@ |
||
| 10 | 10 | namespace Wordlift\Entity; |
| 11 | 11 | |
| 12 | 12 | class Entity_Rest_Service { |
| 13 | - /** |
|
| 14 | - * @var \Wordlift_Entity_Type_Service |
|
| 15 | - */ |
|
| 16 | - private $entity_type_service; |
|
| 13 | + /** |
|
| 14 | + * @var \Wordlift_Entity_Type_Service |
|
| 15 | + */ |
|
| 16 | + private $entity_type_service; |
|
| 17 | 17 | |
| 18 | - /** |
|
| 19 | - * Entity_Rest_Service constructor. |
|
| 20 | - * |
|
| 21 | - * @param $entity_type_service \Wordlift_Entity_Type_Service |
|
| 22 | - */ |
|
| 23 | - public function __construct( $entity_type_service ) { |
|
| 18 | + /** |
|
| 19 | + * Entity_Rest_Service constructor. |
|
| 20 | + * |
|
| 21 | + * @param $entity_type_service \Wordlift_Entity_Type_Service |
|
| 22 | + */ |
|
| 23 | + public function __construct( $entity_type_service ) { |
|
| 24 | 24 | |
| 25 | - $this->entity_type_service = $entity_type_service; |
|
| 25 | + $this->entity_type_service = $entity_type_service; |
|
| 26 | 26 | |
| 27 | - add_action( 'rest_insert_entity', array( $this, 'action_rest_insert_entity' ), 10, 3 ); |
|
| 27 | + add_action( 'rest_insert_entity', array( $this, 'action_rest_insert_entity' ), 10, 3 ); |
|
| 28 | 28 | |
| 29 | - } |
|
| 29 | + } |
|
| 30 | 30 | |
| 31 | - /** |
|
| 32 | - * @param $post \WP_Post |
|
| 33 | - * @param $request \WP_REST_Request |
|
| 34 | - * @param $creating bool |
|
| 35 | - */ |
|
| 36 | - public function action_rest_insert_entity( $post, $request, $creating ) { |
|
| 31 | + /** |
|
| 32 | + * @param $post \WP_Post |
|
| 33 | + * @param $request \WP_REST_Request |
|
| 34 | + * @param $creating bool |
|
| 35 | + */ |
|
| 36 | + public function action_rest_insert_entity( $post, $request, $creating ) { |
|
| 37 | 37 | |
| 38 | - // Set the type only on entity create. |
|
| 39 | - if ( ! $creating ) { |
|
| 40 | - return; |
|
| 41 | - } |
|
| 38 | + // Set the type only on entity create. |
|
| 39 | + if ( ! $creating ) { |
|
| 40 | + return; |
|
| 41 | + } |
|
| 42 | 42 | |
| 43 | - $entity_types = $request->get_param( 'wlEntityMainType' ); |
|
| 43 | + $entity_types = $request->get_param( 'wlEntityMainType' ); |
|
| 44 | 44 | |
| 45 | - if ( null === $entity_types ) { |
|
| 46 | - // Return early if entity types not set. |
|
| 47 | - return; |
|
| 48 | - } |
|
| 45 | + if ( null === $entity_types ) { |
|
| 46 | + // Return early if entity types not set. |
|
| 47 | + return; |
|
| 48 | + } |
|
| 49 | 49 | |
| 50 | - foreach ( $entity_types as $type_uri ) { |
|
| 51 | - // we don't replace since its entity creation and only one entity will be present. |
|
| 52 | - $this->entity_type_service->set( $post->ID, $type_uri ); |
|
| 53 | - } |
|
| 50 | + foreach ( $entity_types as $type_uri ) { |
|
| 51 | + // we don't replace since its entity creation and only one entity will be present. |
|
| 52 | + $this->entity_type_service->set( $post->ID, $type_uri ); |
|
| 53 | + } |
|
| 54 | 54 | |
| 55 | - } |
|
| 55 | + } |
|
| 56 | 56 | |
| 57 | 57 | } |
@@ -20,11 +20,11 @@ discard block |
||
| 20 | 20 | * |
| 21 | 21 | * @param $entity_type_service \Wordlift_Entity_Type_Service |
| 22 | 22 | */ |
| 23 | - public function __construct( $entity_type_service ) { |
|
| 23 | + public function __construct($entity_type_service) { |
|
| 24 | 24 | |
| 25 | 25 | $this->entity_type_service = $entity_type_service; |
| 26 | 26 | |
| 27 | - add_action( 'rest_insert_entity', array( $this, 'action_rest_insert_entity' ), 10, 3 ); |
|
| 27 | + add_action('rest_insert_entity', array($this, 'action_rest_insert_entity'), 10, 3); |
|
| 28 | 28 | |
| 29 | 29 | } |
| 30 | 30 | |
@@ -33,23 +33,23 @@ discard block |
||
| 33 | 33 | * @param $request \WP_REST_Request |
| 34 | 34 | * @param $creating bool |
| 35 | 35 | */ |
| 36 | - public function action_rest_insert_entity( $post, $request, $creating ) { |
|
| 36 | + public function action_rest_insert_entity($post, $request, $creating) { |
|
| 37 | 37 | |
| 38 | 38 | // Set the type only on entity create. |
| 39 | - if ( ! $creating ) { |
|
| 39 | + if ( ! $creating) { |
|
| 40 | 40 | return; |
| 41 | 41 | } |
| 42 | 42 | |
| 43 | - $entity_types = $request->get_param( 'wlEntityMainType' ); |
|
| 43 | + $entity_types = $request->get_param('wlEntityMainType'); |
|
| 44 | 44 | |
| 45 | - if ( null === $entity_types ) { |
|
| 45 | + if (null === $entity_types) { |
|
| 46 | 46 | // Return early if entity types not set. |
| 47 | 47 | return; |
| 48 | 48 | } |
| 49 | 49 | |
| 50 | - foreach ( $entity_types as $type_uri ) { |
|
| 50 | + foreach ($entity_types as $type_uri) { |
|
| 51 | 51 | // we don't replace since its entity creation and only one entity will be present. |
| 52 | - $this->entity_type_service->set( $post->ID, $type_uri ); |
|
| 52 | + $this->entity_type_service->set($post->ID, $type_uri); |
|
| 53 | 53 | } |
| 54 | 54 | |
| 55 | 55 | } |
@@ -8,46 +8,46 @@ |
||
| 8 | 8 | */ |
| 9 | 9 | class Entity_No_Index_Flag { |
| 10 | 10 | |
| 11 | - const YOAST_POST_NO_INDEX_FLAG = '_yoast_wpseo_meta-robots-noindex'; |
|
| 12 | - |
|
| 13 | - public function __construct() { |
|
| 14 | - |
|
| 15 | - $no_index_flag = self::YOAST_POST_NO_INDEX_FLAG; |
|
| 16 | - |
|
| 17 | - add_action( |
|
| 18 | - 'wp_insert_post', |
|
| 19 | - function ( $post_id, $post, $update ) use ( $no_index_flag ) { |
|
| 20 | - |
|
| 21 | - $post_type = get_post_type( $post_id ); |
|
| 22 | - |
|
| 23 | - if ( \Wordlift_Entity_Service::TYPE_NAME !== $post_type ) { |
|
| 24 | - // Don't set this flag for any other post types. |
|
| 25 | - return; |
|
| 26 | - } |
|
| 27 | - |
|
| 28 | - // We need to set this flag only on entity creation. |
|
| 29 | - if ( ! $update ) { |
|
| 30 | - update_post_meta( $post_id, $no_index_flag, 1 ); |
|
| 31 | - } |
|
| 32 | - |
|
| 33 | - }, |
|
| 34 | - PHP_INT_MAX, |
|
| 35 | - 3 |
|
| 36 | - ); |
|
| 37 | - |
|
| 38 | - add_action( |
|
| 39 | - 'post_updated', |
|
| 40 | - function ( $post_id ) use ( $no_index_flag ) { |
|
| 41 | - if ( get_post_type( $post_id ) !== \Wordlift_Entity_Service::TYPE_NAME ) { |
|
| 42 | - return; |
|
| 43 | - } |
|
| 44 | - // if the post is updated, remove this flag |
|
| 45 | - delete_post_meta( $post_id, $no_index_flag ); |
|
| 46 | - }, |
|
| 47 | - PHP_INT_MAX |
|
| 48 | - ); |
|
| 49 | - |
|
| 50 | - } |
|
| 11 | + const YOAST_POST_NO_INDEX_FLAG = '_yoast_wpseo_meta-robots-noindex'; |
|
| 12 | + |
|
| 13 | + public function __construct() { |
|
| 14 | + |
|
| 15 | + $no_index_flag = self::YOAST_POST_NO_INDEX_FLAG; |
|
| 16 | + |
|
| 17 | + add_action( |
|
| 18 | + 'wp_insert_post', |
|
| 19 | + function ( $post_id, $post, $update ) use ( $no_index_flag ) { |
|
| 20 | + |
|
| 21 | + $post_type = get_post_type( $post_id ); |
|
| 22 | + |
|
| 23 | + if ( \Wordlift_Entity_Service::TYPE_NAME !== $post_type ) { |
|
| 24 | + // Don't set this flag for any other post types. |
|
| 25 | + return; |
|
| 26 | + } |
|
| 27 | + |
|
| 28 | + // We need to set this flag only on entity creation. |
|
| 29 | + if ( ! $update ) { |
|
| 30 | + update_post_meta( $post_id, $no_index_flag, 1 ); |
|
| 31 | + } |
|
| 32 | + |
|
| 33 | + }, |
|
| 34 | + PHP_INT_MAX, |
|
| 35 | + 3 |
|
| 36 | + ); |
|
| 37 | + |
|
| 38 | + add_action( |
|
| 39 | + 'post_updated', |
|
| 40 | + function ( $post_id ) use ( $no_index_flag ) { |
|
| 41 | + if ( get_post_type( $post_id ) !== \Wordlift_Entity_Service::TYPE_NAME ) { |
|
| 42 | + return; |
|
| 43 | + } |
|
| 44 | + // if the post is updated, remove this flag |
|
| 45 | + delete_post_meta( $post_id, $no_index_flag ); |
|
| 46 | + }, |
|
| 47 | + PHP_INT_MAX |
|
| 48 | + ); |
|
| 49 | + |
|
| 50 | + } |
|
| 51 | 51 | |
| 52 | 52 | } |
| 53 | 53 | |
@@ -16,18 +16,18 @@ discard block |
||
| 16 | 16 | |
| 17 | 17 | add_action( |
| 18 | 18 | 'wp_insert_post', |
| 19 | - function ( $post_id, $post, $update ) use ( $no_index_flag ) { |
|
| 19 | + function($post_id, $post, $update) use ($no_index_flag) { |
|
| 20 | 20 | |
| 21 | - $post_type = get_post_type( $post_id ); |
|
| 21 | + $post_type = get_post_type($post_id); |
|
| 22 | 22 | |
| 23 | - if ( \Wordlift_Entity_Service::TYPE_NAME !== $post_type ) { |
|
| 23 | + if (\Wordlift_Entity_Service::TYPE_NAME !== $post_type) { |
|
| 24 | 24 | // Don't set this flag for any other post types. |
| 25 | 25 | return; |
| 26 | 26 | } |
| 27 | 27 | |
| 28 | 28 | // We need to set this flag only on entity creation. |
| 29 | - if ( ! $update ) { |
|
| 30 | - update_post_meta( $post_id, $no_index_flag, 1 ); |
|
| 29 | + if ( ! $update) { |
|
| 30 | + update_post_meta($post_id, $no_index_flag, 1); |
|
| 31 | 31 | } |
| 32 | 32 | |
| 33 | 33 | }, |
@@ -37,12 +37,12 @@ discard block |
||
| 37 | 37 | |
| 38 | 38 | add_action( |
| 39 | 39 | 'post_updated', |
| 40 | - function ( $post_id ) use ( $no_index_flag ) { |
|
| 41 | - if ( get_post_type( $post_id ) !== \Wordlift_Entity_Service::TYPE_NAME ) { |
|
| 40 | + function($post_id) use ($no_index_flag) { |
|
| 41 | + if (get_post_type($post_id) !== \Wordlift_Entity_Service::TYPE_NAME) { |
|
| 42 | 42 | return; |
| 43 | 43 | } |
| 44 | 44 | // if the post is updated, remove this flag |
| 45 | - delete_post_meta( $post_id, $no_index_flag ); |
|
| 45 | + delete_post_meta($post_id, $no_index_flag); |
|
| 46 | 46 | }, |
| 47 | 47 | PHP_INT_MAX |
| 48 | 48 | ); |
@@ -32,142 +32,142 @@ |
||
| 32 | 32 | */ |
| 33 | 33 | class Entity_Store { |
| 34 | 34 | |
| 35 | - protected function __construct() { |
|
| 36 | - } |
|
| 37 | - |
|
| 38 | - private static $instance = null; |
|
| 39 | - |
|
| 40 | - /** |
|
| 41 | - * Get the Entity_Store singleton, lazily initialized. |
|
| 42 | - * |
|
| 43 | - * @return Entity_Store The singleton. |
|
| 44 | - */ |
|
| 45 | - public static function get_instance() { |
|
| 46 | - if ( ! isset( self::$instance ) ) { |
|
| 47 | - self::$instance = new Entity_Store(); |
|
| 48 | - } |
|
| 49 | - |
|
| 50 | - return self::$instance; |
|
| 51 | - } |
|
| 52 | - |
|
| 53 | - /** |
|
| 54 | - * Create and persist an entity. |
|
| 55 | - * |
|
| 56 | - * @param array $params { |
|
| 57 | - * The entity parameters. |
|
| 58 | - * |
|
| 59 | - * @type string|array $labels A label, or an array of labels. The first label is set as post title. |
|
| 60 | - * @type string $description The entity description, stored in the post content. |
|
| 61 | - * @type string|array $same_as One or more entity URIs, stored in the sameAs post meta. |
|
| 62 | - * } |
|
| 63 | - * |
|
| 64 | - * @param string $post_status The post status, by default `draft`. |
|
| 65 | - * |
|
| 66 | - * @return int|\WP_Error |
|
| 67 | - * @throws \Exception when an error occurs. |
|
| 68 | - */ |
|
| 69 | - public function create( $params, $post_status = 'draft' ) { |
|
| 70 | - |
|
| 71 | - $args = wp_parse_args( |
|
| 72 | - $params, |
|
| 73 | - array( |
|
| 74 | - 'labels' => array(), |
|
| 75 | - 'description' => '', |
|
| 76 | - 'same_as' => array(), |
|
| 77 | - ) |
|
| 78 | - ); |
|
| 79 | - |
|
| 80 | - // Use the first label as `post_title`. |
|
| 81 | - $labels = (array) $args['labels']; |
|
| 82 | - $label = array_shift( $labels ); |
|
| 83 | - |
|
| 84 | - $post_id = wp_insert_post( |
|
| 85 | - array( |
|
| 86 | - 'post_type' => Wordlift_Entity_Service::TYPE_NAME, |
|
| 87 | - 'post_status' => $post_status, |
|
| 88 | - 'post_title' => $label, |
|
| 89 | - 'post_name' => sanitize_title( $label ), |
|
| 90 | - 'post_content' => $args['description'], |
|
| 91 | - ) |
|
| 92 | - ); |
|
| 93 | - |
|
| 94 | - // Bail out if we've got an error. |
|
| 95 | - if ( empty( $post_id ) || is_wp_error( $post_id ) ) { |
|
| 96 | - throw new \Exception( 'An error occurred while creating an entity.' ); |
|
| 97 | - } |
|
| 98 | - |
|
| 99 | - Wordlift_Entity_Service::get_instance() |
|
| 100 | - ->set_alternative_labels( $post_id, array_diff( $labels, array( $label ) ) ); |
|
| 101 | - $this->merge_post_meta( |
|
| 102 | - $post_id, |
|
| 103 | - \Wordlift_Schema_Service::FIELD_SAME_AS, |
|
| 104 | - (array) $args['same_as'], |
|
| 105 | - (array) Wordpress_Content_Service::get_instance()->get_entity_id( Wordpress_Content_Id::create_post( $post_id ) ) |
|
| 106 | - ); |
|
| 107 | - |
|
| 108 | - return $post_id; |
|
| 109 | - } |
|
| 110 | - |
|
| 111 | - /** |
|
| 112 | - * Update an entity. |
|
| 113 | - * |
|
| 114 | - * @param array $params { |
|
| 115 | - * |
|
| 116 | - * @type int $ID The post ID. |
|
| 117 | - * @type string|array One or more labels to add to the synonyms. |
|
| 118 | - * @type string|array One or more URIs to add to the sameAs. |
|
| 119 | - * } |
|
| 120 | - * |
|
| 121 | - * @return int The post id. |
|
| 122 | - */ |
|
| 123 | - public function update( $params ) { |
|
| 124 | - |
|
| 125 | - $args = wp_parse_args( |
|
| 126 | - $params, |
|
| 127 | - array( |
|
| 128 | - 'ID' => 0, |
|
| 129 | - 'labels' => array(), |
|
| 130 | - 'same_as' => array(), |
|
| 131 | - ) |
|
| 132 | - ); |
|
| 133 | - |
|
| 134 | - $post_id = $args['ID']; |
|
| 135 | - |
|
| 136 | - // Save synonyms except title. |
|
| 137 | - Wordlift_Entity_Service::get_instance() |
|
| 138 | - ->append_alternative_labels( $post_id, array_diff( (array) $args['labels'], array( get_the_title( $post_id ) ) ) ); |
|
| 139 | - |
|
| 140 | - $this->merge_post_meta( |
|
| 141 | - $post_id, |
|
| 142 | - \Wordlift_Schema_Service::FIELD_SAME_AS, |
|
| 143 | - (array) $args['same_as'], |
|
| 144 | - (array) Wordpress_Content_Service::get_instance()->get_entity_id( Wordpress_Content_Id::create_post( $post_id ) ) |
|
| 145 | - ); |
|
| 146 | - |
|
| 147 | - return $post_id; |
|
| 148 | - } |
|
| 149 | - |
|
| 150 | - /** |
|
| 151 | - * Merge the post meta. |
|
| 152 | - * |
|
| 153 | - * @param int $post_id The post ID. |
|
| 154 | - * @param string $meta_key The post meta key. |
|
| 155 | - * @param string|array $values One or more values to add. |
|
| 156 | - * @param string|array $exclusions An additional list of values to exclude. |
|
| 157 | - */ |
|
| 158 | - private function merge_post_meta( $post_id, $meta_key, $values, $exclusions = array() ) { |
|
| 159 | - |
|
| 160 | - $existing = array_merge( |
|
| 161 | - (array) $exclusions, |
|
| 162 | - get_post_meta( $post_id, $meta_key ) |
|
| 163 | - ); |
|
| 164 | - |
|
| 165 | - foreach ( (array) $values as $value ) { |
|
| 166 | - // phpcs:ignore WordPress.PHP.StrictInArray.MissingTrueStrict |
|
| 167 | - if ( ! in_array( $value, $existing ) ) { |
|
| 168 | - add_post_meta( $post_id, $meta_key, $value ); |
|
| 169 | - } |
|
| 170 | - } |
|
| 171 | - } |
|
| 35 | + protected function __construct() { |
|
| 36 | + } |
|
| 37 | + |
|
| 38 | + private static $instance = null; |
|
| 39 | + |
|
| 40 | + /** |
|
| 41 | + * Get the Entity_Store singleton, lazily initialized. |
|
| 42 | + * |
|
| 43 | + * @return Entity_Store The singleton. |
|
| 44 | + */ |
|
| 45 | + public static function get_instance() { |
|
| 46 | + if ( ! isset( self::$instance ) ) { |
|
| 47 | + self::$instance = new Entity_Store(); |
|
| 48 | + } |
|
| 49 | + |
|
| 50 | + return self::$instance; |
|
| 51 | + } |
|
| 52 | + |
|
| 53 | + /** |
|
| 54 | + * Create and persist an entity. |
|
| 55 | + * |
|
| 56 | + * @param array $params { |
|
| 57 | + * The entity parameters. |
|
| 58 | + * |
|
| 59 | + * @type string|array $labels A label, or an array of labels. The first label is set as post title. |
|
| 60 | + * @type string $description The entity description, stored in the post content. |
|
| 61 | + * @type string|array $same_as One or more entity URIs, stored in the sameAs post meta. |
|
| 62 | + * } |
|
| 63 | + * |
|
| 64 | + * @param string $post_status The post status, by default `draft`. |
|
| 65 | + * |
|
| 66 | + * @return int|\WP_Error |
|
| 67 | + * @throws \Exception when an error occurs. |
|
| 68 | + */ |
|
| 69 | + public function create( $params, $post_status = 'draft' ) { |
|
| 70 | + |
|
| 71 | + $args = wp_parse_args( |
|
| 72 | + $params, |
|
| 73 | + array( |
|
| 74 | + 'labels' => array(), |
|
| 75 | + 'description' => '', |
|
| 76 | + 'same_as' => array(), |
|
| 77 | + ) |
|
| 78 | + ); |
|
| 79 | + |
|
| 80 | + // Use the first label as `post_title`. |
|
| 81 | + $labels = (array) $args['labels']; |
|
| 82 | + $label = array_shift( $labels ); |
|
| 83 | + |
|
| 84 | + $post_id = wp_insert_post( |
|
| 85 | + array( |
|
| 86 | + 'post_type' => Wordlift_Entity_Service::TYPE_NAME, |
|
| 87 | + 'post_status' => $post_status, |
|
| 88 | + 'post_title' => $label, |
|
| 89 | + 'post_name' => sanitize_title( $label ), |
|
| 90 | + 'post_content' => $args['description'], |
|
| 91 | + ) |
|
| 92 | + ); |
|
| 93 | + |
|
| 94 | + // Bail out if we've got an error. |
|
| 95 | + if ( empty( $post_id ) || is_wp_error( $post_id ) ) { |
|
| 96 | + throw new \Exception( 'An error occurred while creating an entity.' ); |
|
| 97 | + } |
|
| 98 | + |
|
| 99 | + Wordlift_Entity_Service::get_instance() |
|
| 100 | + ->set_alternative_labels( $post_id, array_diff( $labels, array( $label ) ) ); |
|
| 101 | + $this->merge_post_meta( |
|
| 102 | + $post_id, |
|
| 103 | + \Wordlift_Schema_Service::FIELD_SAME_AS, |
|
| 104 | + (array) $args['same_as'], |
|
| 105 | + (array) Wordpress_Content_Service::get_instance()->get_entity_id( Wordpress_Content_Id::create_post( $post_id ) ) |
|
| 106 | + ); |
|
| 107 | + |
|
| 108 | + return $post_id; |
|
| 109 | + } |
|
| 110 | + |
|
| 111 | + /** |
|
| 112 | + * Update an entity. |
|
| 113 | + * |
|
| 114 | + * @param array $params { |
|
| 115 | + * |
|
| 116 | + * @type int $ID The post ID. |
|
| 117 | + * @type string|array One or more labels to add to the synonyms. |
|
| 118 | + * @type string|array One or more URIs to add to the sameAs. |
|
| 119 | + * } |
|
| 120 | + * |
|
| 121 | + * @return int The post id. |
|
| 122 | + */ |
|
| 123 | + public function update( $params ) { |
|
| 124 | + |
|
| 125 | + $args = wp_parse_args( |
|
| 126 | + $params, |
|
| 127 | + array( |
|
| 128 | + 'ID' => 0, |
|
| 129 | + 'labels' => array(), |
|
| 130 | + 'same_as' => array(), |
|
| 131 | + ) |
|
| 132 | + ); |
|
| 133 | + |
|
| 134 | + $post_id = $args['ID']; |
|
| 135 | + |
|
| 136 | + // Save synonyms except title. |
|
| 137 | + Wordlift_Entity_Service::get_instance() |
|
| 138 | + ->append_alternative_labels( $post_id, array_diff( (array) $args['labels'], array( get_the_title( $post_id ) ) ) ); |
|
| 139 | + |
|
| 140 | + $this->merge_post_meta( |
|
| 141 | + $post_id, |
|
| 142 | + \Wordlift_Schema_Service::FIELD_SAME_AS, |
|
| 143 | + (array) $args['same_as'], |
|
| 144 | + (array) Wordpress_Content_Service::get_instance()->get_entity_id( Wordpress_Content_Id::create_post( $post_id ) ) |
|
| 145 | + ); |
|
| 146 | + |
|
| 147 | + return $post_id; |
|
| 148 | + } |
|
| 149 | + |
|
| 150 | + /** |
|
| 151 | + * Merge the post meta. |
|
| 152 | + * |
|
| 153 | + * @param int $post_id The post ID. |
|
| 154 | + * @param string $meta_key The post meta key. |
|
| 155 | + * @param string|array $values One or more values to add. |
|
| 156 | + * @param string|array $exclusions An additional list of values to exclude. |
|
| 157 | + */ |
|
| 158 | + private function merge_post_meta( $post_id, $meta_key, $values, $exclusions = array() ) { |
|
| 159 | + |
|
| 160 | + $existing = array_merge( |
|
| 161 | + (array) $exclusions, |
|
| 162 | + get_post_meta( $post_id, $meta_key ) |
|
| 163 | + ); |
|
| 164 | + |
|
| 165 | + foreach ( (array) $values as $value ) { |
|
| 166 | + // phpcs:ignore WordPress.PHP.StrictInArray.MissingTrueStrict |
|
| 167 | + if ( ! in_array( $value, $existing ) ) { |
|
| 168 | + add_post_meta( $post_id, $meta_key, $value ); |
|
| 169 | + } |
|
| 170 | + } |
|
| 171 | + } |
|
| 172 | 172 | |
| 173 | 173 | } |
@@ -43,7 +43,7 @@ discard block |
||
| 43 | 43 | * @return Entity_Store The singleton. |
| 44 | 44 | */ |
| 45 | 45 | public static function get_instance() { |
| 46 | - if ( ! isset( self::$instance ) ) { |
|
| 46 | + if ( ! isset(self::$instance)) { |
|
| 47 | 47 | self::$instance = new Entity_Store(); |
| 48 | 48 | } |
| 49 | 49 | |
@@ -66,7 +66,7 @@ discard block |
||
| 66 | 66 | * @return int|\WP_Error |
| 67 | 67 | * @throws \Exception when an error occurs. |
| 68 | 68 | */ |
| 69 | - public function create( $params, $post_status = 'draft' ) { |
|
| 69 | + public function create($params, $post_status = 'draft') { |
|
| 70 | 70 | |
| 71 | 71 | $args = wp_parse_args( |
| 72 | 72 | $params, |
@@ -79,30 +79,30 @@ discard block |
||
| 79 | 79 | |
| 80 | 80 | // Use the first label as `post_title`. |
| 81 | 81 | $labels = (array) $args['labels']; |
| 82 | - $label = array_shift( $labels ); |
|
| 82 | + $label = array_shift($labels); |
|
| 83 | 83 | |
| 84 | 84 | $post_id = wp_insert_post( |
| 85 | 85 | array( |
| 86 | 86 | 'post_type' => Wordlift_Entity_Service::TYPE_NAME, |
| 87 | 87 | 'post_status' => $post_status, |
| 88 | 88 | 'post_title' => $label, |
| 89 | - 'post_name' => sanitize_title( $label ), |
|
| 89 | + 'post_name' => sanitize_title($label), |
|
| 90 | 90 | 'post_content' => $args['description'], |
| 91 | 91 | ) |
| 92 | 92 | ); |
| 93 | 93 | |
| 94 | 94 | // Bail out if we've got an error. |
| 95 | - if ( empty( $post_id ) || is_wp_error( $post_id ) ) { |
|
| 96 | - throw new \Exception( 'An error occurred while creating an entity.' ); |
|
| 95 | + if (empty($post_id) || is_wp_error($post_id)) { |
|
| 96 | + throw new \Exception('An error occurred while creating an entity.'); |
|
| 97 | 97 | } |
| 98 | 98 | |
| 99 | 99 | Wordlift_Entity_Service::get_instance() |
| 100 | - ->set_alternative_labels( $post_id, array_diff( $labels, array( $label ) ) ); |
|
| 100 | + ->set_alternative_labels($post_id, array_diff($labels, array($label))); |
|
| 101 | 101 | $this->merge_post_meta( |
| 102 | 102 | $post_id, |
| 103 | 103 | \Wordlift_Schema_Service::FIELD_SAME_AS, |
| 104 | 104 | (array) $args['same_as'], |
| 105 | - (array) Wordpress_Content_Service::get_instance()->get_entity_id( Wordpress_Content_Id::create_post( $post_id ) ) |
|
| 105 | + (array) Wordpress_Content_Service::get_instance()->get_entity_id(Wordpress_Content_Id::create_post($post_id)) |
|
| 106 | 106 | ); |
| 107 | 107 | |
| 108 | 108 | return $post_id; |
@@ -120,7 +120,7 @@ discard block |
||
| 120 | 120 | * |
| 121 | 121 | * @return int The post id. |
| 122 | 122 | */ |
| 123 | - public function update( $params ) { |
|
| 123 | + public function update($params) { |
|
| 124 | 124 | |
| 125 | 125 | $args = wp_parse_args( |
| 126 | 126 | $params, |
@@ -135,13 +135,13 @@ discard block |
||
| 135 | 135 | |
| 136 | 136 | // Save synonyms except title. |
| 137 | 137 | Wordlift_Entity_Service::get_instance() |
| 138 | - ->append_alternative_labels( $post_id, array_diff( (array) $args['labels'], array( get_the_title( $post_id ) ) ) ); |
|
| 138 | + ->append_alternative_labels($post_id, array_diff((array) $args['labels'], array(get_the_title($post_id)))); |
|
| 139 | 139 | |
| 140 | 140 | $this->merge_post_meta( |
| 141 | 141 | $post_id, |
| 142 | 142 | \Wordlift_Schema_Service::FIELD_SAME_AS, |
| 143 | 143 | (array) $args['same_as'], |
| 144 | - (array) Wordpress_Content_Service::get_instance()->get_entity_id( Wordpress_Content_Id::create_post( $post_id ) ) |
|
| 144 | + (array) Wordpress_Content_Service::get_instance()->get_entity_id(Wordpress_Content_Id::create_post($post_id)) |
|
| 145 | 145 | ); |
| 146 | 146 | |
| 147 | 147 | return $post_id; |
@@ -155,17 +155,17 @@ discard block |
||
| 155 | 155 | * @param string|array $values One or more values to add. |
| 156 | 156 | * @param string|array $exclusions An additional list of values to exclude. |
| 157 | 157 | */ |
| 158 | - private function merge_post_meta( $post_id, $meta_key, $values, $exclusions = array() ) { |
|
| 158 | + private function merge_post_meta($post_id, $meta_key, $values, $exclusions = array()) { |
|
| 159 | 159 | |
| 160 | 160 | $existing = array_merge( |
| 161 | 161 | (array) $exclusions, |
| 162 | - get_post_meta( $post_id, $meta_key ) |
|
| 162 | + get_post_meta($post_id, $meta_key) |
|
| 163 | 163 | ); |
| 164 | 164 | |
| 165 | - foreach ( (array) $values as $value ) { |
|
| 165 | + foreach ((array) $values as $value) { |
|
| 166 | 166 | // phpcs:ignore WordPress.PHP.StrictInArray.MissingTrueStrict |
| 167 | - if ( ! in_array( $value, $existing ) ) { |
|
| 168 | - add_post_meta( $post_id, $meta_key, $value ); |
|
| 167 | + if ( ! in_array($value, $existing)) { |
|
| 168 | + add_post_meta($post_id, $meta_key, $value); |
|
| 169 | 169 | } |
| 170 | 170 | } |
| 171 | 171 | } |
@@ -6,10 +6,10 @@ |
||
| 6 | 6 | |
| 7 | 7 | interface Remote_Entity_Importer { |
| 8 | 8 | |
| 9 | - /** |
|
| 10 | - * @return Wordpress_Content_Id|boolean |
|
| 11 | - * Returns content id or false. |
|
| 12 | - */ |
|
| 13 | - public function import(); |
|
| 9 | + /** |
|
| 10 | + * @return Wordpress_Content_Id|boolean |
|
| 11 | + * Returns content id or false. |
|
| 12 | + */ |
|
| 13 | + public function import(); |
|
| 14 | 14 | |
| 15 | 15 | } |
@@ -7,9 +7,9 @@ |
||
| 7 | 7 | */ |
| 8 | 8 | class Invalid_Remote_Entity_Importer implements Remote_Entity_Importer { |
| 9 | 9 | |
| 10 | - public function import() { |
|
| 11 | - // Cant import invalid entities. |
|
| 12 | - return false; |
|
| 13 | - } |
|
| 10 | + public function import() { |
|
| 11 | + // Cant import invalid entities. |
|
| 12 | + return false; |
|
| 13 | + } |
|
| 14 | 14 | |
| 15 | 15 | } |
@@ -8,39 +8,39 @@ |
||
| 8 | 8 | |
| 9 | 9 | class Valid_Remote_Entity_Importer implements Remote_Entity_Importer { |
| 10 | 10 | |
| 11 | - /** |
|
| 12 | - * @var Valid_Remote_Entity |
|
| 13 | - */ |
|
| 14 | - private $entity; |
|
| 15 | - |
|
| 16 | - /** |
|
| 17 | - * @param $entity Valid_Remote_Entity |
|
| 18 | - */ |
|
| 19 | - public function __construct( $entity ) { |
|
| 20 | - $this->entity = $entity; |
|
| 21 | - } |
|
| 22 | - |
|
| 23 | - public function import() { |
|
| 24 | - |
|
| 25 | - $entity_type_service = Wordlift_Entity_Type_Service::get_instance(); |
|
| 26 | - |
|
| 27 | - $post_id = wp_insert_post( |
|
| 28 | - array( |
|
| 29 | - 'post_title' => $this->entity->get_name(), |
|
| 30 | - 'post_content' => $this->entity->get_description(), |
|
| 31 | - 'post_status' => 'draft', |
|
| 32 | - 'post_type' => 'entity', |
|
| 33 | - ) |
|
| 34 | - ); |
|
| 35 | - |
|
| 36 | - foreach ( $this->entity->get_types() as $type ) { |
|
| 37 | - $entity_type_service->set( $post_id, "http://schema.org/$type", false ); |
|
| 38 | - } |
|
| 39 | - |
|
| 40 | - foreach ( $this->entity->get_same_as() as $same_as ) { |
|
| 41 | - add_post_meta( $post_id, 'entity_same_as', $same_as ); |
|
| 42 | - } |
|
| 43 | - |
|
| 44 | - return Wordpress_Content_Id::create_post( $post_id ); |
|
| 45 | - } |
|
| 11 | + /** |
|
| 12 | + * @var Valid_Remote_Entity |
|
| 13 | + */ |
|
| 14 | + private $entity; |
|
| 15 | + |
|
| 16 | + /** |
|
| 17 | + * @param $entity Valid_Remote_Entity |
|
| 18 | + */ |
|
| 19 | + public function __construct( $entity ) { |
|
| 20 | + $this->entity = $entity; |
|
| 21 | + } |
|
| 22 | + |
|
| 23 | + public function import() { |
|
| 24 | + |
|
| 25 | + $entity_type_service = Wordlift_Entity_Type_Service::get_instance(); |
|
| 26 | + |
|
| 27 | + $post_id = wp_insert_post( |
|
| 28 | + array( |
|
| 29 | + 'post_title' => $this->entity->get_name(), |
|
| 30 | + 'post_content' => $this->entity->get_description(), |
|
| 31 | + 'post_status' => 'draft', |
|
| 32 | + 'post_type' => 'entity', |
|
| 33 | + ) |
|
| 34 | + ); |
|
| 35 | + |
|
| 36 | + foreach ( $this->entity->get_types() as $type ) { |
|
| 37 | + $entity_type_service->set( $post_id, "http://schema.org/$type", false ); |
|
| 38 | + } |
|
| 39 | + |
|
| 40 | + foreach ( $this->entity->get_same_as() as $same_as ) { |
|
| 41 | + add_post_meta( $post_id, 'entity_same_as', $same_as ); |
|
| 42 | + } |
|
| 43 | + |
|
| 44 | + return Wordpress_Content_Id::create_post( $post_id ); |
|
| 45 | + } |
|
| 46 | 46 | } |
@@ -16,7 +16,7 @@ discard block |
||
| 16 | 16 | /** |
| 17 | 17 | * @param $entity Valid_Remote_Entity |
| 18 | 18 | */ |
| 19 | - public function __construct( $entity ) { |
|
| 19 | + public function __construct($entity) { |
|
| 20 | 20 | $this->entity = $entity; |
| 21 | 21 | } |
| 22 | 22 | |
@@ -33,14 +33,14 @@ discard block |
||
| 33 | 33 | ) |
| 34 | 34 | ); |
| 35 | 35 | |
| 36 | - foreach ( $this->entity->get_types() as $type ) { |
|
| 37 | - $entity_type_service->set( $post_id, "http://schema.org/$type", false ); |
|
| 36 | + foreach ($this->entity->get_types() as $type) { |
|
| 37 | + $entity_type_service->set($post_id, "http://schema.org/$type", false); |
|
| 38 | 38 | } |
| 39 | 39 | |
| 40 | - foreach ( $this->entity->get_same_as() as $same_as ) { |
|
| 41 | - add_post_meta( $post_id, 'entity_same_as', $same_as ); |
|
| 40 | + foreach ($this->entity->get_same_as() as $same_as) { |
|
| 41 | + add_post_meta($post_id, 'entity_same_as', $same_as); |
|
| 42 | 42 | } |
| 43 | 43 | |
| 44 | - return Wordpress_Content_Id::create_post( $post_id ); |
|
| 44 | + return Wordpress_Content_Id::create_post($post_id); |
|
| 45 | 45 | } |
| 46 | 46 | } |
@@ -6,16 +6,16 @@ |
||
| 6 | 6 | |
| 7 | 7 | class Url_To_Remote_Entity_Converter { |
| 8 | 8 | |
| 9 | - /** |
|
| 10 | - * @param $url |
|
| 11 | - * |
|
| 12 | - * @return Remote_Entity |
|
| 13 | - */ |
|
| 14 | - public static function convert( $url ) { |
|
| 15 | - $target_path = '/id/' . preg_replace( '@^(https?)://@', '$1/', $url ); |
|
| 16 | - $response = Default_Api_Service::get_instance()->get( $target_path ); |
|
| 9 | + /** |
|
| 10 | + * @param $url |
|
| 11 | + * |
|
| 12 | + * @return Remote_Entity |
|
| 13 | + */ |
|
| 14 | + public static function convert( $url ) { |
|
| 15 | + $target_path = '/id/' . preg_replace( '@^(https?)://@', '$1/', $url ); |
|
| 16 | + $response = Default_Api_Service::get_instance()->get( $target_path ); |
|
| 17 | 17 | |
| 18 | - return Remote_Entity_Factory::from_response( $url, $response ); |
|
| 19 | - } |
|
| 18 | + return Remote_Entity_Factory::from_response( $url, $response ); |
|
| 19 | + } |
|
| 20 | 20 | |
| 21 | 21 | } |
@@ -11,11 +11,11 @@ |
||
| 11 | 11 | * |
| 12 | 12 | * @return Remote_Entity |
| 13 | 13 | */ |
| 14 | - public static function convert( $url ) { |
|
| 15 | - $target_path = '/id/' . preg_replace( '@^(https?)://@', '$1/', $url ); |
|
| 16 | - $response = Default_Api_Service::get_instance()->get( $target_path ); |
|
| 14 | + public static function convert($url) { |
|
| 15 | + $target_path = '/id/'.preg_replace('@^(https?)://@', '$1/', $url); |
|
| 16 | + $response = Default_Api_Service::get_instance()->get($target_path); |
|
| 17 | 17 | |
| 18 | - return Remote_Entity_Factory::from_response( $url, $response ); |
|
| 18 | + return Remote_Entity_Factory::from_response($url, $response); |
|
| 19 | 19 | } |
| 20 | 20 | |
| 21 | 21 | } |
@@ -4,19 +4,19 @@ |
||
| 4 | 4 | |
| 5 | 5 | class Invalid_Remote_Entity implements Remote_Entity { |
| 6 | 6 | |
| 7 | - public function get_name() { |
|
| 8 | - // TODO: Implement getName() method. |
|
| 9 | - } |
|
| 7 | + public function get_name() { |
|
| 8 | + // TODO: Implement getName() method. |
|
| 9 | + } |
|
| 10 | 10 | |
| 11 | - public function get_description() { |
|
| 12 | - // TODO: Implement getDescription() method. |
|
| 13 | - } |
|
| 11 | + public function get_description() { |
|
| 12 | + // TODO: Implement getDescription() method. |
|
| 13 | + } |
|
| 14 | 14 | |
| 15 | - public function get_same_as() { |
|
| 16 | - // TODO: Implement getSameAs() method. |
|
| 17 | - } |
|
| 15 | + public function get_same_as() { |
|
| 16 | + // TODO: Implement getSameAs() method. |
|
| 17 | + } |
|
| 18 | 18 | |
| 19 | - public function get_types() { |
|
| 20 | - // TODO: Implement getTypes() method. |
|
| 21 | - } |
|
| 19 | + public function get_types() { |
|
| 20 | + // TODO: Implement getTypes() method. |
|
| 21 | + } |
|
| 22 | 22 | } |