135 lines
4.0 KiB
PHP
135 lines
4.0 KiB
PHP
<?php
|
|
|
|
namespace CropLogic;
|
|
|
|
if ( ! defined( 'ABSPATH' ) ) {
|
|
exit;
|
|
}
|
|
|
|
require_once CROP_LOGIC_PLUGIN_DIR . 'includes/class-settings.php';
|
|
require_once CROP_LOGIC_PLUGIN_DIR . 'includes/class-rest-controller.php';
|
|
|
|
class Plugin {
|
|
const MINIMUM_ELEMENTOR_VERSION = '3.1.0';
|
|
const MINIMUM_PHP_VERSION = '7.4';
|
|
|
|
/**
|
|
* @var Plugin|null
|
|
*/
|
|
private static $instance = null;
|
|
|
|
public static function instance(): Plugin {
|
|
if ( null === self::$instance ) {
|
|
self::$instance = new self();
|
|
}
|
|
|
|
return self::$instance;
|
|
}
|
|
|
|
private function __construct() {
|
|
add_action( 'init', [ $this, 'load_textdomain' ] );
|
|
add_action( 'plugins_loaded', [ $this, 'bootstrap' ] );
|
|
}
|
|
|
|
public function load_textdomain(): void {
|
|
load_plugin_textdomain( 'crop-logic', false, dirname( plugin_basename( CROP_LOGIC_PLUGIN_FILE ) ) . '/languages' );
|
|
}
|
|
|
|
public function bootstrap(): void {
|
|
Settings::instance();
|
|
Rest_Controller::instance();
|
|
|
|
if ( ! did_action( 'elementor/loaded' ) ) {
|
|
add_action( 'admin_notices', [ $this, 'admin_notice_missing_elementor' ] );
|
|
return;
|
|
}
|
|
|
|
if ( version_compare( ELEMENTOR_VERSION, self::MINIMUM_ELEMENTOR_VERSION, '<' ) ) {
|
|
add_action( 'admin_notices', [ $this, 'admin_notice_minimum_elementor_version' ] );
|
|
return;
|
|
}
|
|
|
|
if ( version_compare( PHP_VERSION, self::MINIMUM_PHP_VERSION, '<' ) ) {
|
|
add_action( 'admin_notices', [ $this, 'admin_notice_minimum_php_version' ] );
|
|
return;
|
|
}
|
|
|
|
add_action( 'elementor/elements/categories_registered', [ $this, 'register_category' ] );
|
|
add_action( 'elementor/widgets/register', [ $this, 'register_widgets' ] );
|
|
add_action( 'wp_enqueue_scripts', [ $this, 'register_assets' ] );
|
|
add_action( 'elementor/frontend/after_register_scripts', [ $this, 'register_assets' ] );
|
|
add_action( 'elementor/frontend/after_enqueue_styles', [ $this, 'register_assets' ] );
|
|
}
|
|
|
|
public function register_category( $elements_manager ): void {
|
|
$elements_manager->add_category(
|
|
'crop-logic',
|
|
[
|
|
'title' => __( 'Crop Logic', 'crop-logic' ),
|
|
'icon' => 'fa fa-plug',
|
|
]
|
|
);
|
|
}
|
|
|
|
public function register_assets(): void {
|
|
wp_register_style(
|
|
'crop-logic-ai-chat',
|
|
CROP_LOGIC_PLUGIN_URL . 'assets/css/ai-chat.css',
|
|
[],
|
|
CROP_LOGIC_VERSION
|
|
);
|
|
|
|
wp_register_script(
|
|
'crop-logic-ai-chat',
|
|
CROP_LOGIC_PLUGIN_URL . 'assets/js/ai-chat.js',
|
|
[],
|
|
CROP_LOGIC_VERSION,
|
|
true
|
|
);
|
|
}
|
|
|
|
public function register_widgets( $widgets_manager ): void {
|
|
require_once CROP_LOGIC_PLUGIN_DIR . 'includes/class-ai-chat-widget.php';
|
|
$widgets_manager->register( new AI_Chat_Widget() );
|
|
}
|
|
|
|
public function admin_notice_missing_elementor(): void {
|
|
$this->render_notice(
|
|
sprintf(
|
|
/* translators: 1: plugin name, 2: dependency name */
|
|
esc_html__( '"%1$s" requires "%2$s" to be installed and activated.', 'crop-logic' ),
|
|
'<strong>' . esc_html__( 'Crop Logic', 'crop-logic' ) . '</strong>',
|
|
'<strong>' . esc_html__( 'Elementor', 'crop-logic' ) . '</strong>'
|
|
)
|
|
);
|
|
}
|
|
|
|
public function admin_notice_minimum_elementor_version(): void {
|
|
$this->render_notice(
|
|
sprintf(
|
|
/* translators: 1: plugin name, 2: dependency name, 3: minimum version */
|
|
esc_html__( '"%1$s" requires "%2$s" version %3$s or greater.', 'crop-logic' ),
|
|
'<strong>' . esc_html__( 'Crop Logic', 'crop-logic' ) . '</strong>',
|
|
'<strong>' . esc_html__( 'Elementor', 'crop-logic' ) . '</strong>',
|
|
self::MINIMUM_ELEMENTOR_VERSION
|
|
)
|
|
);
|
|
}
|
|
|
|
public function admin_notice_minimum_php_version(): void {
|
|
$this->render_notice(
|
|
sprintf(
|
|
/* translators: 1: plugin name, 2: dependency name, 3: minimum version */
|
|
esc_html__( '"%1$s" requires "%2$s" version %3$s or greater.', 'crop-logic' ),
|
|
'<strong>' . esc_html__( 'Crop Logic', 'crop-logic' ) . '</strong>',
|
|
'<strong>' . esc_html__( 'PHP', 'crop-logic' ) . '</strong>',
|
|
self::MINIMUM_PHP_VERSION
|
|
)
|
|
);
|
|
}
|
|
|
|
private function render_notice( string $message ): void {
|
|
printf( '<div class="notice notice-warning is-dismissible"><p>%s</p></div>', wp_kses_post( $message ) );
|
|
}
|
|
}
|