230 lines
6.2 KiB
PHP
230 lines
6.2 KiB
PHP
<?php
|
|
/**
|
|
* Plugin Name: Maple Icons
|
|
* Plugin URI: https://jetrails.com/maple-icons
|
|
* Description: Insert beautiful open-source icons into your content with a Gutenberg block. Download icon sets from CDN and serve locally.
|
|
* Version: 1.0.0
|
|
* Requires at least: 6.5
|
|
* Requires PHP: 7.4
|
|
* Author: JetRails
|
|
* Author URI: https://jetrails.com
|
|
* License: GPL-2.0-or-later
|
|
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
|
|
* Text Domain: maple-icons
|
|
* Domain Path: /languages
|
|
*
|
|
* @package MapleIcons
|
|
*/
|
|
|
|
if ( ! defined( 'ABSPATH' ) ) {
|
|
exit;
|
|
}
|
|
|
|
/**
|
|
* Plugin constants.
|
|
*/
|
|
define( 'MI_VERSION', '1.0.0' );
|
|
define( 'MI_PLUGIN_FILE', __FILE__ );
|
|
define( 'MI_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
|
|
define( 'MI_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
|
|
define( 'MI_PLUGIN_BASENAME', plugin_basename( __FILE__ ) );
|
|
define( 'MI_ICONS_DIR', WP_CONTENT_DIR . '/maple-icons/' );
|
|
define( 'MI_ICONS_URL', content_url( '/maple-icons/' ) );
|
|
define( 'MI_PRESETS_DIR', MI_PLUGIN_DIR . 'presets/' );
|
|
|
|
// Limits.
|
|
define( 'MI_DOWNLOAD_BATCH_SIZE', 50 );
|
|
define( 'MI_SEARCH_LIMIT', 50 );
|
|
define( 'MI_DOWNLOAD_TIMEOUT', 10 );
|
|
|
|
/**
|
|
* Autoloader for plugin classes.
|
|
*
|
|
* @param string $class_name The class name to load.
|
|
*/
|
|
function mi_autoloader( $class_name ) {
|
|
// Only handle our classes.
|
|
if ( strpos( $class_name, 'MI_' ) !== 0 ) {
|
|
return;
|
|
}
|
|
|
|
// Convert class name to filename.
|
|
$class_file = 'class-' . strtolower( str_replace( '_', '-', $class_name ) ) . '.php';
|
|
$class_path = MI_PLUGIN_DIR . 'includes/' . $class_file;
|
|
|
|
if ( file_exists( $class_path ) ) {
|
|
require_once $class_path;
|
|
}
|
|
}
|
|
spl_autoload_register( 'mi_autoloader' );
|
|
|
|
/**
|
|
* Plugin activation hook.
|
|
*/
|
|
function mi_activate() {
|
|
// Check WordPress version.
|
|
if ( version_compare( get_bloginfo( 'version' ), '6.5', '<' ) ) {
|
|
deactivate_plugins( MI_PLUGIN_BASENAME );
|
|
wp_die(
|
|
esc_html__( 'Maple Icons requires WordPress 6.5 or higher.', 'maple-icons' ),
|
|
esc_html__( 'Plugin Activation Error', 'maple-icons' ),
|
|
array( 'back_link' => true )
|
|
);
|
|
}
|
|
|
|
// Create icons directory if it doesn't exist.
|
|
if ( ! file_exists( MI_ICONS_DIR ) ) {
|
|
wp_mkdir_p( MI_ICONS_DIR );
|
|
}
|
|
|
|
// Initialize default settings.
|
|
$default_settings = array(
|
|
'active_set' => '',
|
|
'downloaded_sets' => array(),
|
|
);
|
|
|
|
if ( false === get_option( 'maple_icons_settings' ) ) {
|
|
add_option( 'maple_icons_settings', $default_settings );
|
|
}
|
|
|
|
// Flush rewrite rules.
|
|
flush_rewrite_rules();
|
|
}
|
|
register_activation_hook( MI_PLUGIN_FILE, 'mi_activate' );
|
|
|
|
/**
|
|
* Plugin deactivation hook.
|
|
*/
|
|
function mi_deactivate() {
|
|
// Clean up transients.
|
|
global $wpdb;
|
|
$wpdb->query(
|
|
"DELETE FROM {$wpdb->options} WHERE option_name LIKE '_transient_mi_%' OR option_name LIKE '_transient_timeout_mi_%'"
|
|
);
|
|
|
|
flush_rewrite_rules();
|
|
}
|
|
register_deactivation_hook( MI_PLUGIN_FILE, 'mi_deactivate' );
|
|
|
|
/**
|
|
* Check WordPress version on admin init (in case WP was downgraded).
|
|
*/
|
|
function mi_check_wp_version() {
|
|
if ( version_compare( get_bloginfo( 'version' ), '6.5', '<' ) ) {
|
|
deactivate_plugins( MI_PLUGIN_BASENAME );
|
|
add_action( 'admin_notices', 'mi_wp_version_notice' );
|
|
}
|
|
}
|
|
add_action( 'admin_init', 'mi_check_wp_version' );
|
|
|
|
/**
|
|
* Display admin notice for WordPress version requirement.
|
|
*/
|
|
function mi_wp_version_notice() {
|
|
echo '<div class="error"><p>';
|
|
esc_html_e( 'Maple Icons has been deactivated. It requires WordPress 6.5 or higher.', 'maple-icons' );
|
|
echo '</p></div>';
|
|
}
|
|
|
|
/**
|
|
* Initialize the plugin.
|
|
*/
|
|
function mi_init() {
|
|
// Load text domain.
|
|
load_plugin_textdomain( 'maple-icons', false, dirname( MI_PLUGIN_BASENAME ) . '/languages' );
|
|
|
|
// Register the Gutenberg block.
|
|
mi_register_block();
|
|
}
|
|
add_action( 'init', 'mi_init' );
|
|
|
|
/**
|
|
* Register the Maple Icons Gutenberg block.
|
|
*/
|
|
function mi_register_block() {
|
|
// Check if build directory exists.
|
|
$block_path = MI_PLUGIN_DIR . 'build';
|
|
|
|
if ( ! file_exists( $block_path ) ) {
|
|
return;
|
|
}
|
|
|
|
register_block_type( $block_path );
|
|
}
|
|
|
|
/**
|
|
* Enqueue block editor assets.
|
|
*/
|
|
function mi_enqueue_block_assets() {
|
|
// Only load in block editor.
|
|
if ( ! is_admin() ) {
|
|
return;
|
|
}
|
|
|
|
$screen = get_current_screen();
|
|
if ( ! $screen || ! $screen->is_block_editor() ) {
|
|
return;
|
|
}
|
|
|
|
// Localize script data for the block editor.
|
|
$active_set = '';
|
|
$settings = get_option( 'maple_icons_settings', array() );
|
|
if ( ! empty( $settings['active_set'] ) ) {
|
|
$active_set = $settings['active_set'];
|
|
}
|
|
|
|
wp_localize_script(
|
|
'maple-icon-editor-script',
|
|
'miBlock',
|
|
array(
|
|
'ajaxUrl' => admin_url( 'admin-ajax.php' ),
|
|
'nonce' => wp_create_nonce( 'mi_block_nonce' ),
|
|
'activeSet' => $active_set,
|
|
'settingsUrl' => admin_url( 'options-general.php?page=maple-icons' ),
|
|
)
|
|
);
|
|
}
|
|
add_action( 'enqueue_block_editor_assets', 'mi_enqueue_block_assets' );
|
|
|
|
/**
|
|
* Initialize admin functionality.
|
|
*/
|
|
function mi_admin_init() {
|
|
// Initialize admin page.
|
|
new MI_Admin_Page();
|
|
|
|
// Initialize AJAX handler.
|
|
new MI_Ajax_Handler();
|
|
}
|
|
add_action( 'admin_init', 'mi_admin_init' );
|
|
|
|
/**
|
|
* Add settings link to plugin action links.
|
|
*
|
|
* @param array $links Plugin action links.
|
|
* @return array Modified action links.
|
|
*/
|
|
function mi_add_action_links( $links ) {
|
|
$settings_link = sprintf(
|
|
'<a href="%s">%s</a>',
|
|
admin_url( 'options-general.php?page=maple-icons' ),
|
|
__( 'Settings', 'maple-icons' )
|
|
);
|
|
array_unshift( $links, $settings_link );
|
|
return $links;
|
|
}
|
|
add_filter( 'plugin_action_links_' . MI_PLUGIN_BASENAME, 'mi_add_action_links' );
|
|
|
|
/**
|
|
* Declare WooCommerce HPOS compatibility.
|
|
*/
|
|
function mi_declare_hpos_compatibility() {
|
|
if ( class_exists( \Automattic\WooCommerce\Utilities\FeaturesUtil::class ) ) {
|
|
\Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility(
|
|
'custom_order_tables',
|
|
MI_PLUGIN_FILE,
|
|
true
|
|
);
|
|
}
|
|
}
|
|
add_action( 'before_woocommerce_init', 'mi_declare_hpos_compatibility' );
|