Files
beep/includes/class-beep.php
T

113 lines
3.7 KiB
PHP
Executable File

<?php
if (!defined('ABSPATH')) {
exit;
}
class Beep_Plugin {
public static function init() {
Beep_CPT::init();
Beep_Likes::init();
Beep_Replies::init();
Beep_Moderation::init();
Beep_Polls::init();
Beep_Shortcode::init();
add_action('wp_enqueue_scripts', [__CLASS__, 'enqueue_assets']);
// Elementor widget — only load if Elementor is active
add_action('elementor/widgets/register', function ($widgets_manager) {
if (!class_exists('\Elementor\Widget_Base')) return;
$widget_file = BEEP_DIR . 'includes/class-elementor-widget.php';
if (!file_exists($widget_file)) return;
require_once $widget_file;
if (class_exists('Beep_Elementor_Widget')) {
$widgets_manager->register(new Beep_Elementor_Widget());
}
}, 5);
// Admin settings (Giphy API key only — full settings via Beep_Settings)
add_action('admin_menu', [__CLASS__, 'add_admin_menu']);
add_action('admin_init', [__CLASS__, 'register_settings']);
}
public static function add_admin_menu() {
add_options_page(
'Beep Settings',
'Beep',
'manage_options',
'beep-settings',
[__CLASS__, 'render_settings_page']
);
}
public static function register_settings() {
register_setting('beep_settings_group', 'beep_giphy_key', [
'sanitize_callback' => 'sanitize_text_field',
]);
add_settings_section('beep_main_section', 'API Configuration', null, 'beep-settings');
add_settings_field(
'beep_giphy_key',
'Giphy API Key',
[__CLASS__, 'giphy_key_field'],
'beep-settings',
'beep_main_section',
['label_for' => 'beep_giphy_key']
);
}
public static function giphy_key_field() {
$key = get_option('beep_giphy_key', '');
echo '<input type="text" id="beep_giphy_key" name="beep_giphy_key" value="' . esc_attr($key) . '" class="regular-text" placeholder="Enter your Giphy API key">';
echo '<p class="description">Get a free API key at <a href="https://developers.giphy.com/" target="_blank">developers.giphy.com</a></p>';
}
public static function render_settings_page() {
if (!current_user_can('manage_options')) return;
echo '<div class="wrap"><h1>Beep Settings</h1>';
echo '<form method="post" action="options.php">';
settings_fields('beep_settings_group');
do_settings_sections('beep-settings');
submit_button();
echo '</form></div>';
}
public static function activate() {
Beep_CPT::register();
Beep_Likes::create_table();
Beep_Polls::create_tables();
Beep_Replies::create_tables();
flush_rewrite_rules();
}
public static function deactivate() {
flush_rewrite_rules();
}
public static function enqueue_assets() {
wp_enqueue_style(
'beep',
BEEP_URL . 'assets/beep.css',
[],
BEEP_VERSION
);
wp_enqueue_script(
'beep',
BEEP_URL . 'assets/beep.js',
[],
BEEP_VERSION,
true
);
$giphy_key = get_option('beep_giphy_key', '');
wp_localize_script('beep', 'BeepConfig', [
'restUrl' => esc_url_raw(rest_url('beep/v1/')),
'nonce' => wp_create_nonce('wp_rest'),
'charLimit' => BEEP_CHAR_LIMIT,
'loginUrl' => esc_url_raw(wp_login_url(is_singular() ? get_permalink() : home_url('/'))),
'isLoggedIn' => is_user_logged_in(),
'giphyKey' => $giphy_key,
]);
}
}