Plugin Directory

Changeset 3397401


Ignore:
Timestamp:
11/17/2025 04:40:49 PM (5 months ago)
Author:
eugenezolo
Message:

Release 1.0.3 - Add custom duplicate slugs handling

Location:
outrank/trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • outrank/trunk/libs/api.php

    r3364907 r3397401  
    4646        'methods' => 'POST',
    4747        'callback' => 'outrank_receive_article',
    48         'permission_callback' => function () {
    49             $headers = getallheaders();
    50             $secretKey = $headers['X-Secret-Key'] ?? $headers['x-secret-key'] ?? null;
     48        'permission_callback' => function ($request) {
     49            $secretKey = $request->get_header('X-Secret-Key');
     50            if (!$secretKey) {
     51                $secretKey = $request->get_header('x-secret-key');
     52            }
    5153            return $secretKey && hash_equals($secretKey, OUTRANK_API_SECRET);
    5254        }
     
    140142    }
    141143
     144    // Check if slug exists in custom table and generate unique one if needed
     145    $unique_slug = $slug;
     146    $suffix = 2;
     147    $max_attempts = 10;
     148
     149    while ($suffix <= $max_attempts) {
     150        // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery
     151        $existing_in_custom = $wpdb->get_var(
     152            $wpdb->prepare("SELECT COUNT(*) FROM {$table_name} WHERE slug = %s", $unique_slug)
     153        );
     154
     155        // Check if slug exists in WordPress posts
     156        $existing_in_wp = get_page_by_path($unique_slug, OBJECT, 'post');
     157
     158        if ($existing_in_custom == 0 && !$existing_in_wp) {
     159            break; // Slug is unique in both tables
     160        }
     161
     162        // Slug exists, try next suffix
     163        $unique_slug = $slug . '-' . $suffix;
     164        $suffix++;
     165    }
     166
     167    // If we couldn't find a unique slug after max attempts, return error
     168    if ($suffix > $max_attempts) {
     169        return new WP_REST_Response([
     170            'error' => 'Too many posts with the same slug. Please use a different slug.'
     171        ], 409);
     172    }
     173
    142174    remove_filter('content_save_pre', 'wp_filter_post_kses');
    143175
    144176    $sanitized_content = sanitize_content($params['content'] ?? '');
    145177
    146     // Insert post
     178    // Insert post with the unique slug
    147179    $post_id = wp_insert_post([
    148180        'post_title'    => $title,
     
    150182        'post_status'   => get_option('outrank_post_as_draft', 'yes') === 'yes' ? 'draft' : 'publish',
    151183        'post_type'     => 'post',
    152         'post_name'     => $slug,
     184        'post_name'     => $unique_slug,
    153185        'post_category' => $category_ids,
    154186        'tags_input'    => isset($params['tags']) ? array_map('sanitize_text_field', $params['tags']) : [],
     
    162194    }
    163195
    164     // Get the actual slug WordPress assigned (handles duplicates with -2, -3, etc.)
    165     $actual_slug = get_post_field('post_name', $post_id);
     196    // Get the final slug and status
     197    $final_slug = get_post_field('post_name', $post_id);
    166198    $post_status = get_post_field('post_status', $post_id);
    167199
     
    170202    $inserted = $wpdb->insert($table_name, [
    171203        'image'            => $imageId,
    172         'slug'             => $actual_slug,  // Use WordPress-generated slug
     204        'slug'             => $final_slug,  // Use unique slug
    173205        'title'            => $title,
    174206        'meta_description' => sanitize_text_field($params['meta_description'] ?? ''),
  • outrank/trunk/outrank.php

    r3364907 r3397401  
    66 * Plugin URI: https://outrank.so
    77 * Description: Get traffic and outrank competitors with automatic SEO-optimized content generation published to your WordPress site.
    8  * Version: 1.0.2
     8 * Version: 1.0.3
    99 * Author: Outrank
    1010 * License: GPLv2 or later
  • outrank/trunk/readme.txt

    r3364907 r3397401  
    55Tested up to: 6.8 
    66Requires PHP: 8.0 
    7 Stable tag: 1.0.2 
     7Stable tag: 1.0.3 
    88License: GPLv2 or later 
    99License URI: https://www.gnu.org/licenses/gpl-2.0.html 
     
    7474== Changelog ==
    7575
     76= 1.0.3 =
     77* Add custom duplicate slugs handling
     78
    7679= 1.0.2 =
    7780* Fixed YouTube video embedding in synced articles
Note: See TracChangeset for help on using the changeset viewer.