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') . '

'; } else { echo '

' . esc_html__('Error deleting Affiliate Block.', 'affiliblocks') . '

'; } } $cache_key = 'affiliblocks_shortcode_all'; $shortcodes = wp_cache_get($cache_key, 'affiliblocks'); if ($shortcodes === false) { // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery $shortcodes = $wpdb->get_results("SELECT * FROM {$this->table_name} ORDER BY id DESC"); wp_cache_set($cache_key, $shortcodes, 'affiliblocks'); } echo '
'; echo '

' . esc_html__('Affiliate Blocks', 'affiliblocks') . '

'; echo '' . esc_html__('Add New', 'affiliblocks') . ''; if (!empty($shortcodes)) { echo ''; echo ''; echo ''; foreach ($shortcodes as $shortcode) { $delete_url = wp_nonce_url( admin_url('admin.php?page=affili-blocks&delete=' . $shortcode->id), 'affili_delete_nonce' ); echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; } echo '
' . 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) . '' . esc_html($shortcode->title) . '' . esc_html($shortcode->subtitle) . '' . esc_html($shortcode->award_type) . '' . esc_html($shortcode->overall_score) . ' ' . esc_html__('Edit', 'affiliblocks') . ' | ' . esc_html__('Delete', 'affiliblocks') . '
'; } else { echo '

' . esc_html__('No blocks found. Add your first one.', 'affiliblocks') . '

'; } echo '
'; } /** * Add/Edit tent page */ public function affili_add_tent_page() { global $wpdb; // Handle form submission if (isset($_POST['submit_tent']) && check_admin_referer('affili_edit_nonce')) { // Validate and sanitize data $data = array( 'title' => sanitize_text_field($_POST['title']), 'subtitle' => sanitize_text_field($_POST['subtitle']), 'award_type' => sanitize_text_field($_POST['award_type']), 'award_title' => sanitize_text_field($_POST['award_title']), 'image_url' => esc_url_raw($_POST['image_url']), 'price' => sanitize_text_field($_POST['price']), 'post_url' => esc_url_raw($_POST['post_url']), 'overall_score' => floatval($_POST['overall_score']), 'space_comfort_score' => floatval($_POST['space_comfort_score']), 'weather_resistance_score' => floatval($_POST['weather_resistance_score']), 'ease_of_use_score' => floatval($_POST['ease_of_use_score']), 'family_friendliness_score' => floatval($_POST['family_friendliness_score']), 'quality_score' => floatval($_POST['quality_score']), 'pros' => sanitize_textarea_field($_POST['pros']), 'cons' => sanitize_textarea_field($_POST['cons']), 'description' => wp_kses_post($_POST['description']), 'specifications' => "-" ); // Validate required fields $errors = array(); if (empty($data['title'])) $errors[] = __('Title is required', 'affiliblocks'); if (empty($data['post_url'])) $errors[] = __('Post URL is required', 'affiliblocks'); if (empty($errors)) { if (isset($_GET['edit'])) { // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery $result = $wpdb->update( $this->table_name, $data, array('id' => intval($_GET['edit'])), array( '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%f', '%f', '%f', '%f', '%f', '%f', '%s', '%s', '%s', '%s' ), array('%d') ); if (false !== $result) { echo '

' . esc_html__('Affiliate Block updated successfully!', 'affiliblocks') . '

'; } else { echo '

' . esc_html__('Error updating Affiliate Block.', 'affiliblocks') . '

'; } } else { // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery $result = $wpdb->insert( $this->table_name, $data, array( '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%f', '%f', '%f', '%f', '%f', '%f', '%s', '%s', '%s', '%s' ) ); if ($result) { echo '

' . esc_html__('Affiliate Block added successfully!', 'affiliblocks') . '

'; } else { echo '

' . esc_html__('Error adding Affiliate Block.', 'affiliblocks') . '

'; } } } else { echo '

' . esc_html(implode('
', $errors)) . '

'; } } // Get tent data if editing $shortcode = null; if (isset($_GET['edit'])) { $cache_key = 'affiliblocks_shortcode_' . intval($_GET['edit']); $shortcode = wp_cache_get($cache_key, 'affiliblocks'); if ($shortcode === false) { // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery $shortcode = $wpdb->get_row( $wpdb->prepare("SELECT * FROM {$this->table_name} WHERE id = %d", intval($_GET['edit'])) ); wp_cache_set($cache_key, $shortcode, 'affiliblocks'); } } echo '
'; echo '

' . (isset($_GET['edit']) ? esc_html__('Edit Affiliate Block', 'affiliblocks') : esc_html__('Add New Affiliate Block', 'affiliblocks')) . '

'; echo '
'; wp_nonce_field('affili_edit_nonce'); // Main container with columns echo '
'; echo '
'; // Left column - Basic Information echo '
'; echo '

' . esc_html__('Basic Information', 'affiliblocks') . '


'; echo '
'; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo '
'; echo '
'; // .inside echo '
'; // .postbox // Left column - Description echo '
'; echo '

' . esc_html__('Description', 'affiliblocks') . '


'; echo '
'; wp_editor( $shortcode ? wp_kses_post($shortcode->description) : '', 'description', array( 'textarea_rows' => 15, 'media_buttons' => true, 'tinymce' => array( 'toolbar1' => 'formatselect,bold,italic,underline,strikethrough,forecolor,backcolor,fontsizeselect,fontselect,alignleft,aligncenter,alignright,alignjustify,bullist,numlist,outdent,indent,link,unlink,wp_more,spellchecker,fullscreen,wp_adv,table', 'toolbar2' => 'hr,subscript,superscript,removeformat,charmap,code,undo,redo', ), 'quicktags' => true, ) ); echo '
'; // .inside echo '
'; // .postbox echo '
'; // .postbox-container (left column) // Right column - Scores and Pros/Cons echo '
'; // Right column - Scores echo '
'; echo '

' . esc_html__('Scores', 'affiliblocks') . '


'; echo '
'; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo '
'; echo '
'; // .inside echo '
'; // .postbox // Right column - Pros & Cons echo '
'; echo '

' . esc_html__('Pros & Cons', 'affiliblocks') . '


'; echo '
'; echo ''; echo ''; echo ''; echo '
'; echo '
'; // Submit button at bottom echo '

'; submit_button(esc_html__('Save', 'affiliblocks'), 'primary', 'submit_tent', false, array( 'style' => 'padding:5px 30px;' )); echo '  '; echo '' . esc_html__('Cancel', 'affiliblocks') . ''; echo '

'; echo '
'; // .inside echo '
'; // .postbox echo '
'; // .postbox-container (right column) echo '
'; // .metabox-holder echo '
'; echo '
'; // .wrap // Enqueue media uploader wp_enqueue_media(); } /** * function for admin scripts * @return void */ public function affili_admin_scripts($hook_suffix) { wp_enqueue_script( 'affiliblocks-admin', plugin_dir_url(__FILE__) . 'js/affiliblocks.js', array('jquery', 'wp-i18n', 'media-editor'), '1.0', true ); } /** * Shortcode to display products */ public function affili_display_tent_shortcode($atts) { global $wpdb; $atts = shortcode_atts(array( 'id' => 0 ), $atts, 'affili-block'); $cache_key = 'affiliblocks_shortcode_' . intval($atts['id']); $shortcode = wp_cache_get($cache_key, 'affiliblocks'); if ($shortcode === false) { // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery $shortcode = $wpdb->get_row( $wpdb->prepare("SELECT * FROM {$this->table_name} WHERE id = %d", intval($atts['id'])) ); wp_cache_set($cache_key, $shortcode, 'affiliblocks'); } if (!$shortcode) { return '

' . esc_html__('Affiliate block not found.', 'affiliblocks') . '

'; } $selected_url = AFFILI_PLUGIN_URL . 'assets/images/' . $this->affili_create_slug($shortcode->award_type) . '.jpg'; ob_start(); ?>

title); ?>

subtitle); ?>

<?php echo esc_attr($shortcode->award_type); ?>
overall_score) > 0): ?>
overall_score); ?>
SCORE', 'affiliblocks') ); ?>
  • space_comfort_score); ?>
  • weather_resistance_score); ?>
  • ease_of_use_score); ?>
  • family_friendliness_score); ?>
  • quality_score); ?>
pros) != ''): ?>
pros); foreach ($pros as $pro) { if (!empty(trim($pro))) { echo '
' . esc_html(trim($pro)) . '
'; } } ?>
cons) != ''): ?>
cons); foreach ($cons as $con) { if (!empty(trim($con))) { echo '
' . esc_html(trim($con)) . '
'; } } ?>
description)); ?>