-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplugin.php
168 lines (145 loc) · 4.51 KB
/
plugin.php
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<?php
/**
* Plugin Name: Open Graph Tags
* Description: Adds Open Graph meta tags to your posts with custom values and fallbacks
* Version: 0.1.0
* Author: JuanMa Garrido
* Text Domain: open-graph-tags
* Domain Path: /languages
* License: GPL v2 or later
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
*
* @package OpenGraphTags
*/
declare(strict_types=1);
if (!defined('ABSPATH')) {
exit;
}
// Plugin constants
define('OGT_VERSION', '0.1.0');
define('OGT_PLUGIN_DIR', plugin_dir_path(__FILE__));
define('OGT_PLUGIN_URL', plugin_dir_url(__FILE__));
/**
* Initialize the plugin
*/
function ogt_init(): void {
// Load text domain for translations
load_plugin_textdomain('open-graph-tags', false, dirname(plugin_basename(__FILE__)) . '/languages');
// Register meta fields
add_action('init', 'ogt_register_meta');
// Add meta tags to head
add_action('wp_head', 'ogt_output_meta_tags');
// Enqueue editor assets
add_action('enqueue_block_editor_assets', 'ogt_enqueue_editor_assets');
}
add_action('init', 'ogt_init');
/**
* Register meta fields for Open Graph tags
*/
function ogt_register_meta(): void {
register_post_meta('post', '_ogt_title', [
'show_in_rest' => true,
'single' => true,
'type' => 'string',
'auth_callback' => function() {
return current_user_can('edit_posts');
}
]);
register_post_meta('post', '_ogt_description', [
'show_in_rest' => true,
'single' => true,
'type' => 'string',
'auth_callback' => function() {
return current_user_can('edit_posts');
}
]);
register_post_meta('post', '_ogt_image_id', [
'show_in_rest' => true,
'single' => true,
'type' => 'integer',
'auth_callback' => function() {
return current_user_can('edit_posts');
}
]);
register_post_meta('post', '_ogt_url', [
'show_in_rest' => true,
'single' => true,
'type' => 'string',
'auth_callback' => function() {
return current_user_can('edit_posts');
}
]);
register_post_meta('post', '_ogt_type', [
'show_in_rest' => true,
'single' => true,
'type' => 'string',
'auth_callback' => function() {
return current_user_can('edit_posts');
}
]);
}
/**
* Enqueue editor assets
*/
function ogt_enqueue_editor_assets(): void {
$asset_file = include(OGT_PLUGIN_DIR . 'build/index.asset.php');
wp_enqueue_script(
'open-graph-tags-editor',
OGT_PLUGIN_URL . 'build/index.js',
$asset_file['dependencies'],
$asset_file['version'],
true
);
wp_set_script_translations('open-graph-tags-editor', 'open-graph-tags');
}
/**
* Output Open Graph meta tags in head
*/
function ogt_output_meta_tags(): void {
if (!is_singular('post')) {
return;
}
$post_id = get_the_ID();
// Get custom values or fallbacks
$title = get_post_meta($post_id, '_ogt_title', true) ?: get_the_title($post_id);
$description = get_post_meta($post_id, '_ogt_description', true) ?: get_the_excerpt($post_id);
$url = get_post_meta($post_id, '_ogt_url', true) ?: get_permalink($post_id);
$type = get_post_meta($post_id, '_ogt_type', true) ?: 'article';
$image_id = get_post_meta($post_id, '_ogt_image_id', true);
$image_url = '';
if ($image_id) {
$image_url = wp_get_attachment_image_url($image_id, 'full');
} elseif (has_post_thumbnail($post_id)) {
$image_url = get_the_post_thumbnail_url($post_id, 'full');
}
// Output meta tags
if ($title) {
printf('<meta property="og:title" content="%s" />' . PHP_EOL, esc_attr($title));
}
if ($description) {
printf('<meta property="og:description" content="%s" />' . PHP_EOL, esc_attr($description));
}
if ($url) {
printf('<meta property="og:url" content="%s" />' . PHP_EOL, esc_url($url));
}
if ($type) {
printf('<meta property="og:type" content="%s" />' . PHP_EOL, esc_attr($type));
}
if ($image_url) {
printf('<meta property="og:image" content="%s" />' . PHP_EOL, esc_url($image_url));
}
}
/**
* Plugin deactivation hook
*/
function ogt_deactivate(): void {
global $wpdb;
// Remove all plugin meta data
$wpdb->query(
$wpdb->prepare(
"DELETE FROM {$wpdb->postmeta} WHERE meta_key LIKE %s",
'_ogt_%'
)
);
}
register_deactivation_hook(__FILE__, 'ogt_deactivate');