table_name = $wpdb->prefix . 'affili_blocks'; // Initialize plugin add_action('init', array($this, 'affili_init')); add_action('admin_menu', array($this, 'affili_add_admin_menu')); add_action('wp_enqueue_scripts', array($this, 'affili_enqueue_scripts')); add_action('admin_enqueue_scripts', array($this, 'affili_admin_scripts')); // Register activation/deactivation hooks register_activation_hook(__FILE__, array($this, 'affili_activate')); register_deactivation_hook(__FILE__, array($this, 'affili_deactivate')); register_uninstall_hook(__FILE__, array('Affiliblocks', 'affili_uninstall')); // Register shortcode add_shortcode('affili-block', array($this, 'affili_display_tent_shortcode')); } /** * Initialize plugin */ public function affili_init() { // Text domain loading is handled automatically by WordPress } /** * Plugin activation */ public function affili_activate() { $this->affili_create_database_table(); flush_rewrite_rules(); } /** * Plugin deactivation */ public function affili_deactivate() { flush_rewrite_rules(); } /** * Plugin uninstallation */ public static function affili_uninstall() { global $wpdb; // Remove database table $table_name = $wpdb->prefix . 'affili_blocks'; $cache_key = $table_name.'_db_exists'; $table_exists = wp_cache_get( $cache_key ); if ( false === $table_exists ) { // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery $table_exists = $wpdb->get_var( $wpdb->prepare( "SHOW TABLES LIKE %s", $table_name ) ); // Cache the result to avoid future database hits wp_cache_set( $cache_key, $table_exists ); } // Create table 1 if it doesn't exist if ( $table_exists ) { // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery $wpdb->query($wpdb->prepare("DROP TABLE IF EXISTS %s", $table_name)); } // Remove any options if they exist delete_option('affili_version'); } /** * Create database table */ private function affili_create_database_table() { global $wpdb; $charset_collate = $wpdb->get_charset_collate(); $sql = "CREATE TABLE {$this->table_name} ( id mediumint(9) NOT NULL AUTO_INCREMENT, title varchar(255) NOT NULL, subtitle varchar(255) NOT NULL, award_type varchar(100) NOT NULL, award_title varchar(255) NOT NULL, image_url varchar(255) NOT NULL, post_url varchar(255) NOT NULL, price varchar(100) NOT NULL, overall_score float NOT NULL, space_comfort_score float NOT NULL, weather_resistance_score float NOT NULL, ease_of_use_score float NOT NULL, family_friendliness_score float NOT NULL, quality_score float NOT NULL, pros text NOT NULL, cons text NOT NULL, description text NOT NULL, specifications text NOT NULL, created_at datetime DEFAULT CURRENT_TIMESTAMP, updated_at datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, PRIMARY KEY (id) ) $charset_collate;"; require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); dbDelta($sql); // Store plugin version for future updates update_option('affili_version', AFFILI_VERSION); } /** * Add admin menu */ public function affili_add_admin_menu() { add_menu_page( esc_html__('Affiliate Blocks', 'affiliblocks'), esc_html__('Affiliate Blocks', 'affiliblocks'), 'manage_options', 'affili-blocks', array($this, 'affili_admin_page'), 'dashicons-store', 20 ); add_submenu_page( 'affili-blocks', esc_html__('Add New', 'affiliblocks'), esc_html__('Add New', 'affiliblocks'), 'manage_options', 'affili-blocks-add', array($this, 'affili_add_tent_page') ); } /** * Admin page to view all shortcodes */ public function affili_admin_page() { global $wpdb; // Handle delete if (isset($_GET['delete']) && check_admin_referer('affili_delete_nonce')) { $delete_id = intval($_GET['delete']); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery $result = $wpdb->delete($this->table_name, array('id' => $delete_id), array('%d')); if ($result) { echo '
' . esc_html__('Affiliate Block deleted successfully!', 'affiliblocks') . '
' . esc_html__('Error deleting Affiliate Block.', 'affiliblocks') . '
| ' . esc_html__('ID', 'affiliblocks') . ' | ' . esc_html__('Title', 'affiliblocks') . ' | ' . esc_html__('Subtitle', 'affiliblocks') . ' | ' . esc_html__('Award', 'affiliblocks') . ' | ' . esc_html__('Score', 'affiliblocks') . ' | ' . esc_html__('Shortcode', 'affiliblocks') . ' | ' . esc_html__('Actions', 'affiliblocks') . ' |
|---|---|---|---|---|---|---|
| ' . esc_html($shortcode->id) . ' | '; echo '' . esc_html($shortcode->title) . ' | '; echo '' . esc_html($shortcode->subtitle) . ' | '; echo '' . esc_html($shortcode->award_type) . ' | '; echo '' . esc_html($shortcode->overall_score) . ' | '; echo ''; echo ' | ' . esc_html__('Edit', 'affiliblocks') . ' | ' . esc_html__('Delete', 'affiliblocks') . ' | '; echo '
' . esc_html__('No blocks found. Add your first one.', 'affiliblocks') . '
'; } echo '' . esc_html__('Affiliate Block updated successfully!', 'affiliblocks') . '
' . esc_html__('Error updating Affiliate Block.', 'affiliblocks') . '
' . esc_html__('Affiliate Block added successfully!', 'affiliblocks') . '
' . esc_html__('Error adding Affiliate Block.', 'affiliblocks') . '
' . esc_html(implode('
', $errors)) . '
' . esc_html__('Affiliate block not found.', 'affiliblocks') . '
'; } $selected_url = AFFILI_PLUGIN_URL . 'assets/images/' . $this->affili_create_slug($shortcode->award_type) . '.jpg'; ob_start(); ?>