Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
MrGKanev committed Sep 4, 2024
1 parent ab144b9 commit a40d5f0
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 33 deletions.
14 changes: 9 additions & 5 deletions js/diagram.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,7 @@ jQuery(document).ready(function ($) {

// Load saved internal link analysis if it exists
if (internalLinkAnalysisData) {
renderInternalLinkAnalysis();
$("#analyze-internal-links").text("Update Analysis");
$(
"#delete-internal-link-analysis, #download-internal-link-analysis, #download-internal-link-analysis-svg"
).show();
initializeInternalLinkAnalysis();
}

$("#generate-diagram").on("click", function (e) {
Expand Down Expand Up @@ -179,6 +175,14 @@ jQuery(document).ready(function ($) {
});
});

function initializeInternalLinkAnalysis() {
renderInternalLinkAnalysis();
$("#analyze-internal-links").text("Update Analysis");
$(
"#delete-internal-link-analysis, #download-internal-link-analysis, #download-internal-link-analysis-svg"
).show();
}

function renderInternalLinkAnalysis() {
var $results = $("#internal-link-analysis-results");
$results.empty();
Expand Down
74 changes: 46 additions & 28 deletions wp-internal-linking.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Plugin Name: WordPress Internal Linking
* Plugin URI: https://github.com/Open-WP-Club/wp-internal-linking
* Description: A plugin to manage and analyze internal linking in WordPress
* Version: 0.0.2
* Version: 0.0.3
* Author: Gabriel Kanev
* Author URI: https://gkanev.com
* License: GPL-2.0 License
Expand Down Expand Up @@ -34,6 +34,7 @@ public function __construct()
add_action('wp_ajax_delete_content_relationship_map', array($this, 'delete_content_relationship_map'));
add_action('wp_ajax_analyze_all_content', array($this, 'analyze_all_content'));
add_action('wp_ajax_analyze_internal_links', array($this, 'analyze_internal_links'));
add_action('wp_ajax_delete_internal_link_analysis', array($this, 'delete_internal_link_analysis'));

$this->options = get_option('internal_linking_options');
$this->blacklist = $this->get_blacklist();
Expand All @@ -54,7 +55,7 @@ public function add_plugin_pages()

public function create_main_page()
{
?>
?>
<div class="wrap">
<h1>Internal Linking</h1>
<h2 class="nav-tab-wrapper">
Expand Down Expand Up @@ -141,7 +142,7 @@ public function create_overview_page()
</tbody>
</table>
</div>

<div class="internal-linking-section">
<h3>Manage Blacklist</h3>
<div id="blacklist-manager">
Expand Down Expand Up @@ -209,10 +210,15 @@ public function create_internal_link_analysis_page()
<div class="internal-linking-section">
<h3>Internal Link Analysis</h3>
<p>Analyze existing internal links in your posts, pages, and other content types.</p>
<button id="analyze-internal-links" class="button button-primary">Analyze Internal Links</button>
<div id="internal-link-analysis-results"></div>
<div id="internal-link-analysis-results" data-saved-analysis="<?php echo esc_attr($saved_analysis ? 'true' : 'false'); ?>"></div>
<div class="button-group">
<button id="analyze-internal-links" class="button button-primary"><?php echo $saved_analysis ? 'Update Analysis' : 'Analyze Internal Links'; ?></button>
<button id="delete-internal-link-analysis" class="button" <?php echo $saved_analysis ? '' : 'style="display:none;"'; ?>>Delete Analysis</button>
<button id="download-internal-link-analysis" class="button" <?php echo $saved_analysis ? '' : 'style="display:none;"'; ?>>Download as PNG</button>
<button id="download-internal-link-analysis-svg" class="button" <?php echo $saved_analysis ? '' : 'style="display:none;"'; ?>>Download as SVG</button>
</div>
</div>
<?php
<?php
}

public function page_init()
Expand All @@ -239,7 +245,8 @@ public function enqueue_admin_scripts($hook)
wp_enqueue_script('internal-linking-diagram', plugins_url('js/diagram.js', __FILE__), array('jquery', 'mermaid', 'file-saver'), '1.0', true);
wp_localize_script('internal-linking-admin', 'ailAjax', array(
'ajaxurl' => admin_url('admin-ajax.php'),
'savedDiagram' => get_option('internal_linking_diagram', '')
'savedDiagram' => get_option('internal_linking_diagram', ''),
'savedInternalLinkAnalysis' => get_option('internal_linking_analysis_diagram', '')
));
}
}
Expand All @@ -257,8 +264,8 @@ private function display_blacklist()
{
$page = isset($_GET['blacklist_page']) ? intval($_GET['blacklist_page']) : 1;
$search = isset($_GET['blacklist_search']) ? sanitize_text_field($_GET['blacklist_search']) : '';
$filtered_blacklist = array_filter($this->blacklist, function($word) use ($search) {

$filtered_blacklist = array_filter($this->blacklist, function ($word) use ($search) {
return empty($search) || stripos($word, $search) !== false;
});

Expand All @@ -283,8 +290,8 @@ private function display_blacklist_pagination()
{
$page = isset($_GET['blacklist_page']) ? intval($_GET['blacklist_page']) : 1;
$search = isset($_GET['blacklist_search']) ? sanitize_text_field($_GET['blacklist_search']) : '';
$filtered_blacklist = array_filter($this->blacklist, function($word) use ($search) {

$filtered_blacklist = array_filter($this->blacklist, function ($word) use ($search) {
return empty($search) || stripos($word, $search) !== false;
});

Expand Down Expand Up @@ -511,21 +518,6 @@ public function delete_content_relationship_map()
wp_send_json_success();
}

private function escape_mermaid_label($text)
{
// Remove HTML entities
$text = html_entity_decode($text, ENT_QUOTES | ENT_HTML5, 'UTF-8');
// Remove any characters that might interfere with Mermaid syntax
$text = preg_replace('/[\\\\\'\"]/u', '', $text);
// Limit length to prevent overly long labels
$text = mb_substr($text, 0, 30);
if (mb_strlen($text) >= 30) {
$text .= '...';
}
// Ensure the label is not empty
return !empty($text) ? $text : 'Unnamed';
}

public function analyze_internal_links()
{
if (!current_user_can('manage_options')) {
Expand Down Expand Up @@ -571,9 +563,20 @@ public function analyze_internal_links()
wp_reset_postdata();

$diagram = $this->generate_internal_link_diagram($link_data);
update_option('internal_linking_analysis_diagram', $diagram);
wp_send_json_success($diagram);
}

public function delete_internal_link_analysis()
{
if (!current_user_can('manage_options')) {
wp_die('Unauthorized user');
}

delete_option('internal_linking_analysis_diagram');
wp_send_json_success();
}

private function generate_internal_link_diagram($link_data)
{
$diagram = "graph TD\n";
Expand Down Expand Up @@ -605,6 +608,21 @@ private function generate_internal_link_diagram($link_data)
return $diagram;
}

private function escape_mermaid_label($text)
{
// Remove HTML entities
$text = html_entity_decode($text, ENT_QUOTES | ENT_HTML5, 'UTF-8');
// Remove any characters that might interfere with Mermaid syntax
$text = preg_replace('/[\\\\\'\"]/u', '', $text);
// Limit length to prevent overly long labels
$text = mb_substr($text, 0, 30);
if (mb_strlen($text) >= 30) {
$text .= '...';
}
// Ensure the label is not empty
return !empty($text) ? $text : 'Unnamed';
}

public function add_ajax_handlers()
{
add_action('wp_ajax_add_blacklist_word', array($this, 'ajax_add_blacklist_word'));
Expand Down Expand Up @@ -654,7 +672,7 @@ public function ajax_search_blacklist()
$search = isset($_POST['search']) ? sanitize_text_field($_POST['search']) : '';
$page = isset($_POST['page']) ? intval($_POST['page']) : 1;

$filtered_blacklist = array_filter($this->blacklist, function($word) use ($search) {
$filtered_blacklist = array_filter($this->blacklist, function ($word) use ($search) {
return empty($search) || stripos($word, $search) !== false;
});

Expand Down Expand Up @@ -701,4 +719,4 @@ function run_internal_linking()
}

// Hook to WordPress init action
add_action('init', 'run_internal_linking');
add_action('init', 'run_internal_linking');

0 comments on commit a40d5f0

Please sign in to comment.