Advertisement
JoachimBrnd

Relation counter for Voxel

Aug 27th, 2024
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.98 KB | None | 0 0
  1. function register_relation_dynamic_tags($dynamic_tags_manager) {
  2.     global $wpdb;
  3.  
  4.     $results = $wpdb->get_col("
  5.         SELECT DISTINCT relation_key
  6.         FROM {$wpdb->prefix}voxel_relations
  7.     ");
  8.  
  9.     if (!empty($results) && !is_wp_error($results)) {
  10.         foreach ($results as $relation_key) {
  11.             $file_name = 'relation-' . sanitize_title($relation_key) . '-tag.php';
  12.             $file_path = __DIR__ . '/dynamic-tags/' . $file_name;
  13.            
  14.             if (file_exists($file_path)) {
  15.                 require_once($file_path);
  16.                 $class_name = 'Elementor_Relation_' . str_replace('-', '_', sanitize_title($relation_key)) . '_Tag';
  17.                 if (class_exists($class_name)) {
  18.                     $dynamic_tags_manager->register(new $class_name());
  19.                 }
  20.             }
  21.         }
  22.     }
  23. }
  24. add_action('elementor/dynamic_tags/register', 'register_relation_dynamic_tags');
  25.  
  26. function create_relation_dynamic_tag_files() {
  27.     global $wpdb;
  28.  
  29.     $results = $wpdb->get_col("
  30.         SELECT DISTINCT relation_key
  31.         FROM {$wpdb->prefix}voxel_relations
  32.     ");
  33.  
  34.     if (!empty($results) && !is_wp_error($results)) {
  35.         $dynamic_tags_dir = __DIR__ . '/dynamic-tags';
  36.        
  37.         if (!file_exists($dynamic_tags_dir)) {
  38.             wp_mkdir_p($dynamic_tags_dir);
  39.         }
  40.  
  41.         foreach ($results as $relation_key) {
  42.             $file_name = 'relation-' . sanitize_title($relation_key) . '-tag.php';
  43.             $file_path = $dynamic_tags_dir . '/' . $file_name;
  44.            
  45.             if (!file_exists($file_path)) {
  46.                 $class_name = 'Elementor_Relation_' . str_replace('-', '_', sanitize_title($relation_key)) . '_Tag';
  47.                 $content = "<?php
  48. if (!defined('ABSPATH')) {
  49.     exit; // Exit if accessed directly
  50. }
  51.  
  52. class {$class_name} extends \\Elementor\\Core\\DynamicTags\\Tag {
  53.     public function get_name() {
  54.         return 'relation-" . sanitize_title($relation_key) . "';
  55.     }
  56.  
  57.     public function get_title() {
  58.         return __('" . ucwords(str_replace('-', ' ', $relation_key)) . " Relation', 'your-text-domain');
  59.     }
  60.  
  61.     public function get_group() {
  62.         return 'post';
  63.     }
  64.  
  65.     public function get_categories() {
  66.         return ['text'];
  67.     }
  68.  
  69.     protected function render() {
  70.         global \$post, \$wpdb;
  71.  
  72.         if (!\$post) return;
  73.  
  74.         \$count = \$wpdb->get_var(\$wpdb->prepare(\"
  75.            SELECT COUNT(*)
  76.            FROM {\$wpdb->prefix}voxel_relations
  77.            WHERE (parent_id = %d OR child_id = %d) AND relation_key = %s
  78.        \", \$post->ID, \$post->ID, '{$relation_key}'));
  79.  
  80.        echo \$count !== null ? \$count : '0';
  81.    }
  82. }";
  83.                 $result = file_put_contents($file_path, $content);
  84.                 if ($result === false) {
  85.                     error_log("Failed to create dynamic tag file: $file_path");
  86.                 }
  87.             }
  88.         }
  89.     }
  90. }
  91. add_action('init', 'create_relation_dynamic_tag_files');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement