-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathfunkhaus-auto-seo.php
208 lines (182 loc) · 5.54 KB
/
funkhaus-auto-seo.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
<?php
/**
* Plugin Name: Auto SEO
* Description: Automatically implement SEO best practices using the power of AI.
* Version: 2.11
* Author: Funkhaus
* Plugin URI: https://github.com/funkhaus/auto-seo
* Author URI: http://funkhaus.us
* Text Domain: funkhaus-auto-seo
*
* @package funkhaus-auto-seo
*/
define( 'FH_AS_PATH', plugin_dir_path( __FILE__ ) );
/**
* Import required files
*/
require_once FH_AS_PATH . 'lib/load.php';
require_once FH_AS_PATH . 'includes/utilities.php';
require_once FH_AS_PATH . 'includes/settings.php';
require_once FH_AS_PATH . 'includes/azure.php';
require_once FH_AS_PATH . 'includes/convert-image.php';
require_once FH_AS_PATH . 'includes/rename-image.php';
require_once FH_AS_PATH . 'includes/focal-point.php';
require_once FH_AS_PATH . 'includes/color-detect.php';
require_once FH_AS_PATH . 'includes/blurhash.php';
require_once FH_AS_PATH . 'includes/api.php';
if ( ! fh_seo_is_gd_enabled() ) {
add_action( 'admin_notices', 'fh_seo_admin_notices' );
} else {
/**
* Main WP hooks to fire our logic on.
*/
add_filter( 'wp_handle_upload', 'fh_seo_convert', 10, 2 );
add_action( 'add_attachment', 'fh_seo_rename_and_discribe', 10, 1 );
}
/**
* Plugin activated, setup default options
*/
function fh_seo_plugin_activated() {
$defaults = array(
'api_key' => '',
);
add_option( 'fh_seo_settings', $defaults );
}
register_activation_hook( __FILE__, 'fh_seo_plugin_activated' );
/**
* Attempt to convert an image on upload. Currently only works for PNGs
*
* @see https://developer.wordpress.org/reference/hooks/wp_handle_upload/
*
* @param array $upload Reference to a single element of `$_FILES`.
* Call the function once for each uploaded file.
* See _wp_handle_upload() for accepted values.
* @param array|false $context Optional. An associative array of names => values
* to override default variables. Default false.
* See _wp_handle_upload() for accepted values.
*/
function fh_seo_convert( $upload, $context ) {
// Attempt to convert image
$converted = fh_seo_convert_to_jpg(
$upload['file'],
$upload['url'],
$upload['type']
);
if ( ! is_wp_error( $converted ) ) {
$upload = $converted;
}
return $upload;
}
/**
* Rename file and add meta data via Azure.
*
* @param int $attachment_id Attachment ID.
*/
function fh_seo_rename_and_discribe( $attachment_id ) {
$output = array(
'id' => $attachment_id,
);
// Only try this on formats Azure and ColorThief support
$type = get_post_mime_type( $attachment_id );
switch ( $type ) {
case 'image/jpeg':
case 'image/png':
case 'image/gif':
break;
default:
return false;
}
// Set color first as we don't need Azure for this
$output['set_color'] = fh_seo_attachment_set_colors( $attachment_id );
// Set blurhash
$output['set_blurhash'] = fh_seo_attachment_set_bluehash( $attachment_id );
// Abort now if no API key
$options = get_option( 'fh_seo_settings' );
if ( empty( $options['api_key'] ) ) {
return false;
}
// Name and caption attachment
$output['set_metadata'] = fh_seo_attachment_set_name_and_caption( $attachment_id );
// Set focal point
$output['set_focal_point'] = fh_seo_attachment_set_focal_point( $attachment_id );
// Save a timestamp so we know image was processed already
update_post_meta( $attachment_id, 'fh_seo_timestamp', date( 'Y-m-d H:i:s' ) ); // phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date
return $output;
}
/**
* Enqueue any CSS & JS scripts
*
* @param string $hook_suffix The current admin page.
*/
function fh_seo_admin_scripts( $hook_suffix ) {
// Only load scripts on the settings page
if ( $hook_suffix == 'settings_page_auto-seo' ) {
wp_enqueue_script(
'fh_seo_main',
plugins_url( 'js/main.js', __FILE__ ),
null,
time(),
true
);
wp_enqueue_style(
'fh_seo_main',
plugins_url( 'css/main.css', __FILE__ ),
null,
time()
);
// Import some JS vars from PHP
$js_vars = array(
'api_url' => site_url( '/wp-json/auto-seo' ),
'nonce' => wp_create_nonce( 'wp_rest' ),
);
wp_add_inline_script(
'fh_seo_main',
'var fh_seo_vars = ' . wp_json_encode( $js_vars ),
'before'
);
}
// Load focushaus scripts
wp_enqueue_script( 'jquery' );
wp_enqueue_script( 'focushaus', plugins_url( 'js/focushaus.js', __FILE__ ), array( 'jquery' ), '2.01', true );
wp_enqueue_style( 'focushaus', plugins_url( 'css/focushaus.css', __FILE__ ), null, '2.01' );
}
add_action( 'admin_enqueue_scripts', 'fh_seo_admin_scripts' );
/**
* Register custom API endpoints
*/
function fh_seo_add_api_routes() {
// Use this to trigger a deploy at Netlify
register_rest_route(
'auto-seo',
'/generate',
array(
array(
'methods' => 'POST',
'callback' => 'fh_seo_generate',
'args' => array(
'id' => array(),
'offset' => array(),
),
'permission_callback' => 'fh_seo_permissions',
),
)
);
}
add_action( 'rest_api_init', 'fh_seo_add_api_routes' );
/**
* Check if PHP GD module enabled.
*
* @return bool Returns true if enabled.
*/
function fh_seo_is_gd_enabled() {
return function_exists( 'imagecreatefrompng' );
}
/**
* Adds admin notice.
*/
function fh_seo_admin_notices() {
printf(
'<div class="notice notice-warning is-dismissible"><p>Warning: %s</p></div>',
__( '<b>Auto SEO</b> plugin requires GD php extension. Please install/enable it. For more information, please check <a href="https://www.php.net/manual/en/image.installation.php" target="_blank">documentation.</a>' )
);
}