Last active
October 20, 2021 10:27
-
-
Save wpmudev-sls/2565b6a88fcb26d892bad928fb3db6f8 to your computer and use it in GitHub Desktop.
[Broken Link Checker] - Siteorigin integration
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: [Broken Link Checker] - Integrate with SiteOrigin page builder. | |
* Plugin URI: https://premium.wpmudev.org/ | |
* Description: This integration allows to find and unlink broken links for SiteOrigin builder's Text and HTML widgets. | |
* Task: BLC-144 | |
* Author: Panos Lyrakis @ WPMUDEV | |
* Author URI: https://premium.wpmudev.org/ | |
* License: GPLv2 or later | |
*/ | |
if ( ! defined( 'ABSPATH' ) || ( defined( 'WP_CLI' ) && WP_CLI ) ) { | |
return; | |
} | |
add_filter( | |
'blc-parser-html-link-content', | |
function ( $content ) { | |
if ( false === strpos( $content, 'class="panel-layout"' ) ) { | |
return $content; | |
} | |
return html_entity_decode( stripslashes_deep( $content ), ENT_COMPAT, 'UTF-8' ); | |
} | |
); | |
add_action( | |
'wp_ajax_blc_unlink', | |
function () { | |
if ( ! current_user_can( 'edit_others_posts' ) || ! check_ajax_referer( 'blc_unlink', false, false ) ) { | |
die( | |
json_encode( | |
array( | |
'error' => "You're not allowed to do that!", | |
) | |
) | |
); | |
} | |
if ( isset( $_POST['link_id'] ) ) { | |
$link_id = intval( $_POST['link_id'] ); | |
$link = new blcLink( $link_id ); | |
if ( ! $link->valid() ) { | |
// Just return. The link was probably deleted by original ajax callback. | |
return; | |
} | |
global $wpdb; | |
// $broken_url = str_replace( '/', '\/', htmlentities( addslashes( $link->url ) ) ); | |
$_instances = blc_get_instances( array( $link_id ) ); | |
if ( ! isset( $_instances[ $link_id ] ) ) { | |
return; | |
} | |
foreach ( $_instances[ $link_id ] as $instance ) { | |
$parser = $instance->get_parser(); | |
$post_id = (int) $instance->container_id; | |
$post = get_post( $post_id ); | |
$panels_data = get_post_meta( $post_id, 'panels_data', true ); | |
if ( ! empty( $panels_data ) ) { | |
if ( isset( $panels_data['widgets'] ) && ! empty( $panels_data['widgets'] ) ) { | |
foreach ( $panels_data['widgets'] as $widget_key => $widget ) { | |
if ( isset( $widget['content'] ) ) { | |
$panels_data['widgets'][ $widget_key ]['content'] = $parser->unlink( $widget['content'], $link->url, $link->url ); | |
} | |
if ( isset( $widget['text'] ) ) { | |
$panels_data['widgets'][ $widget_key ]['text'] = $parser->unlink( $widget['text'], $link->url, $link->url ); | |
} | |
} | |
} | |
if ( update_post_meta( $post_id, 'panels_data', $panels_data ) ) { | |
// $link->forget(); | |
$wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->prefix}blc_instances WHERE link_id=%d", $link_id ) ); | |
$wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->prefix}blc_links WHERE link_id=%d", $link_id ) ); | |
} | |
$response = array( | |
'cnt_okay' => 2, | |
'cnt_error' => 0, | |
'errors' => array(), | |
); | |
die( json_encode( $response ) ); | |
} | |
} | |
} | |
return; | |
} | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment