$chunk array of post IDs to add alt text to
* @param string $call_type context of the call ('single' or 'bulk')
*
* @return array Response data with the results of the alt text addition.
*/
function alt_text_magic_change_alt($chunk, $call_type)
{
$success = false;
$request_body = array();
$im_data_list = array();
$api_key = get_option('alt_text_magic_api_key');
if (!$api_key) {
$request_body = array('message' => 'invalid API key');
return array('success' => false, 'response' => $request_body);
}
foreach ($chunk as $post_ID) {
if (alt_text_magic_is_image_supported($post_ID)) {
$post_thumbnail_src = wp_get_attachment_url($post_ID);
$im = file_get_contents($post_thumbnail_src);
$imdata = base64_encode($im);
$im_data_list[] = $imdata;
}
}
$language = get_option('alt_text_magic_language');
if ($language === false) {
$language = 'en-US';
}
$url = 'https://api.alttextmagic.com/predict';
$body = array('api_key' => $api_key, 'images' => $im_data_list, 'target_language' => $language);
$args = array(
'method' => 'POST',
'mode' => 'cors',
'headers' => array(
'Content-Type' => 'application/json',
'Access-Control-Allow-Origin' => '*',
),
'body' => json_encode($body),
'timeout' => 120,
'sslcertificates' => dirname(__FILE__) . '/data/cacert.pem'
);
$request = wp_remote_post($url, $args);
$status = wp_remote_retrieve_response_code($request);
$success = !(is_wp_error($request) || $status != 200);
if ($success) {
$response = wp_remote_retrieve_body($request);
$response = json_decode($response, true);
$request_body = json_decode($request['body']);
// Update alt texts for images in $chunk.
for ($i = 0; $i < count($response['captions']); $i++) {
// Index into captions and chunk to get the caption and post_ID.
$caption = $response['captions'][$i];
$post_ID = $chunk[$i];
update_post_meta($post_ID, '_wp_attachment_image_alt', $caption);
}
// Update bulk recent suggestions if the $call_type is 'bulk'.
if ($call_type == 'bulk') {
$bulk_suggestions = get_option('alt_text_magic_bulk_suggestions');
if (!$bulk_suggestions) {
$bulk_suggestions = array();
}
for ($i = 0; $i < count($response['captions']); $i++) {
// Index into captions and chunk to get the caption and post_ID.
$caption = $response['captions'][$i];
$post_ID = $chunk[$i];
array_push($bulk_suggestions, array(
'title' => get_the_title($post_ID),
'suggestion' => $caption,
'url' => wp_get_attachment_url($post_ID),
'post_ID' => $post_ID,
));
}
update_option('alt_text_magic_bulk_suggestions', $bulk_suggestions);
}
// Update account data.
$account_type = $request_body->account_type;
$image_credit_count = $request_body->image_credit_count;
$image_credit_limit = $request_body->image_credit_limit;
$monthly_image_count = $request_body->monthly_image_count;
$monthly_image_limit = $request_body->monthly_image_limit;
$updated_at = $request_body->updated_at;
update_option('alt_text_magic_account_type', $account_type);
update_option('alt_text_magic_image_credit_count', $image_credit_count);
update_option('alt_text_magic_image_credit_limit', $image_credit_limit);
update_option('alt_text_magic_monthly_image_count', $monthly_image_count);
update_option('alt_text_magic_monthly_image_limit', $monthly_image_limit);
update_option('alt_text_magic_status', $status);
update_option('alt_text_magic_batch_timestamp', $updated_at);
} else {
if ($status === '') {
$request_body = array('response' => array('message' => 'cURL error'));
} else if (is_wp_error($request)) {
$request_body = array('response' => array('message' => 'WP error'));
} else {
$request_body = json_decode($request['body']);
if ($request_body === null) {
$request_body = array('response' => array('message' => 'Request body is not JSON'));
} else if (!property_exists($request_body, 'message')) {
$request_body = array('response' => array('message' => 'Request body did not contain a message'));
} else if ($request_body->message == 'insufficient image count or credits') {
update_option('alt_text_magic_image_limit_notification_dismissed', false);
}
}
}
return array('success' => $success, 'response' => $request_body);
}
/**
* Checks to see if the filename is well-formed.
*
* A well-formed filename is one that does not contain any spaces.
*
* @param int $filename the filename to check
*
* @return bool true if the filename is well-formed, false otherwise
*/
function alt_text_magic_is_well_formed_filename($filename)
{
// The param $filename is from wp_get_attachment_url($post_ID), which can return boolean false.
if ($filename === false) {
return false;
}
// Check if $filename is a string.
if (is_string($filename) === false) {
return false;
}
// If the filename contains any spaces after trim(), return false.
$filename = trim($filename);
if (strpos($filename, ' ') !== false) {
return false;
}
return true;
}
/**
* Adds alt text to only images when an attachment is added.
*
* Calls alt_text_magic_change_alt() to add alt text to the image
* and passes a $call_type of "single" as the second argument as
* this call is made when an image is added to the media library
* and not when a library update is performed.
*
* @param int $post_ID ID of the attachment that was added
*
* @return null
*/
function alt_text_magic_on_add_attachment($post_ID)
{
$generate_on_upload = get_option('alt_text_magic_generate_on_upload', null);
// Generate on upload setting is turned off, so return before setting alt text.
if ($generate_on_upload === '0') {
return null;
}
if (alt_text_magic_is_image_supported($post_ID)) {
$chunk = array($post_ID);
alt_text_magic_change_alt($chunk, "single");
}
return null;
}
// Add action for when an attachment is added.
add_action('add_attachment', 'alt_text_magic_on_add_attachment', 10, 1);
/**
* Add notices when Alt Text Magic needs to display information to the user.
*
* Notifications are added when the API Key is invalid or the user
* has run out of credits. Notifications are only displayed on
* index.php and upload.php.
*
* @return null
*/
function alt_text_magic_admin_notices()
{
global $pagenow;
// Alt Text Magic src icon.
$icon_src = plugin_dir_url(__FILE__) . 'assets/chicken-icon-gr.png';
wp_enqueue_script('jquery');
// Only display notifications on index.php and upload.php.
if ($pagenow == 'index.php' || $pagenow == 'upload.php') {
$info = alt_text_magic_get_info();
if (!$info->success) {
$notification_dismissed = get_option('alt_text_magic_invalid_api_key_notification_dismissed');
if (!$notification_dismissed && $info->message == 'invalid API key') {
?>