diff --git a/includes/CPT.php b/includes/CPT.php index a206381..65a77cc 100644 --- a/includes/CPT.php +++ b/includes/CPT.php @@ -24,8 +24,12 @@ public function register_idm_cpt() 'name' => __('IDMs'), 'singular_name' => __('IDM'), ), - 'public' => true, + 'public' => false, 'has_archive' => false, + 'exclude_from_search' => true, + 'publicly_queryable' => false, + 'show_in_rest' => false, + 'rewrite' => false, 'supports' => array('title', 'editor', 'custom-fields'), )); } catch (CustomException $e) { @@ -42,8 +46,12 @@ public function register_domain_cpt() 'name' => __('Domains'), 'singular_name' => __('Domain'), ), - 'public' => true, + 'public' => false, 'has_archive' => false, + 'exclude_from_search' => true, + 'publicly_queryable' => false, + 'show_in_rest' => false, + 'rewrite' => false, 'supports' => array('title', 'custom-fields'), )); } catch (CustomException $e) { @@ -60,8 +68,12 @@ public function register_service_cpt() 'name' => __('Services'), 'singular_name' => __('Service'), ), - 'public' => true, + 'public' => false, 'has_archive' => false, + 'exclude_from_search' => true, + 'publicly_queryable' => false, + 'show_in_rest' => false, + 'rewrite' => false, 'supports' => array('title', 'custom-fields'), )); } catch (CustomException $e) { @@ -78,8 +90,12 @@ public function register_link_cpt() 'name' => __('Links'), 'singular_name' => __('Link'), ), - 'public' => true, + 'public' => false, 'has_archive' => false, + 'exclude_from_search' => true, + 'publicly_queryable' => false, + 'show_in_rest' => false, + 'rewrite' => false, 'supports' => array('title', 'editor', 'custom-fields'), )); } catch (CustomException $e) { @@ -96,8 +112,12 @@ public function register_category_cpt() 'name' => __('Short URL Categories'), 'singular_name' => __('Short URL Categories'), ), - 'public' => true, + 'public' => false, 'has_archive' => false, + 'exclude_from_search' => true, + 'publicly_queryable' => false, + 'show_in_rest' => false, + 'rewrite' => false, 'hierarchical' => true, // Makes this CPT hierarchical 'supports' => array('title', 'editor', 'custom-fields', 'page-attributes'), )); diff --git a/includes/CustomerDomains.php b/includes/CustomerDomains.php index 732785e..65a812f 100644 --- a/includes/CustomerDomains.php +++ b/includes/CustomerDomains.php @@ -12,7 +12,7 @@ public function __construct() add_action('rrze_shorturl_fetch_and_store_customerdomains', array($this, 'fetch_and_store_customerdomains')); // job has never run: do it immediately (like on plugin activation) - $this->fetch_and_store_customerdomains(); + // $this->fetch_and_store_customerdomains(); // let the job run daily as 4 a.m. wp_schedule_event(strtotime('today 4:00'), 'daily', 'rrze_shorturl_fetch_and_store_customerdomains'); @@ -22,7 +22,7 @@ public function __construct() public function fetch_and_store_customerdomains() { - error_log('fetch_and_store_customerdomains() ran'); + // error_log('fetch_and_store_customerdomains() ran'); // List of API URLs to fetch data from $aAPI_url = [ diff --git a/includes/Main.php b/includes/Main.php index b2398d9..b0843c6 100644 --- a/includes/Main.php +++ b/includes/Main.php @@ -44,6 +44,7 @@ public function onLoaded() add_action('wp_enqueue_scripts', [$this, 'enqueueScripts']); add_action('admin_enqueue_scripts', [$this, 'enqueueScripts']); add_action('init', [$this, 'migrate_db_to_cpt']); + // add_action('init', [$this, 'drop_shorturl_tables']); add_action('init', [$this, 'initialize_services']); add_action('init', [$this, 'init_query_dependend_classes']); @@ -134,12 +135,12 @@ public function migrate_db_to_cpt() global $wpdb; // Migrate shorturl_idms to CPT 'idm' - $idm_ids = ['post_id' => 0]; + $idm_ids = []; $idms = $wpdb->get_results("SELECT * FROM {$wpdb->prefix}shorturl_idms", ARRAY_A); - foreach ($idms as $nr => $idm) { + foreach ($idms as $idm) { // Check if the IDM already exists as a post - $existing_idm_id = get_posts( + $post_id = get_posts( array( 'post_type' => 'shorturl_idm', 'title' => $idm['idm'], @@ -149,7 +150,7 @@ public function migrate_db_to_cpt() ) ); - if (empty($existing_idm_id)) { + if (empty($post_id)) { // Insert IdM as a CPT post $post_data = [ 'post_title' => sanitize_text_field($idm['idm']), @@ -165,18 +166,18 @@ public function migrate_db_to_cpt() update_post_meta($post_id, 'allow_get', intval($idm['allow_get'])); update_post_meta($post_id, 'allow_utm', intval($idm['allow_utm'])); update_post_meta($post_id, 'created_by', sanitize_text_field($idm['created_by'])); - $idm_ids[$nr]['post_id'] = $post_id; } } + $idm_ids[$idm['id']] = $post_id; } // Migrate shorturl_domains to CPT 'domain' - $domain_ids = ['post_id' => 0]; + $domain_ids = []; $domains = $wpdb->get_results("SELECT * FROM {$wpdb->prefix}shorturl_domains", ARRAY_A); - foreach ($domains as $nr => $domain) { + foreach ($domains as $domain) { // Check if the domain already exists as a post - $existing_domain_id = get_posts( + $post_id = get_posts( array( 'post_type' => 'shorturl_domain', 'title' => $domain['hostname'], @@ -186,7 +187,7 @@ public function migrate_db_to_cpt() ) ); - if (empty($existing_domain_id)) { + if (empty($post_id)) { // Insert domain as a CPT post $post_data = [ 'post_title' => sanitize_text_field($domain['hostname']), @@ -204,19 +205,19 @@ public function migrate_db_to_cpt() update_post_meta($post_id, 'notice', sanitize_text_field($domain['notice'])); update_post_meta($post_id, 'webmaster_name', sanitize_text_field($domain['webmaster_name'])); update_post_meta($post_id, 'webmaster_email', sanitize_email($domain['webmaster_email'])); - $domain_ids[$nr]['post_id'] = $post_id; } } + $domain_ids[$domain['id']] = $post_id; } // Migrate shorturl_categories to CPT 'shorturl_category' $category_ids = []; - $categories = $wpdb->get_results("SELECT * FROM {$wpdb->prefix}shorturl_categories", ARRAY_A); + $categories = $wpdb->get_results("SELECT * FROM {$wpdb->prefix}shorturl_categories ORDER BY id", ARRAY_A); - foreach ($categories as $nr => $category) { + foreach ($categories as $category) { // Check if the category already exists as a post - $existing_category_id = get_posts( + $post_id = get_posts( array( 'post_type' => 'shorturl_category', 'title' => $category['label'], @@ -226,28 +227,23 @@ public function migrate_db_to_cpt() ) ); - if (empty($existing_category_id)) { + if (empty($post_id)) { // Insert category as a CPT post $post_data = [ 'post_title' => sanitize_text_field($category['label']), 'post_type' => 'shorturl_category', 'post_status' => 'publish', - 'post_parent' => !empty($category['parent_id']) ? intval($category_ids[$category['parent_id']]['post_id']) : 0 // Set parent category if applicable + 'post_parent' => !empty($category['parent_id']) ? intval($category_ids[$category['parent_id']]) : 0 // Set parent category if applicable ]; $post_id = wp_insert_post($post_data); if (!is_wp_error($post_id)) { // Add meta fields - update_post_meta($post_id, 'idm_id', intval($category['idm_id'])); - - // Track the new post ID for reference (especially for parent-child relationships) - $category_ids[$category['id']]['post_id'] = $post_id; + update_post_meta($post_id, 'idm_id', intval($idm_ids[$category['idm_id']])); } - } else { - // If category already exists, store its ID for future reference - $category_ids[$category['id']]['post_id'] = $existing_category_id[0]; } + $category_ids[$category['id']] = $post_id; } @@ -266,11 +262,11 @@ public function migrate_db_to_cpt() if (!is_wp_error($post_id)) { // Add meta fields - update_post_meta($post_id, 'domain_id', $domain_ids[$link['domain_id']]['post_id']); + update_post_meta($post_id, 'domain_id', $domain_ids[$link['domain_id']]); + update_post_meta($post_id, 'idm_id', $idm_ids[$link['idm_id']]); update_post_meta($post_id, 'long_url', esc_url($link['long_url'])); update_post_meta($post_id, 'short_url', esc_url($link['short_url'])); update_post_meta($post_id, 'uri', sanitize_text_field($link['uri'])); - update_post_meta($post_id, 'idm_id', $idm_ids[$link['idm_id']]['post_id']); update_post_meta($post_id, 'created_at', sanitize_text_field($link['created_at'])); update_post_meta($post_id, 'updated_at', sanitize_text_field($link['updated_at'])); update_post_meta($post_id, 'deleted_at', sanitize_text_field($link['deleted_at'])); @@ -283,26 +279,23 @@ public function migrate_db_to_cpt() $link['id'] ), ARRAY_A); - // Collect all category post IDs - $category_post_ids = []; - foreach ($link_categories as $link_category) { - if (isset($category_ids[$link_category['category_id']])) { - $category_post_ids[] = $category_ids[$link_category['category_id']]['post_id']; - } - } - - // Save all category post IDs as an array in the post meta - if (!empty($category_post_ids)) { - update_post_meta($post_id, 'category_id', $category_post_ids); + // add all categories to link + foreach ($link_categories as $category) { + add_post_meta($post_id, 'category_id', $category_ids[$category['category_id']], false); } } } - // $this->drop_custom_tables(); - update_option('rrze_shorturl_migration_completed', true); } + public function drop_shorturl_tables(){ + if (get_option('rrze_shorturl_custom_tables_dropped')) { + return; + } + $this->drop_custom_tables(); + update_option('rrze_shorturl_custom_tables_dropped', true); + } public function initialize_services() { diff --git a/includes/Rights.php b/includes/Rights.php index 12cb892..b2bb487 100644 --- a/includes/Rights.php +++ b/includes/Rights.php @@ -41,27 +41,22 @@ public function getRights(): array $args = [ 'post_type' => 'shorturl_idm', // The Custom Post Type for IDMs 'posts_per_page' => 1, // We only need one result - 'meta_query' => [ - [ - 'key' => 'idm', - 'value' => $this->idm, - 'compare' => '=' - ] - ] + 'fields' => 'ids', + 'name' => sanitize_title($this->idm) ]; // Execute the query $query = new \WP_Query($args); // Check if a matching IDM post was found - if ($query->have_posts()) { - $query->the_post(); + if (!empty($query->posts)) { + $post_id = $query->posts[0]; // Fetch the rights from the post meta - $aRet['id'] = get_the_ID(); - $aRet['allow_uri'] = (bool) get_post_meta(get_the_ID(), 'allow_uri', true); - $aRet['allow_get'] = (bool) get_post_meta(get_the_ID(), 'allow_get', true); - $aRet['allow_utm'] = (bool) get_post_meta(get_the_ID(), 'allow_utm', true); + $aRet['id'] = $post_id; + $aRet['allow_uri'] = (bool) get_post_meta($post_id, 'allow_uri', true); + $aRet['allow_get'] = (bool) get_post_meta($post_id, 'allow_get', true); + $aRet['allow_utm'] = (bool) get_post_meta($post_id, 'allow_utm', true); // Restore original Post Data wp_reset_postdata(); diff --git a/includes/Shortcode.php b/includes/Shortcode.php index 314db15..f3b88d2 100644 --- a/includes/Shortcode.php +++ b/includes/Shortcode.php @@ -195,7 +195,6 @@ public function shortcode_categories_handler(): string private static function get_categories_hierarchically() { - $ret = []; // Fetch all categories for the current IdM using get_posts @@ -215,7 +214,6 @@ private static function get_categories_hierarchically() // WP_Query statt get_posts verwenden, um die Hierarchie besser zu verarbeiten $query = new \WP_Query($args); - $categories = $query->posts; $ret = self::build_category_hierarchy($query->posts); @@ -553,10 +551,12 @@ private static function display_hierarchical_categories_checkbox($categories, $l $isChecked = in_array($category->ID, $aVal) ? 'checked' : ''; $ret .= ''; } - if ($ret){$ret .= '
';} + if ($ret) { + $ret .= '
'; + } echo $ret; } - + public function shortcode_list_handler(): string { $bUpdated = false; @@ -688,7 +688,7 @@ public function shortcode_list_handler(): string // Output table row $table .= ''; - $table .= '' . esc_html($long_url) . ''; + $table .= '' . esc_html($long_url) . ''; $table .= '' . esc_html($short_url) . ''; $table .= '' . esc_html($uri) . ''; $table .= '' . (!empty($valid_until) ? esc_html($valid_until) : __('indefinite', 'rrze-shorturl')) . ''; diff --git a/rrze-shorturl.php b/rrze-shorturl.php index 7ede769..fc5477e 100644 --- a/rrze-shorturl.php +++ b/rrze-shorturl.php @@ -127,6 +127,8 @@ function deactivation() // delete our options delete_option('rrze_shorturl_option'); delete_option('rrze_shorturl_services_initialized'); + delete_option('rrze_shorturl_migration_completed'); + delete_option('rrze_shorturl_custom_tables_dropped'); } /**