forked from wikimedia/wikipediapreview-wordpress
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwikipediapreview.php
171 lines (150 loc) · 4.78 KB
/
wikipediapreview.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
169
170
171
<?php
/**
* Plugin Name: Wikipedia Preview
* Plugin URI: https://github.com/wikimedia/wikipedia-preview
* Description: Provide context to your readers by displaying a Wikipedia article preview when a reader clicks or hovers over a word or concept.
* Text Domain: wikipedia-preview
* Version: 1.14.2
* Requires at least: 6.1
* Requires PHP: 5.6.39
* Author: Wikimedia Foundation
* Author URI: https://wikimediafoundation.org/
* License: MIT
* License URI: https://github.com/wikimedia/wikipedia-preview/blob/main/LICENSE
*/
DEFINE( 'WIKIPEDIA_PREVIEW_PLUGIN_VERSION', '1.14.2' );
function get_public_post_types() {
$post_types = get_post_types( array(), 'objects' );
$public_post_types = array();
foreach ( $post_types as $post_type ) {
if ( $post_type->public ) {
$public_post_types[] = $post_type->name;
}
}
return $public_post_types;
}
function is_enabled_on_post_type( $post_type ) {
$enabled_post_types = apply_filters(
'wikipediapreview_enabled_post_types',
get_public_post_types()
);
return in_array( $post_type, $enabled_post_types, true );
}
function wikipediapreview_enqueue_scripts() {
if ( ! is_enabled_on_post_type( get_post_type() ) ) {
return;
}
$build_dir = plugin_dir_url( __FILE__ ) . 'build/';
$libs_dir = plugin_dir_url( __FILE__ ) . 'libs/';
$media_type_all = 'all';
$no_dependencies = array();
$in_footer = true;
wp_enqueue_script(
'wikipedia-preview',
$libs_dir . 'wikipedia-preview.js',
$no_dependencies,
WIKIPEDIA_PREVIEW_PLUGIN_VERSION,
$in_footer
);
wp_enqueue_script(
'wikipedia-preview-init',
$build_dir . 'init.js',
$no_dependencies,
WIKIPEDIA_PREVIEW_PLUGIN_VERSION,
$in_footer
);
global $post;
if ( isset( $post->ID ) ) {
$options = array(
'detectLinks' => get_post_meta( $post->ID, 'wikipediapreview_detectlinks', true ),
);
wp_localize_script( 'wikipedia-preview-init', 'wikipediapreview_init_options', $options );
}
wp_enqueue_style(
'wikipedia-preview-link-style',
$libs_dir . 'wikipedia-preview-link.css',
$no_dependencies,
WIKIPEDIA_PREVIEW_PLUGIN_VERSION,
$media_type_all
);
}
function wikipediapreview_detect_deletion() {
delete_option( 'wikipediapreview_options_detect_links' );
}
function wikipediapreview_guten_enqueue() {
if ( ! is_enabled_on_post_type( get_post_type() ) ) {
return;
}
$build_dir = plugin_dir_url( __FILE__ ) . 'build/';
$libs_dir = plugin_dir_url( __FILE__ ) . 'libs/';
$media_type_all = 'all';
$no_dependencies = array();
$in_footer = true;
wp_enqueue_script(
'wikipedia-preview-edit-link',
$build_dir . 'index.js',
$no_dependencies,
WIKIPEDIA_PREVIEW_PLUGIN_VERSION,
$in_footer
);
wp_enqueue_style(
'wikipedia-preview-core-style',
$libs_dir . 'wikipedia-preview.css',
$no_dependencies,
WIKIPEDIA_PREVIEW_PLUGIN_VERSION,
$media_type_all
);
wp_enqueue_style(
'wikipedia-preview-style',
$build_dir . 'style-index.css',
$no_dependencies,
WIKIPEDIA_PREVIEW_PLUGIN_VERSION,
$media_type_all
);
wp_enqueue_style(
'wikipedia-preview-link-style',
$libs_dir . 'wikipedia-preview-link.css',
$no_dependencies,
WIKIPEDIA_PREVIEW_PLUGIN_VERSION,
$media_type_all
);
}
function myguten_set_script_translations() {
wp_set_script_translations( 'wikipedia-preview-localization', 'wikipedia-preview' );
}
function register_detectlinks_postmeta() {
$all_post_types = '';
$meta_name = 'wikipediapreview_detectlinks';
$options = array(
'show_in_rest' => true,
'auth_callback' => true,
'single' => true,
'type' => 'boolean',
'default' => true, // it could default to false when the gutenburg support is released
);
register_post_meta( $all_post_types, $meta_name, $options );
}
function make_link( $text, $url ) {
return '<a target="_BLANK" href="' . esc_url( $url ) . '">' . $text . '</a>';
}
function add_meta_links( $links_array, $plugin_file_name, $plugin_data, $status ) { // phpcs:ignore
if ( strpos( $plugin_file_name, basename( __FILE__ ) ) ) {
$links_array = array_merge(
$links_array,
array(
make_link( __( 'Review', 'wikipedia-preview' ), 'https://wordpress.org/support/plugin/wikipedia-preview/reviews/#new-post' ),
make_link( __( 'Support', 'wikipedia-preview' ), 'https://wordpress.org/support/plugin/wikipedia-preview/' ),
)
);
}
return $links_array;
}
add_filter( 'plugin_row_meta', 'add_meta_links', 10, 4 );
register_deactivation_hook( __FILE__, 'wikipediapreview_detect_deletion' );
add_action( 'wp_enqueue_scripts', 'wikipediapreview_enqueue_scripts' );
add_action( 'enqueue_block_editor_assets', 'wikipediapreview_guten_enqueue' );
add_action( 'init', 'myguten_set_script_translations' );
add_action( 'init', 'register_detectlinks_postmeta' );
require __DIR__ . '/tooltip.php';
require __DIR__ . '/banner.php';
require __DIR__ . '/intro.php';