Skip to content

Commit

Permalink
finalized (CPT, migrate_db_to_cpt cleanup db, Rights, layout, cleanup…
Browse files Browse the repository at this point in the history
… on uninstall)
  • Loading branch information
thenickless committed Oct 4, 2024
1 parent 59b713e commit 539c373
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 62 deletions.
30 changes: 25 additions & 5 deletions includes/CPT.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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'),
));
Expand Down
4 changes: 2 additions & 2 deletions includes/CustomerDomains.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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 = [
Expand Down
67 changes: 30 additions & 37 deletions includes/Main.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);

Expand Down Expand Up @@ -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'],
Expand All @@ -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']),
Expand All @@ -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'],
Expand All @@ -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']),
Expand All @@ -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'],
Expand All @@ -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;
}


Expand All @@ -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']));
Expand All @@ -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()
{
Expand Down
21 changes: 8 additions & 13 deletions includes/Rights.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
10 changes: 5 additions & 5 deletions includes/Shortcode.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);

Expand Down Expand Up @@ -553,10 +551,12 @@ private static function display_hierarchical_categories_checkbox($categories, $l
$isChecked = in_array($category->ID, $aVal) ? 'checked' : '';
$ret .= '<label>' . $category->hierarchy_nbsp . '<input type="checkbox" name="categories[]" value="' . esc_attr($category->ID) . '" ' . $isChecked . ' />' . esc_html($category->post_title) . '</label>';
}
if ($ret){$ret .= '<br>';}
if ($ret) {
$ret .= '<br>';
}
echo $ret;
}

public function shortcode_list_handler(): string
{
$bUpdated = false;
Expand Down Expand Up @@ -688,7 +688,7 @@ public function shortcode_list_handler(): string

// Output table row
$table .= '<tr>';
$table .= '<td><a href="' . esc_url($long_url) . '">' . esc_html($long_url) . '</a></td>';
$table .= '<td class="column-long-url"><a href="' . esc_url($long_url) . '">' . esc_html($long_url) . '</a></td>';
$table .= '<td><a href="' . esc_url($short_url) . '+">' . esc_html($short_url) . '</a></td>';
$table .= '<td>' . esc_html($uri) . '</td>';
$table .= '<td>' . (!empty($valid_until) ? esc_html($valid_until) : __('indefinite', 'rrze-shorturl')) . '</td>';
Expand Down
2 changes: 2 additions & 0 deletions rrze-shorturl.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}

/**
Expand Down

0 comments on commit 539c373

Please sign in to comment.