Plugin Directory

Changeset 3236026


Ignore:
Timestamp:
02/06/2025 12:58:13 PM (14 months ago)
Author:
topirank
Message:

Release version 1.0.5

Location:
topirank-integration
Files:
32 added
7 edited

Legend:

Unmodified
Added
Removed
  • topirank-integration/trunk/includes/create-template.php

    r3220350 r3236026  
    11<?php
    22
    3 if (! defined('ABSPATH')) exit;
     3if (!defined('ABSPATH')) exit;
    44
    5 add_filter('theme_post_templates', 'topirank_add_custom_post_template');
    6 function topirank_add_custom_post_template($templates)
     5add_filter('theme_page_templates', 'topirank_add_custom_page_template');
     6function topirank_add_custom_page_template($templates)
    77{
    8     $templates['includes/topirank-template.php'] = 'Topirank Custom Template';
     8    $templates['includes/topirank-template.php'] = 'Topirank Custom Page Template';
    99    return $templates;
    1010}
    1111
    12 
    13 add_filter('template_include', 'topirank_load_custom_template');
    14 
    15 function topirank_load_custom_template($template)
     12add_filter('template_include', 'topirank_load_custom_page_template');
     13function topirank_load_custom_page_template($template)
    1614{
    1715    global $post;
     
    2725function topirank_disable_wpautop_on_custom_template($content)
    2826{
    29     if (is_single() && get_post_meta(get_the_ID(), '_wp_page_template', true) === 'includes/topirank-template.php') {
     27    if (is_page() && get_post_meta(get_the_ID(), '_wp_page_template', true) === 'includes/topirank-template.php') {
    3028        remove_filter('the_content', 'wpautop');
    3129    }
     
    3432add_filter('the_content', 'topirank_disable_wpautop_on_custom_template', 9);
    3533
     34add_filter('use_block_editor_for_post', function ($use_block_editor, $post) {
     35    if ($post && $post->post_type === 'page') {
     36        $template = get_post_meta($post->ID, '_wp_page_template', true);
     37        if ($template === 'includes/topirank-template.php') {
     38            return false;
     39        }
     40    }
     41    return $use_block_editor;
     42}, 10, 2);
    3643
    3744add_action('load-post.php', function () {
     
    5259    }
    5360
    54     if (!current_user_can('edit_post', $post_id)) {
    55         wp_die(('You are not allowed to edit this post.'));
     61    if (!current_user_can('edit_page', $post_id)) {
     62        wp_die(('You are not allowed to edit this page.'));
    5663    }
    5764
     
    7683    });
    7784});
    78 
    79 
    80 
    81 
    82 add_filter('use_block_editor_for_post', function ($use_block_editor, $post) {
    83     if ($post && $post->post_type === 'post') {
    84         $template = get_post_meta($post->ID, '_wp_page_template', true);
    85         if ($template === 'includes/topirank-template.php') {
    86             return false;
    87         }
    88     }
    89     return $use_block_editor;
    90 }, 10, 2);
  • topirank-integration/trunk/includes/helpers.php

    r3234611 r3236026  
    1818                continue;
    1919            }
    20 
    21             $article_title = preg_replace('/\s{2,}/', ' ', $directory_name);
    2220            $file_path = $file->getPathname();
    2321            $content = $wp_filesystem->get_contents($file_path);
     22            $article_title = topirank_extract_title_from_html($file_path);
     23
     24            if (empty($article_title)) {
     25                continue;
     26            }
     27
    2428            $unique_title = topirank_generate_unique_title($article_title);
    2529
    2630            $args = [
    27                 'post_type'      => 'post',
     31                'post_type'      => 'page',
    2832                'title'          => $article_title,
    2933                'post_status'    => 'publish',
     
    6064                'post_content' => $content,
    6165                'post_status'  => 'publish',
    62                 'post_type'    => 'post',
     66                'post_type'    => 'page',
    6367                'post_name'    => sanitize_title($article_title),
    6468            ]);
     
    184188    }
    185189}
     190
     191function topirank_extract_title_from_html($file_path)
     192{
     193    $content = file_get_contents($file_path);
     194    if (!$content) {
     195        return null;
     196    }
     197
     198    libxml_use_internal_errors(true);
     199    $dom = new DOMDocument();
     200    @$dom->loadHTML($content);
     201    libxml_clear_errors();
     202
     203    $title_elements = $dom->getElementsByTagName('title');
     204
     205    return ($title_elements->length > 0) ? trim($title_elements->item(0)->textContent) : null;
     206}
  • topirank-integration/trunk/includes/register-files.php

    r3220350 r3236026  
    5151
    5252add_action('template_redirect', function () {
    53     if (is_single()) {
    54         global $post;
    55 
    56         $archive_name = get_post_meta($post->ID, '_topirank_archive_name', true);
    57         if (empty($archive_name)) {
    58             return;
    59         }
    60         wp_enqueue_style(
    61             'topirank-normalize',
    62             plugins_url('/assets/css/topirank-normalize.css', dirname(__FILE__)),
    63             [],
    64             time()
    65         );
    66         wp_enqueue_script(
    67             'set-height-js',
    68             plugins_url('/assets/js/topirank-normalize.js', dirname(__FILE__)),
    69             [],
    70             time(),
    71             true
    72         );
    73     }
     53
     54    global $post;
     55
     56    $archive_name = get_post_meta($post->ID, '_topirank_archive_name', true);
     57    if (empty($archive_name)) {
     58        return;
     59    }
     60    wp_enqueue_style(
     61        'topirank-normalize',
     62        plugins_url('/assets/css/topirank-normalize.css', dirname(__FILE__)),
     63        [],
     64        time()
     65    );
     66    wp_enqueue_script(
     67        'set-height-js',
     68        plugins_url('/assets/js/topirank-normalize.js', dirname(__FILE__)),
     69        [],
     70        time(),
     71        true
     72    );
    7473});
    7574
     
    7776function topirank_enqueue_archive_files()
    7877{
    79     if (is_single()) {
    80         global $post;
    81 
    82         $archive_name = get_post_meta($post->ID, '_topirank_archive_name', true);
    83         if (empty($archive_name)) {
    84             return;
    85         }
    86 
    87         $upload_dir = wp_upload_dir();
    88         $base_upload_path = $upload_dir['basedir'];
    89         $base_upload_url = $upload_dir['baseurl'];
    90 
    91         $css_path = $base_upload_path . '/topirank/css/' . $archive_name;
    92         $js_path = $base_upload_path . '/topirank/js/' . $archive_name;
    93 
    94         $css_url = $base_upload_url . '/topirank/css/' . $archive_name;
    95         $js_url = $base_upload_url . '/topirank/js/' . $archive_name;
    96 
    97         $post_slugs = get_posts([
    98             'fields' => 'post_name',
    99             'posts_per_page' => -1,
    100             'post_type' => 'post',
    101         ]);
    102 
    103         $post_slugs = wp_list_pluck($post_slugs, 'post_name');
    104 
    105         if (is_dir($css_path)) {
    106             $css_files = glob($css_path . '/*.css');
    107             foreach ($css_files as $css_file) {
    108                 $file_name = basename($css_file);
    109 
    110                 wp_enqueue_style(
    111                     'topirank-archive-css-' . sanitize_title($file_name),
    112                     $css_url . '/' . $file_name,
    113                     [],
    114                     time()
    115                 );
    116             }
    117         }
    118 
    119         if (is_dir($js_path)) {
    120             $js_files = glob($js_path . '/*.js');
    121             $matched_files = [];
    122 
    123             foreach ($js_files as $js_file) {
    124                 $file_name = basename($js_file, '.js');
    125                 $file_name = str_replace('-script', '', $file_name);
    126 
    127                 if (strpos($file_name, '-page-') !== false) {
    128                     $file_slug = explode('-page-', $file_name)[1];
    129 
    130                     if (in_array($file_slug, $post_slugs, true)) {
    131                         $matched_files[$file_slug] = $js_file;
    132                     } else {
    133                         wp_enqueue_script(
    134                             'topirank-archive-js-' . sanitize_title($file_name),
    135                             $js_url . '/' . basename($js_file),
    136                             [],
    137                             time(),
    138                             true
    139                         );
    140                     }
     78    global $post;
     79
     80    $archive_name = get_post_meta($post->ID, '_topirank_archive_name', true);
     81    if (empty($archive_name)) {
     82        return;
     83    }
     84
     85    $upload_dir = wp_upload_dir();
     86    $base_upload_path = $upload_dir['basedir'];
     87    $base_upload_url = $upload_dir['baseurl'];
     88
     89    $css_path = $base_upload_path . '/topirank/css/' . $archive_name;
     90    $js_path = $base_upload_path . '/topirank/js/' . $archive_name;
     91
     92    $css_url = $base_upload_url . '/topirank/css/' . $archive_name;
     93    $js_url = $base_upload_url . '/topirank/js/' . $archive_name;
     94
     95    $post_slugs = get_posts([
     96        'fields' => 'post_name',
     97        'posts_per_page' => -1,
     98        'post_type' => 'post',
     99    ]);
     100
     101    $post_slugs = wp_list_pluck($post_slugs, 'post_name');
     102
     103    if (is_dir($css_path)) {
     104        $css_files = glob($css_path . '/*.css');
     105        foreach ($css_files as $css_file) {
     106            $file_name = basename($css_file);
     107
     108            wp_enqueue_style(
     109                'topirank-archive-css-' . sanitize_title($file_name),
     110                $css_url . '/' . $file_name,
     111                [],
     112                time()
     113            );
     114        }
     115    }
     116
     117    if (is_dir($js_path)) {
     118        $js_files = glob($js_path . '/*.js');
     119        $matched_files = [];
     120
     121        foreach ($js_files as $js_file) {
     122            $file_name = basename($js_file, '.js');
     123            $file_name = str_replace('-script', '', $file_name);
     124
     125            if (strpos($file_name, '-page-') !== false) {
     126                $file_slug = explode('-page-', $file_name)[1];
     127
     128                if (in_array($file_slug, $post_slugs, true)) {
     129                    $matched_files[$file_slug] = $js_file;
    141130                } else {
    142131                    wp_enqueue_script(
     
    148137                    );
    149138                }
    150             }
    151 
    152             $current_post_slug = $post->post_name;
    153             if (isset($matched_files[$current_post_slug])) {
     139            } else {
    154140                wp_enqueue_script(
    155                     'topirank-archive-js-' . sanitize_title($current_post_slug),
    156                     $js_url . '/' . basename($matched_files[$current_post_slug]),
     141                    'topirank-archive-js-' . sanitize_title($file_name),
     142                    $js_url . '/' . basename($js_file),
    157143                    [],
    158144                    time(),
     
    161147            }
    162148        }
     149
     150        $current_post_slug = $post->post_name;
     151        if (isset($matched_files[$current_post_slug])) {
     152            wp_enqueue_script(
     153                'topirank-archive-js-' . sanitize_title($current_post_slug),
     154                $js_url . '/' . basename($matched_files[$current_post_slug]),
     155                [],
     156                time(),
     157                true
     158            );
     159        }
    163160    }
    164161}
     
    168165    global $post;
    169166
    170     if ('post.php' === $hook && isset($post)) {
    171         $current_template = get_post_meta($post->ID, '_wp_page_template', true);
    172         if ($current_template === 'includes/topirank-template.php') {
    173             wp_enqueue_style(
    174                 'my_special_template_admin_css',
    175                 plugins_url('/assets/css/topirank-post-admin-style.css', dirname(__FILE__)),
    176                 [],
    177                 time(),
    178                 'all'
    179             );
    180         }
     167    $current_template = get_post_meta($post->ID, '_wp_page_template', true);
     168    if ($current_template === 'includes/topirank-template.php') {
     169        wp_enqueue_style(
     170            'my_special_template_admin_css',
     171            plugins_url('/assets/css/topirank-post-admin-style.css', dirname(__FILE__)),
     172            [],
     173            time(),
     174            'all'
     175        );
    181176    }
    182177}
     
    202197    }
    203198
     199    if (get_post_type($post_id) !== 'page') {
     200        return $mce_css;
     201    }
     202
    204203    $template = get_post_meta($post_id, '_wp_page_template', true);
    205204
  • topirank-integration/trunk/includes/update-content-and-seo.php

    r3234611 r3236026  
    6868    $href = $link->getAttribute('href');
    6969    if (preg_match('/^(?:\.\.\/)+(.+)\/index\.html$/iu', $href, $matches)) {
    70       $encoded_title = $matches[1];
    71       $decoded_title = urldecode($encoded_title);
    72       $formatted_title = preg_replace('/\s+/', ' ', str_replace(['-', '_'], ' ', $decoded_title));
    73       $formatted_title = trim($formatted_title);
    74 
    75       if (!empty($formatted_title)) {
    76         $link_titles[] = $formatted_title;
     70      $encoded_url = $matches[1];
     71      $decoded_url = urldecode($encoded_url);
     72      $slug = basename($decoded_url);
     73
     74      if (!empty($slug)) {
     75        $link_slugs[] = $slug;
    7776        $link_elements[] = $link;
    7877      }
     
    8079  }
    8180
    82   $unique_titles = array_unique($link_titles);
    83   $title_to_post = [];
    84 
    85   if (!empty($unique_titles)) {
    86     foreach ($unique_titles as $unique_title) {
     81  $unique_slugs = array_unique($link_slugs);
     82  $slug_to_post = [];
     83
     84  if (!empty($unique_slugs)) {
     85    foreach ($unique_slugs as $unique_slug) {
    8786      $query = new WP_Query([
    88         'title'  => $unique_title,
    89         'post_type' => 'post',
     87        'name'       => $unique_slug,
     88        'post_type'  => 'page',
    9089        'post_status' => 'publish',
    91         'fields' => 'ids',
     90        'fields'     => 'ids',
    9291        'posts_per_page' => 1,
    9392      ]);
    9493      $found_post_id = !empty($query->posts) ? $query->posts[0] : null;
    9594      if ($found_post_id) {
    96         $title_to_post[$unique_title] = get_permalink($found_post_id);
     95        $slug_to_post[$unique_slug] = get_permalink($found_post_id);
    9796      }
    9897      wp_reset_postdata();
     
    101100
    102101  foreach ($link_elements as $index => $link) {
    103     $formatted_title = $link_titles[$index];
    104     if (isset($title_to_post[$formatted_title])) {
    105       $link->setAttribute('href', $title_to_post[$formatted_title]);
     102    $formatted_slug = $link_slugs[$index];
     103    if (isset($slug_to_post[$formatted_slug])) {
     104      $link->setAttribute('href', $slug_to_post[$formatted_slug]);
    106105    }
    107106  }
  • topirank-integration/trunk/includes/update-functionality.php

    r3220350 r3236026  
    5656{
    5757    $args = [
    58         'post_type'      => 'post',
     58        'post_type'      => 'page',
    5959        'title'          => $title,
    6060        'post_status'    => 'any',
  • topirank-integration/trunk/readme.txt

    r3234611 r3236026  
    44Requires at least: 6.0
    55Tested up to: 6.7.1
    6 Stable tag: 1.0.4
     6Stable tag: 1.0.5
    77Requires PHP: 7.3
    88License: GPLv2 or later
     
    5858== Changelog ==
    5959
     60= 1.0.5 =
     61* Minor admin panel style fixes.
     62* Minor post style fixes.
     63* Improved template handling.
     64
    6065= 1.0.4 =
    6166* Improved large file upload functionality via the application.
  • topirank-integration/trunk/topirank-integration.php

    r3234611 r3236026  
    33Plugin Name: Topirank Integration
    44Description: Plugin for parsing pages
    5 Version: 1.0.4
     5Version: 1.0.5
    66Author: Topirank
    77License: GPLv2 or later
Note: See TracChangeset for help on using the changeset viewer.