MetaData.php
3.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<?php
namespace AIOSEO\Plugin\Common\Meta;
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
use AIOSEO\Plugin\Common\Models;
/**
* Handles fetching metadata for the current object.
*
* @since 4.0.0
*/
class MetaData {
/**
* Class constructor.
*
* @since 4.0.0
*/
public function __construct() {
add_action( 'wpml_pro_translation_completed', [ $this, 'updateWpmlLocalization' ], 1000, 3 );
}
/**
* Update the localized data in our posts table.
*
* @since 4.0.0
*
* @param integer $postId The post ID.
* @param array $fields An array of fields to update.
* @return void
*/
public function updateWpmlLocalization( $postId, $fields, $job ) {
$aioseoFields = [
'_aioseo_title',
'_aioseo_description',
'_aioseo_keywords',
'_aioseo_og_title',
'_aioseo_og_description',
'_aioseo_twitter_title',
'_aioseo_twitter_description'
];
$parentId = $job->original_doc_id;
$parentPost = Models\Post::getPost( $parentId );
$currentPost = Models\Post::getPost( $postId );
$columns = $parentPost->getColumns();
foreach ( $columns as $column => $value ) {
// Skip the ID columns.
if ( 'id' === $column || 'post_id' === $column ) {
continue;
}
$currentPost->$column = $parentPost->$column;
}
$currentPost->post_id = $postId;
foreach ( $aioseoFields as $aioseoField ) {
if ( ! empty( $fields[ 'field-' . $aioseoField . '-0' ] ) ) {
$value = $fields[ 'field-' . $aioseoField . '-0' ]['data'];
if ( '_aioseo_keywords' === $aioseoField ) {
$value = explode( ',', $value );
foreach ( $value as $k => $keyword ) {
$value[ $k ] = [
'label' => $keyword,
'value' => $keyword
];
}
$value = wp_json_encode( $value );
}
$currentPost->{ str_replace( '_aioseo_', '', $aioseoField ) } = $value;
}
}
$currentPost->save();
}
/**
* Returns the metadata for the current object.
*
* @since 4.0.0
*
* @param WP_Post $post The post object (optional).
* @return array|bool The meta data or false.
*/
public function getMetaData( $post = null ) {
static $posts = [];
if ( ! $post ) {
$post = aioseo()->helpers->getPost();
}
if ( $post ) {
$post = is_object( $post ) ? $post : aioseo()->helpers->getPost( $post );
// If we still have no post, let's return false.
if ( empty( $post ) ) {
return false;
}
if ( isset( $posts[ $post->ID ] ) ) {
return $posts[ $post->ID ];
}
$posts[ $post->ID ] = Models\Post::getPost( $post->ID );
if ( ! $posts[ $post->ID ]->exists() ) {
$migratedMeta = aioseo()->migration->meta->getMigratedPostMeta( $post->ID );
if ( ! empty( $migratedMeta ) ) {
foreach ( $migratedMeta as $k => $v ) {
$posts[ $post->ID ]->{$k} = $v;
}
$posts[ $post->ID ]->save();
}
}
return $posts[ $post->ID ];
}
return false;
}
/**
* Returns the cached OG image from the meta data.
*
* @since 4.1.6
*
* @param Object $metaData The meta data object.
* @return array An array of image data.
*/
public function getCachedOgImage( $metaData ) {
return [
$metaData->og_image_url,
isset( $metaData->og_image_width ) ? $metaData->og_image_width : null,
isset( $metaData->og_image_height ) ? $metaData->og_image_height : null
];
}
}