true ), 'objects' );
unset( $types['attachment'] );
return $types;
}
public function register_metaboxes() {
foreach ( get_post_types( array( 'show_ui' => true ), 'names' ) as $type ) {
if ( 'attachment' !== $type ) {
add_meta_box( 'sila-box', 'đź”— SiloNectra', array( $this, 'metabox_content' ), $type, 'side', 'default' );
}
}
}
public function metabox_content() {
echo '
Loading SiloNectra Assistant...
';
}
private function current_post_id() {
foreach ( array( 'post', 'post_id' ) as $param ) {
if ( isset( $_GET[ $param ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
return absint( wp_unslash( $_GET[ $param ] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
}
}
return 0;
}
private function config( $context ) {
$post_id = $this->current_post_id();
$current_type = $post_id ? get_post_type( $post_id ) : '';
if ( ! $current_type && isset( $_GET['post_type'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
$current_type = sanitize_key( wp_unslash( $_GET['post_type'] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
}
if ( ! $current_type ) {
$current_type = 'post';
}
$post_types = array();
foreach ( $this->public_post_types() as $type ) {
$post_types[] = array( 'value' => $type->name, 'label' => $type->labels->name );
}
$taxonomies = array();
$current_terms = array();
foreach ( get_taxonomies( array( 'public' => true ), 'objects' ) as $tax ) {
if ( ! $tax->hierarchical || ! $tax->show_ui ) {
continue;
}
if ( ! array_intersect( (array) $tax->object_type, array_keys( $this->public_post_types() ) ) ) {
continue;
}
$raw_terms = get_terms( array(
'taxonomy' => $tax->name,
'hide_empty' => false,
'orderby' => 'name',
'order' => 'ASC',
) );
if ( is_wp_error( $raw_terms ) ) {
continue;
}
$terms = array();
foreach ( $raw_terms as $term ) {
$terms[] = array(
'id' => (int) $term->term_id,
'name' => $term->name,
'count' => (int) $term->count,
'parent' => (int) $term->parent,
);
}
$taxonomies[] = array(
'value' => $tax->name,
'label' => $tax->labels->name,
'postTypes' => array_values( array_intersect( (array) $tax->object_type, array_keys( $this->public_post_types() ) ) ),
'terms' => $terms,
);
if ( $post_id && is_object_in_taxonomy( $current_type, $tax->name ) ) {
$ids = wp_get_object_terms( $post_id, $tax->name, array( 'fields' => 'ids' ) );
$current_terms[ $tax->name ] = is_wp_error( $ids ) ? array() : array_map( 'intval', $ids );
}
}
return array(
'ajax' => admin_url( 'admin-ajax.php' ),
'nonce' => wp_create_nonce( 'silonectra_posts' ),
'postId' => $post_id,
'currentType' => $current_type,
'siteUrl' => home_url( '/' ),
'context' => $context,
'pageSize' => self::PAGE_SIZE,
'dashboardUrl' => admin_url( 'admin.php?page=silonectra' ),
'language' => in_array((get_option('sila_pro_settings',array())['language']??'bn'),array('bn','en','hi','ar','es','fr','pt','id','ur'),true)?(get_option('sila_pro_settings',array())['language']??'bn'):'bn',
'postTypes' => $post_types,
'taxonomies' => $taxonomies,
'currentTerms' => $current_terms,
);
}
private function asset_version( $relative ) {
$path = plugin_dir_path( __FILE__ ) . ltrim( $relative, '/' );
return is_file( $path ) ? (string) filemtime( $path ) : self::VERSION;
}
private function enqueue( $context ) {
wp_enqueue_style( 'sila-style', plugins_url( 'assets/assistant.css', __FILE__ ), array(), $this->asset_version( 'assets/assistant.css' ) );
wp_enqueue_script( 'sila-locales', plugins_url( 'assets/locale.js', __FILE__ ), array(), $this->asset_version( 'assets/locale.js' ), true );
wp_enqueue_script( 'sila-script', plugins_url( 'assets/assistant.js', __FILE__ ), array( 'sila-locales' ), $this->asset_version( 'assets/assistant.js' ), true );
wp_add_inline_script( 'sila-script', 'window.SILONECTRA_CONFIG = ' . wp_json_encode( $this->config( $context ) ) . ';', 'before' );
}
public function admin_assets( $hook ) {
if ( 'tools_page_sila-pro-dashboard' === $hook ) { $this->audit_assets( $hook ); return; }
if ( ! in_array( $hook, array( 'post.php', 'post-new.php' ), true ) ) {
return;
}
$screen = get_current_screen();
if ( ! $screen || ! $screen->post_type || 'attachment' === $screen->post_type ) {
return;
}
if ( current_user_can( 'edit_posts' ) || ( $this->current_post_id() && current_user_can( 'edit_post', $this->current_post_id() ) ) ) {
$this->enqueue( 'wordpress' );
}
}
public function elementor_assets() {
if ( current_user_can( 'edit_posts' ) || ( $this->current_post_id() && current_user_can( 'edit_post', $this->current_post_id() ) ) ) {
$this->enqueue( 'elementor' );
}
}
private function post_terms( $post_id ) {
$values = array();
foreach ( array( 'category', 'post_tag' ) as $taxonomy ) {
if ( ! is_object_in_taxonomy( get_post_type( $post_id ), $taxonomy ) ) continue;
$terms = get_the_terms( $post_id, $taxonomy );
if ( is_array( $terms ) ) {
foreach ( $terms as $term ) {
if ( count( $values ) >= 14 ) break;
$values[] = sanitize_text_field( $term->name );
}
}
}
return array_values( array_unique( $values ) );
}
public function admin_menu() {
add_management_page( 'Internal Link Health', 'Internal Link Health', 'edit_others_posts', 'sila-pro-dashboard', array( $this, 'dashboard_page' ) );
}
public function dashboard_page() {
if ( ! current_user_can( 'edit_others_posts' ) ) wp_die( esc_html__( 'Access denied.', 'silonectra' ) );
echo 'SiloNectra · Link Health
';
echo '
Private, API-free audit. Scans published posts in small batches. No content is changed.
';
echo '
';
}
public function audit_assets( $hook ) {
if ( 'tools_page_sila-pro-dashboard' !== $hook || ! current_user_can( 'edit_others_posts' ) ) return;
wp_enqueue_style( 'sila-audit-style', plugins_url( 'assets/audit.css', __FILE__ ), array(), $this->asset_version( 'assets/audit.css' ) );
wp_enqueue_script( 'sila-audit-script', plugins_url( 'assets/audit.js', __FILE__ ), array(), $this->asset_version( 'assets/audit.js' ), true );
wp_add_inline_script( 'sila-audit-script', 'window.SILONECTRA_AUDIT_CONFIG = ' . wp_json_encode( array(
'ajax' => admin_url( 'admin-ajax.php' ), 'nonce' => wp_create_nonce( 'silonectra_audit' ),
'siteUrl' => home_url( '/' ),
) ) . ';', 'before' );
}
private function audit_can() {
check_ajax_referer( 'silonectra_audit', 'nonce' );
if ( ! current_user_can( 'edit_others_posts' ) ) wp_send_json_error( array( 'message' => 'Access denied.' ), 403 );
}
/** Extract normal HTML links plus Elementor editor HTML recursively, without rendering shortcodes. */
private function outgoing_urls( $post ) {
$chunks = array( $post->post_content );
$data = get_post_meta( $post->ID, '_elementor_data', true );
if ( is_string( $data ) && $data ) {
$decoded = json_decode( $data, true );
if ( is_array( $decoded ) ) $this->collect_elementor_html( $decoded, $chunks );
} elseif ( is_array( $data ) ) {
$this->collect_elementor_html( $data, $chunks );
}
$urls = array();
foreach ( $chunks as $chunk ) {
if ( ! is_string( $chunk ) || ! $chunk ) continue;
if ( preg_match_all( '~]*\bhref\s*=\s*(["\'])(.*?)\1~isu', $chunk, $matches ) ) {
foreach ( $matches[2] as $uri ) {
$uri = html_entity_decode( $uri, ENT_QUOTES, 'UTF-8' );
if ( strlen( $uri ) <= 2048 ) $urls[ $uri ] = true;
}
}
}
return array_slice( array_keys( $urls ), 0, 400 );
}
private function collect_elementor_html( $items, &$chunks ) {
foreach ( $items as $key => $value ) {
if ( is_array( $value ) ) { $this->collect_elementor_html( $value, $chunks ); continue; }
if ( is_string( $value ) && in_array( (string) $key, array( 'editor', 'html', 'content', 'description', 'body', 'text' ), true ) ) {
if ( strpos( $value, 'audit_can();
$page = isset( $_POST['page'] ) ? max( 1, absint( wp_unslash( $_POST['page'] ) ) ) : 1; // phpcs:ignore WordPress.Security.NonceVerification.Missing -- audit_can() verifies the AJAX nonce immediately above.
$types = array_keys( $this->public_post_types() );
$query = new WP_Query( array(
'post_type' => $types, 'post_status' => 'publish', 'posts_per_page' => 40,
'paged' => min( $page, 100000 ), 'orderby' => 'ID', 'order' => 'DESC',
'no_found_rows' => false, 'ignore_sticky_posts' => true,
) );
$rows = array();
foreach ( $query->posts as $post ) {
$rows[] = array(
'id' => (int) $post->ID, 'title' => get_the_title( $post ),
'url' => get_permalink( $post ), 'type' => $post->post_type,
'links' => $this->outgoing_urls( $post ),
'edit' => get_edit_post_link( $post->ID, 'raw' ),
);
}
wp_send_json_success( array( 'rows' => $rows, 'page' => $page,
'pages' => (int) $query->max_num_pages, 'total' => (int) $query->found_posts ) );
}
public function ajax_check_url() {
$this->audit_can();
$raw = isset( $_POST['url'] ) ? esc_url_raw( wp_unslash( $_POST['url'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Missing -- audit_can() verifies the AJAX nonce immediately above.
$home = wp_parse_url( home_url( '/' ) );
$u = wp_parse_url( $raw );
if ( ! $u || ! $home || empty( $u['host'] ) || empty( $u['scheme'] ) ||
! in_array( strtolower( $u['scheme'] ), array( 'http', 'https' ), true ) ||
strcasecmp( $u['host'], $home['host'] ) !== 0 ||
isset( $u['user'] ) || isset( $u['pass'] ) ||
( isset( $u['port'] ) ? (int) $u['port'] : ( 'https' === $u['scheme'] ? 443 : 80 ) ) !==
( isset( $home['port'] ) ? (int) $home['port'] : ( 'https' === $home['scheme'] ? 443 : 80 ) ) ) {
wp_send_json_error( array( 'message' => 'Only same-site HTTP(S) URLs are supported.' ), 400 );
}
$cache_key = 'sila_head_' . md5( $raw );
$saved = get_transient( $cache_key );
if ( is_array( $saved ) ) wp_send_json_success( $saved );
$response = wp_safe_remote_head( $raw, array( 'timeout' => 5, 'redirection' => 0, 'limit_response_size' => 1024 ) );
if ( is_wp_error( $response ) ) $result = array( 'code' => 0, 'message' => 'Could not verify: ' . $response->get_error_message() );
else {
$code = (int) wp_remote_retrieve_response_code( $response );
$result = array( 'code' => $code, 'message' => $code === 404 || $code === 410 ? 'Likely broken: HTTP ' . $code :
( $code >= 200 && $code < 400 ? 'Reachable: HTTP ' . $code : 'Review needed: HTTP ' . $code ) );
}
set_transient( $cache_key, $result, 15 * MINUTE_IN_SECONDS );
wp_send_json_success( $result );
}
public function ajax_posts() {
check_ajax_referer( 'silonectra_posts', 'nonce' );
$current = isset( $_POST['current'] ) ? absint( wp_unslash( $_POST['current'] ) ) : 0;
if ( $current ) {
if ( ! current_user_can( 'edit_post', $current ) ) {
wp_send_json_error( array( 'message' => 'You cannot edit this article.' ), 403 );
}
} elseif ( ! current_user_can( 'edit_posts' ) ) {
wp_send_json_error( array( 'message' => 'Permission denied.' ), 403 );
}
$types = $this->public_post_types();
$post_type = isset( $_POST['post_type'] ) ? sanitize_key( wp_unslash( $_POST['post_type'] ) ) : 'post';
$taxonomy = isset( $_POST['taxonomy'] ) ? sanitize_key( wp_unslash( $_POST['taxonomy'] ) ) : '';
$term_id = isset( $_POST['term_id'] ) ? absint( wp_unslash( $_POST['term_id'] ) ) : 0;
$page = isset( $_POST['page'] ) ? max( 1, absint( wp_unslash( $_POST['page'] ) ) ) : 1;
$search = isset( $_POST['search'] ) ? sanitize_text_field( wp_unslash( $_POST['search'] ) ) : '';
$search = function_exists( 'mb_substr' ) ? mb_substr( $search, 0, 100 ) : substr( $search, 0, 100 );
$type_names = array_keys( $types );
if ( 'all' === $post_type ) {
$selected_types = $type_names;
} elseif ( isset( $types[ $post_type ] ) ) {
$selected_types = array( $post_type );
} else {
wp_send_json_error( array( 'message' => 'Invalid post type.' ), 400 );
}
if ( $term_id ) {
$tax = get_taxonomy( $taxonomy );
if ( ! $tax || ! $tax->public || ! $tax->hierarchical || ! term_exists( $term_id, $taxonomy ) ) {
wp_send_json_error( array( 'message' => 'Invalid category/taxonomy.' ), 400 );
}
$selected_types = array_values( array_intersect( $selected_types, (array) $tax->object_type ) );
}
if ( empty( $selected_types ) ) {
wp_send_json_success( array( 'posts' => array(), 'page' => 1, 'pages' => 1, 'total' => 0 ) );
}
$args = array(
'post_type' => $selected_types,
'post_status' => 'publish',
'posts_per_page' => self::PAGE_SIZE,
'paged' => min( $page, 100000 ),
'orderby' => array( 'date' => 'DESC', 'ID' => 'DESC' ),
'ignore_sticky_posts' => true,
'no_found_rows' => false,
// phpcs:ignore WordPressVIPMinimum.Performance.WPQueryParams.PostNotIn_post__not_in -- Exclude exactly one selected article, never an unbounded ID set; this query has a fixed page/scan limit and core result caching.
'post__not_in' => $current ? array( $current ) : array(),
);
if ( $term_id ) {
// phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_tax_query -- Indexed taxonomy membership is the selected-category feature; the WP_Query is paginated/bounded and uses core result caching.
$args['tax_query'] = array( array(
'taxonomy' => $taxonomy,
'field' => 'term_id',
'terms' => array( $term_id ),
'include_children' => true,
) );
}
if ( '' !== $search ) {
$args['s'] = $search;
$args['search_columns'] = array( 'post_title' );
}
$query = new WP_Query( $args );
$posts = array();
foreach ( $query->posts as $post ) {
$url = get_permalink( $post );
if ( ! $url ) {
continue;
}
$posts[] = array(
'id' => (int) $post->ID,
'title' => html_entity_decode( get_the_title( $post ), ENT_QUOTES, get_bloginfo( 'charset' ) ),
'url' => esc_url_raw( $url ),
'postType' => $post->post_type,
'terms' => $this->post_terms( $post->ID ),
'date' => get_the_date( 'Y-m-d', $post ),
'excerpt' => wp_html_excerpt( wp_strip_all_tags( $post->post_excerpt ? $post->post_excerpt : $post->post_content ), 240, '…' ),
);
}
wp_send_json_success( array(
'posts' => $posts,
'page' => $page,
'pages' => (int) $query->max_num_pages,
'total' => (int) $query->found_posts,
) );
}
}
new Silonectra_Editor_Assistant();
// Pro analytics adds indexed reports without replacing the v3 editor assistant.
require_once plugin_dir_path( __FILE__ ) . 'includes/class-sila-pro.php';
Silonectra_Analytics::boot( __FILE__ );