| 1 |
<?php |
| 2 |
/** |
| 3 |
* Plugin Name: iGen SEO |
| 4 |
* Description: Connect WordPress to iGen SEO for publishing, IndexNow, and SEO metadata aligned with Yoast SEO, Rank Math, or All in One SEO. |
| 5 |
* Version: 1.3.21 |
| 6 |
* Author: IGen Team |
| 7 |
* License: GPLv2 or later |
| 8 |
* License URI: https://www.gnu.org/licenses/gpl-2.0.html |
| 9 |
* Text Domain: igen-seo-api |
| 10 |
* Domain Path: /languages |
| 11 |
* Requires at least: 5.0 |
| 12 |
* Tested up to: 6.8 |
| 13 |
* Requires PHP: 7.4 |
| 14 |
*/ |
| 15 |
|
| 16 |
// Prevent direct access |
| 17 |
if ( ! defined( 'ABSPATH' ) ) { |
| 18 |
exit; |
| 19 |
} |
| 20 |
|
| 21 |
define( 'IGEN_SEO_API_VERSION', '1.3.21' ); |
| 22 |
define( 'IGEN_SEO_API_PLUGIN_FILE', __FILE__ ); |
| 23 |
|
| 24 |
/** |
| 25 |
* Load translations from /languages (e.g. igen-seo-api-zh_TW.mo). |
| 26 |
* |
| 27 |
* @return void |
| 28 |
*/ |
| 29 |
function igen_seo_api_load_textdomain() { |
| 30 |
load_plugin_textdomain( |
| 31 |
'igen-seo-api', |
| 32 |
false, |
| 33 |
dirname( plugin_basename( IGEN_SEO_API_PLUGIN_FILE ) ) . '/languages' |
| 34 |
); |
| 35 |
} |
| 36 |
add_action( 'plugins_loaded', 'igen_seo_api_load_textdomain' ); |
| 37 |
|
| 38 |
/** |
| 39 |
* Register Yoast SEO meta fields for REST API access |
| 40 |
* |
| 41 |
* This function registers Yoast SEO meta fields to make them accessible |
| 42 |
* through the WordPress REST API for reading and writing operations. |
| 43 |
* |
| 44 |
* @since 1.0.0 |
| 45 |
*/ |
| 46 |
function igen_register_yoast_meta_fields() { |
| 47 |
// Define supported post types |
| 48 |
$post_types = array( 'page', 'post' ); |
| 49 |
|
| 50 |
// Define Yoast SEO meta fields to register |
| 51 |
$fields = array( |
| 52 |
'_yoast_wpseo_title', // SEO title |
| 53 |
'_yoast_wpseo_metadesc', // Meta description |
| 54 |
'_yoast_wpseo_focuskw' // Focus keyword |
| 55 |
); |
| 56 |
|
| 57 |
// Register meta fields for each post type |
| 58 |
foreach ( $post_types as $post_type ) { |
| 59 |
foreach ( $fields as $field ) { |
| 60 |
register_post_meta( $post_type, $field, array( |
| 61 |
'type' => 'string', |
| 62 |
'single' => true, |
| 63 |
'show_in_rest' => true, |
| 64 |
'auth_callback' => 'igen_meta_auth_callback', |
| 65 |
'description' => sprintf( |
| 66 |
// translators: %s is the field name (title, metadesc, or focuskw) |
| 67 |
__( 'Yoast SEO %s field', 'igen-seo-api' ), |
| 68 |
ucfirst( str_replace( '_yoast_wpseo_', '', $field ) ) |
| 69 |
), |
| 70 |
) ); |
| 71 |
} |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Authentication callback for meta fields |
| 77 |
* |
| 78 |
* @since 1.0.0 |
| 79 |
* @return bool True if user can edit posts, false otherwise |
| 80 |
*/ |
| 81 |
function igen_meta_auth_callback() { |
| 82 |
return current_user_can( 'edit_posts' ); |
| 83 |
} |
| 84 |
|
| 85 |
// Hook the registration function to init |
| 86 |
add_action( 'init', 'igen_register_yoast_meta_fields' ); |
| 87 |
|
| 88 |
/** |
| 89 |
* Check if Yoast SEO plugin is active |
| 90 |
* |
| 91 |
* @since 1.0.0 |
| 92 |
* @return bool True if Yoast SEO is active, false otherwise |
| 93 |
*/ |
| 94 |
function igen_is_yoast_seo_active() { |
| 95 |
if ( ! function_exists( 'is_plugin_active' ) ) { |
| 96 |
require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 97 |
} |
| 98 |
return is_plugin_active( 'wordpress-seo/wp-seo.php' ) || |
| 99 |
is_plugin_active( 'wordpress-seo-premium/wp-seo-premium.php' ); |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Whether Rank Math SEO is active (free or common Pro bootstrap). |
| 104 |
* |
| 105 |
* @return bool |
| 106 |
*/ |
| 107 |
function igen_is_rank_math_active() { |
| 108 |
if ( ! function_exists( 'is_plugin_active' ) ) { |
| 109 |
require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 110 |
} |
| 111 |
return is_plugin_active( 'seo-by-rank-math/rank-math.php' ) || |
| 112 |
is_plugin_active( 'seo-by-rank-math-pro/rank-math-pro.php' ); |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* Whether All in One SEO (free or Pro) is active. |
| 117 |
* |
| 118 |
* @return bool |
| 119 |
*/ |
| 120 |
function igen_is_aioseo_active() { |
| 121 |
if ( ! function_exists( 'is_plugin_active' ) ) { |
| 122 |
require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 123 |
} |
| 124 |
return is_plugin_active( 'all-in-one-seo-pack/all_in_one_seo_pack.php' ) || |
| 125 |
is_plugin_active( 'all-in-one-seo-pack-pro/all_in_one_seo_pack.php' ) || |
| 126 |
is_plugin_active( 'aioseo-pro/aioseo-pro.php' ); |
| 127 |
} |
| 128 |
|
| 129 |
/** |
| 130 |
* Display admin notice if Yoast SEO is not active |
| 131 |
* |
| 132 |
* @since 1.0.0 |
| 133 |
*/ |
| 134 |
function igen_yoast_seo_admin_notice() { |
| 135 |
if ( ! igen_is_yoast_seo_active() ) { |
| 136 |
?> |
| 137 |
<div class="notice notice-warning is-dismissible"> |
| 138 |
<p> |
| 139 |
<strong><?php esc_html_e( 'iGen SEO', 'igen-seo-api' ); ?>:</strong> |
| 140 |
<?php esc_html_e( 'This plugin requires Yoast SEO to be installed and activated.', 'igen-seo-api' ); ?> |
| 141 |
<a href="<?php echo esc_url( admin_url( 'plugin-install.php?s=yoast+seo&tab=search&type=term' ) ); ?>"> |
| 142 |
<?php esc_html_e( 'Install Yoast SEO', 'igen-seo-api' ); ?> |
| 143 |
</a> |
| 144 |
</p> |
| 145 |
</div> |
| 146 |
<?php |
| 147 |
} |
| 148 |
} |
| 149 |
add_action( 'admin_notices', 'igen_yoast_seo_admin_notice' ); |
| 150 |
|
| 151 |
/** |
| 152 |
* Only register meta fields if Yoast SEO is active |
| 153 |
* |
| 154 |
* @since 1.0.0 |
| 155 |
*/ |
| 156 |
function igen_register_yoast_meta_fields_safe() { |
| 157 |
if ( ! igen_is_yoast_seo_active() ) { |
| 158 |
return; |
| 159 |
} |
| 160 |
|
| 161 |
igen_register_yoast_meta_fields(); |
| 162 |
} |
| 163 |
remove_action( 'init', 'igen_register_yoast_meta_fields' ); |
| 164 |
add_action( 'init', 'igen_register_yoast_meta_fields_safe' ); |
| 165 |
|
| 166 |
/** |
| 167 |
* Plugin activation hook |
| 168 |
* |
| 169 |
* @since 1.0.0 |
| 170 |
*/ |
| 171 |
function igen_wordpress_activate() { |
| 172 |
// Flush rewrite rules on activation |
| 173 |
flush_rewrite_rules(); |
| 174 |
} |
| 175 |
register_activation_hook( __FILE__, 'igen_wordpress_activate' ); |
| 176 |
|
| 177 |
/** |
| 178 |
* Plugin deactivation hook |
| 179 |
* |
| 180 |
* @since 1.0.0 |
| 181 |
*/ |
| 182 |
function igen_wordpress_deactivate() { |
| 183 |
// Flush rewrite rules on deactivation |
| 184 |
flush_rewrite_rules(); |
| 185 |
} |
| 186 |
register_deactivation_hook( __FILE__, 'igen_wordpress_deactivate' ); |
| 187 |
|
| 188 |
require_once plugin_dir_path( __FILE__ ) . 'includes/class-igen-seo-connector.php'; |
| 189 |
new IGen_SEO_Connector( IGEN_SEO_API_PLUGIN_FILE ); |
| 190 |
|