forked from android-com-pl/wp-ai-alt-generator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
alt-text-generator-gpt-vision.php
114 lines (93 loc) · 3.84 KB
/
alt-text-generator-gpt-vision.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
<?php
/**
* @wordpress-plugin
* Plugin Name: AI Alt Text Generator for GPT Vision
* Plugin URI: https://github.com/android-com-pl/wp-ai-alt-generator
* Description: Automatically generate alternative text for images using OpenAI's GPT Vision API.
* Version: 2.0.2
* Requires at least: 6.3
* Requires PHP: 8.1
* Author: android.com.pl
* Author URI: https://android.com.pl/
* License: GPL v3
* License URI: https://www.gnu.org/licenses/gpl-3.0.html
* Text Domain: alt-text-generator-gpt-vision
* @package ACPL\AIAltGenerator
*/
namespace ACPL\AIAltGenerator;
use WP_Error;
if ( ! defined( 'ABSPATH' ) ) {
http_response_code( 403 );
exit();
}
define( 'ACPL_AI_ALT_PLUGIN_FILE', __FILE__ );
define( 'ACPL_AI_ALT_PLUGIN_PATH', plugin_dir_path( __FILE__ ) );
define( 'ACPL_AI_ALT_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
require __DIR__ . '/vendor/autoload.php';
class AltGeneratorPlugin {
public const OPTION_NAME = 'acpl_ai_alt_generator';
public function __construct() {
add_filter( 'wp_generate_attachment_metadata', [ AltGenerator::class, 'on_attachment_upload' ], 10, 3 );
add_action( 'rest_api_init', [ ( new Api() ), 'register_routes' ] );
add_action( 'enqueue_block_editor_assets', fn()=> $this->enqueue_script( 'editor' ) );
add_action( 'wp_enqueue_media', fn()=> $this->enqueue_script( 'media-modal', true ) );
add_action( 'admin_enqueue_scripts', fn()=> $this->enqueue_attachment_edit_page_script() );
add_action( 'activated_plugin', [ $this,'redirect_to_plugin_settings_after_activation' ] );
add_filter( 'plugin_row_meta', [ $this, 'plugin_row_meta' ], 10, 2 );
}
public static function get_options(): array|false {
return get_option( self::OPTION_NAME );
}
public static function error_log( WP_Error $error ): WP_Error {
if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
// phpcs:disable WordPress.PHP.DevelopmentFunctions.error_log_error_log
error_log( '[AI Alt Generator] ' . $error->get_error_message() );
// phpcs:enable
}
return $error;
}
private function enqueue_script( string $file_name, array|bool $args = false ): void {
$asset_file = include ACPL_AI_ALT_PLUGIN_PATH . "build/$file_name.asset.php";
$handle = "acpl/ai-alt-generator/$file_name";
wp_enqueue_script( $handle, ACPL_AI_ALT_PLUGIN_URL . "build/$file_name.js", $asset_file['dependencies'], $asset_file['version'], $args );
wp_set_script_translations( $handle, 'alt-text-generator-gpt-vision' );
}
private function enqueue_attachment_edit_page_script(): void {
global $pagenow;
if ( 'post.php' === $pagenow && 'attachment' === get_post_type() && wp_attachment_is_image() ) {
$this->enqueue_script( 'media-edit-page', true );
}
}
public function redirect_to_plugin_settings_after_activation( string $plugin ): void {
global $pagenow;
// Disable redirect if there are multiple plugins activated at once.
// phpcs:disable WordPress.Security.NonceVerification.Recommended
if ( 'plugins.php' === $pagenow && isset( $_REQUEST['action'] ) && 'activate-selected' === $_REQUEST['action'] ) {
return;
}
// phpcs:enable
if ( plugin_basename( __FILE__ ) === $plugin ) {
wp_safe_redirect( admin_url( 'options-media.php#' . Admin::SETTINGS_SECTION_ID ) );
exit();
}
}
public function plugin_row_meta( array $plugin_meta, string $plugin_file ): array {
if ( str_contains( $plugin_file, basename( ACPL_AI_ALT_PLUGIN_FILE ) ) ) {
$plugin_meta[] = sprintf(
'<a href="%s">%s</a>',
admin_url( 'options-media.php#' . Admin::SETTINGS_SECTION_ID ),
__( 'Settings', 'alt-text-generator-gpt-vision' )
);
$plugin_meta[] = sprintf(
'<a href="%s" target="_blank" rel="noopener noreferrer">%s</a>',
'https://github.com/android-com-pl/wp-ai-alt-generator?sponsor=1',
__( 'Support Development', 'alt-text-generator-gpt-vision' )
);
}
return $plugin_meta;
}
}
new AltGeneratorPlugin();
if ( is_admin() ) {
new Admin();
}