Commit inicial powerservice

This commit is contained in:
Gabriel
2026-10-19 12:45:45 -06:00
commit ad85486fe2
10158 changed files with 2736804 additions and 0 deletions
@@ -0,0 +1,207 @@
<?php
/**
* Ability: rank-math/get-link-report
*
* @since 1.0.272
* @package RankMath
* @subpackage RankMath\Abilities\Link_Genius
* @author Rank Math <support@rankmath.com>
*/
namespace RankMath\Abilities\Link_Genius;
use RankMath\Abilities\Ability_Interface;
defined( 'ABSPATH' ) || exit;
/**
* Registers and executes the rank-math/get-link-report ability.
*/
class Get_Link_Report implements Ability_Interface {
/**
* Ability category slug.
*
* @var string
*/
private $category;
/**
* Shared meta args.
*
* @var array
*/
private $shared_meta;
/**
* Runner instance.
*
* @var Link_Report_Runner
*/
private $runner;
/**
* Constructor.
*
* @param string $category Ability category slug.
* @param array $shared_meta Shared meta args.
* @param Link_Report_Runner|null $runner Runner instance.
*/
public function __construct( string $category, array $shared_meta, ?Link_Report_Runner $runner = null ) {
$this->category = $category;
$this->shared_meta = $shared_meta;
$this->runner = $runner ?? new Link_Report_Runner();
}
/**
* Register the ability with the WordPress Abilities API.
*
* @return void
*/
public function register(): void {
\wp_register_ability(
'rank-math/get-link-report',
[
'category' => $this->category,
'label' => \esc_html__( 'Get link report', 'seo-by-rank-math' ),
'description' => \esc_html__(
'Returns a site-wide link health report: total internal and external links, post counts with no internal or external links, and — on Rank Math PRO — broken links, redirects, nofollow counts, and HTTP status distribution from the Link Genius audit.',
'seo-by-rank-math'
),
'input_schema' => [
'type' => 'object',
'default' => [],
'properties' => [
'include_posts' => [
'type' => 'boolean',
'default' => false,
'description' => 'Whether to include per-post link counts in the response.',
],
],
'additionalProperties' => false,
],
'output_schema' => $this->output_schema(),
'permission_callback' => [ $this, 'check_permissions' ],
'execute_callback' => [ $this, 'execute' ],
'meta' => array_merge(
$this->shared_meta,
[
'annotations' => [
'readonly' => true,
'destructive' => false,
'idempotent' => true,
],
]
),
]
);
}
/**
* Check if the current user has permission to execute this ability.
*
* @return bool
*/
public function check_permissions(): bool {
return \current_user_can( 'rank_math_link_builder' );
}
/**
* Execute the ability.
*
* @param array $input Ability input arguments.
* @return array
*/
public function execute( array $input = [] ): array {
$result = $this->runner->run( $input );
rank_math()->tracking->track_ability_executed(
'Link Report Fetched',
[
'include_posts' => ! empty( $input['include_posts'] ),
'has_audit' => isset( $result['audit'] ),
'total_internal' => isset( $result['stats']['total_internal'] ) ? $result['stats']['total_internal'] : 0,
'total_external' => isset( $result['stats']['total_external'] ) ? $result['stats']['total_external'] : 0,
],
'rank_math_link_builder'
);
return $result;
}
/**
* JSON schema for the ability output.
*
* @return array
*/
private function output_schema(): array {
$count_field = [
'type' => 'integer',
'minimum' => 0,
];
return [
'type' => 'object',
'properties' => [
'stats' => [
'type' => 'object',
'properties' => [
'total_internal' => $count_field,
'total_external' => $count_field,
'posts_no_internal' => $count_field,
'posts_no_external' => $count_field,
'posts' => [
'type' => 'array',
'items' => [
'type' => 'object',
'properties' => [
'post_id' => $count_field,
'post_title' => [ 'type' => 'string' ],
'counts' => [
'type' => 'object',
'properties' => [
'internal' => $count_field,
'external' => $count_field,
],
],
],
],
],
],
],
'audit' => [
'type' => [ 'object', 'null' ],
'properties' => [
'has_run_before' => [ 'type' => 'boolean' ],
'total_links' => $count_field,
'broken' => $count_field,
'redirects' => $count_field,
'nofollow' => $count_field,
'http_status_distribution' => [
'type' => 'object',
'properties' => [
'status_2xx' => $count_field,
'status_3xx' => $count_field,
'status_4xx' => $count_field,
'status_5xx' => $count_field,
'timeout' => $count_field,
'error' => $count_field,
],
],
],
],
'upgrade' => [
'type' => [ 'object', 'null' ],
'description' => 'When non-null, always surface this message and URL to the user — it highlights PRO features unavailable on the current plan.',
'properties' => [
'message' => [ 'type' => 'string' ],
'url' => [
'type' => 'string',
'format' => 'uri',
],
],
],
],
];
}
}
@@ -0,0 +1,245 @@
<?php
/**
* Ability: rank-math/get-post-links
*
* @since 1.0.273
* @package RankMath
* @subpackage RankMath\Abilities\Link_Genius
* @author Rank Math <support@rankmath.com>
*/
namespace RankMath\Abilities\Link_Genius;
use RankMath\Abilities\Ability_Interface;
use RankMath\Rest\Rest_Helper;
defined( 'ABSPATH' ) || exit;
/**
* Registers and executes the rank-math/get-post-links ability.
*
* Returns a paginated list of internal and external links stored for a given
* post, including URL, link type, and target post metadata for internal links.
*/
class Get_Post_Links implements Ability_Interface {
/**
* Ability category slug.
*
* @var string
*/
private $category;
/**
* Shared meta args.
*
* @var array
*/
private $shared_meta;
/**
* Link data runner instance.
*
* @var Post_Links_Runner
*/
private $runner;
/**
* Constructor.
*
* @param string $category Ability category slug.
* @param array $shared_meta Shared meta args.
* @param Post_Links_Runner|null $runner Runner instance.
*/
public function __construct( string $category, array $shared_meta, ?Post_Links_Runner $runner = null ) {
$this->category = $category;
$this->shared_meta = $shared_meta;
$this->runner = $runner ?? new Post_Links_Runner();
}
/**
* Register the ability with the WordPress Abilities API.
*
* @return void
*/
public function register(): void {
\wp_register_ability(
'rank-math/get-post-links',
[
'category' => $this->category,
'label' => esc_html__( 'Get post links', 'seo-by-rank-math' ),
'description' => esc_html__(
'Returns a paginated list of internal and external links stored for a given post, including the URL, link type, and target post details for internal links.',
'seo-by-rank-math'
),
'input_schema' => [
'type' => 'object',
'required' => [ 'post_id' ],
'properties' => [
'post_id' => [
'type' => 'integer',
'description' => esc_html__( 'ID of the post whose links to retrieve.', 'seo-by-rank-math' ),
'minimum' => 1,
],
],
'additionalProperties' => false,
],
'output_schema' => $this->output_schema(),
'permission_callback' => [ $this, 'check_permissions' ],
'execute_callback' => [ $this, 'execute' ],
'meta' => array_merge(
$this->shared_meta,
[
'annotations' => [
'readonly' => true,
'destructive' => false,
'idempotent' => true,
],
]
),
]
);
}
/**
* Check if the current user has permission to execute this ability.
*
* @return bool
*/
public function check_permissions(): bool {
return current_user_can( 'rank_math_link_builder' );
}
/**
* Execute the ability.
*
* @param array $input Ability input arguments.
* @return array
*/
public function execute( array $input = [] ): array {
$post_id = absint( $input['post_id'] ?? 0 );
$post = $post_id ? get_post( $post_id ) : null;
if ( ! $post ) {
return [ 'error' => esc_html__( 'Invalid post ID.', 'seo-by-rank-math' ) ];
}
if ( ! Rest_Helper::can_edit_post( $post ) ) {
return [ 'error' => esc_html__( 'Sorry, you are not allowed to edit this post.', 'seo-by-rank-math' ) ];
}
$result = $this->runner->run(
[
'source_id' => $post_id,
]
);
$internal = [];
$external = [];
foreach ( $result['links'] ?? [] as $link ) {
$item = [
'url' => $link['url'],
'anchor' => $link['anchor'],
'dofollow' => $link['dofollow'],
];
if ( 'internal' === $link['type'] ) {
$item['target_post_id'] = $link['target_post_id'];
$internal[] = $item;
} else {
$external[] = $item;
}
}
$output = [
'post_id' => $post_id,
'internal' => $internal,
'external' => $external,
'counts' => [
'internal' => count( $internal ),
'external' => count( $external ),
],
];
rank_math()->tracking->track_ability_executed(
'Post Links Fetched',
[
'post_id' => $post_id,
'internal' => count( $internal ),
'external' => count( $external ),
],
'rank_math_link_builder'
);
return $output;
}
/**
* JSON schema for the ability output.
*
* @return array
*/
private function output_schema(): array {
$link_item = [
'type' => 'object',
'properties' => [
'url' => [
'type' => 'string',
'description' => 'Full URL of the link.',
],
'anchor' => [
'type' => [ 'string', 'null' ],
'description' => 'Anchor text of the link. Null when anchor data is unavailable (FREE plugin).',
],
'dofollow' => [
'type' => [ 'boolean', 'null' ],
'description' => 'True when the link is dofollow. Null when rel data is unavailable.',
],
],
];
$internal_item = $link_item;
$internal_item['properties']['target_post_id'] = [
'type' => 'integer',
'description' => 'WordPress post ID of the link target.',
];
return [
'type' => 'object',
'properties' => [
'post_id' => [
'type' => 'integer',
'description' => 'WordPress post ID of the source post.',
],
'internal' => [
'type' => 'array',
'description' => 'Internal links found in the post.',
'items' => $internal_item,
],
'external' => [
'type' => 'array',
'description' => 'External links found in the post.',
'items' => $link_item,
],
'counts' => [
'type' => 'object',
'description' => 'Count of links returned per type.',
'properties' => [
'internal' => [
'type' => 'integer',
'description' => 'Number of internal links returned.',
],
'external' => [
'type' => 'integer',
'description' => 'Number of external links returned.',
],
],
],
'error' => [
'type' => 'string',
],
],
];
}
}
@@ -0,0 +1,91 @@
<?php
/**
* Runner for the get-link-report ability.
*
* @since 1.0.272
* @package RankMath
* @subpackage RankMath\Abilities\Link_Genius
* @author Rank Math <support@rankmath.com>
*/
namespace RankMath\Abilities\Link_Genius;
use RankMath\Links\Api\Controller;
defined( 'ABSPATH' ) || exit;
/**
* Calls the existing Links REST controller methods to build a link report.
*/
class Link_Report_Runner {
/**
* Links REST controller.
*
* @var Controller
*/
private $controller;
/**
* Constructor.
*
* @param Controller|null $controller Links REST controller.
*/
public function __construct( ?Controller $controller = null ) {
$this->controller = $controller ?? new Controller();
}
/**
* Build and return the link report.
*
* @param array $input Ability input (e.g. include_posts).
* @return array
*/
public function run( array $input = [] ): array {
$posts_response = $this->controller->get_posts_stats();
$links_response = $this->controller->get_links_stats();
$posts = (array) $posts_response->get_data();
$links = (array) $links_response->get_data();
$total_posts = (int) ( $posts['total_posts'] ?? 0 );
$result = [
'stats' => [
'total_internal' => (int) ( $links['internal'] ?? 0 ),
'total_external' => (int) ( $links['external'] ?? 0 ),
'posts_no_internal' => $total_posts - (int) ( $posts['posts_with_internal'] ?? 0 ),
'posts_no_external' => $total_posts - (int) ( $posts['posts_with_external'] ?? 0 ),
],
];
if ( ! empty( $input['include_posts'] ) ) {
$data = $this->controller->get_posts_data( [ 'per_page' => 100 ] );
$result['stats']['posts'] = array_map(
function ( $post ) {
$row = (array) $post;
return [
'post_id' => (int) ( $row['post_id'] ?? 0 ),
'post_title' => (string) ( $row['post_title'] ?? '' ),
'counts' => [
'internal' => (int) ( $row['internal_link_count'] ?? 0 ),
'external' => (int) ( $row['external_link_count'] ?? 0 ),
],
];
},
$data['posts']
);
}
$result = (array) apply_filters( 'rank_math/abilities/link_report_result', $result, $input );
if ( ! isset( $result['audit'] ) ) {
$result['upgrade'] = [
'message' => \esc_html__( 'Upgrade to Rank Math PRO to unlock broken link detection, redirect chain analysis, and HTTP status distribution from the AI Link Genius audit.', 'seo-by-rank-math' ),
'url' => 'https://rankmath.com/pricing/',
];
}
return $result;
}
}
@@ -0,0 +1,79 @@
<?php
/**
* Runner: fetches per-post link data via the existing links REST endpoint.
*
* @since 1.0.273
* @package RankMath
* @subpackage RankMath\Abilities\Link_Genius
* @author Rank Math <support@rankmath.com>
*/
namespace RankMath\Abilities\Link_Genius;
use RankMath\Links\Api\Controller;
defined( 'ABSPATH' ) || exit;
/**
* Delegates to the Links REST controller to retrieve individual link rows for a post.
*
* Calls Controller::get_links() directly instead of going through the REST stack
* so that the request stays in-process. PRO plugin overrides
* (rank_math/links/rest_links_response) still apply because they live inside
* the controller callback itself.
*/
class Post_Links_Runner {
/**
* Links REST controller.
*
* @var Controller
*/
private $controller;
/**
* Constructor.
*
* @param Controller|null $controller Links REST controller (injectable for tests).
*/
public function __construct( ?Controller $controller = null ) {
$this->controller = $controller ?? new Controller();
}
/**
* Fetch link rows for a post by calling the links controller directly.
*
* @param array $args {
* Query arguments.
*
* @type int $post_id Source post ID. Must be > 0.
* @type int $page 1-based page number.
* @type int $per_page Rows per page (max 100).
* @type string $is_internal Filter value: '1' = internal only, '0' = external only, '' = all.
* }
* @return array { links: array, total: int, pages: int }
*/
public function run( array $args ): array {
$data = $this->controller->get_links_data( $args );
$links = [];
foreach ( $data['links'] ?? [] as $link ) {
$links[] = [
'id' => (int) $link->id,
'url' => $link->url,
'type' => $link->type,
'anchor' => (string) ( $link->anchor_text ?? '' ),
'dofollow' => isset( $link->is_nofollow ) ? ! (bool) $link->is_nofollow : null,
'target_post_id' => (int) ( $link->target_post_id ?? 0 ),
'target_title' => $link->target_title ?? '',
'target_url' => $link->target_url ?? '',
];
}
return [
'links' => $links,
'total' => (int) ( $data['total'] ?? 0 ),
'pages' => (int) ( $data['pages'] ?? 0 ),
];
}
}
@@ -0,0 +1,89 @@
<?php
/**
* Subscriber for Link Genius abilities.
*
* @since 1.0.273
* @package RankMath
* @subpackage RankMath\Abilities\Link_Genius
* @author Rank Math <support@rankmath.com>
*/
namespace RankMath\Abilities\Link_Genius;
use RankMath\Traits\Hooker;
use RankMath\Abilities\Subscriber_Interface;
defined( 'ABSPATH' ) || exit;
/**
* Registers the Link Genius ability category and its abilities.
*/
class Subscriber implements Subscriber_Interface {
use Hooker;
/**
* Ability category slug for Link Genius abilities.
*/
const CATEGORY_SLUG = 'rank-math-link-genius';
/**
* Shared meta args.
*
* @var array
*/
private $shared_meta;
/**
* Constructor.
*
* @param array $shared_meta Shared meta args from the top-level Abilities class.
*/
public function __construct( array $shared_meta ) {
$this->shared_meta = $shared_meta;
}
/**
* Wire hooks.
*
* @return void
*/
public function register(): void {
$this->action( 'wp_abilities_api_categories_init', 'register_category' );
$this->action( 'wp_abilities_api_init', 'register_get_link_report' );
$this->action( 'wp_abilities_api_init', 'register_get_post_links' );
}
/**
* Register the Link Genius ability category.
*
* @return void
*/
public function register_category(): void {
\wp_register_ability_category(
self::CATEGORY_SLUG,
[
'label' => esc_html__( 'AI Link Genius', 'seo-by-rank-math' ),
'description' => esc_html__( 'Abilities to retrieve and analyse internal and external link data for posts.', 'seo-by-rank-math' ),
]
);
}
/**
* Register the rank-math/get-link-report ability.
*
* @return void
*/
public function register_get_link_report(): void {
( new Get_Link_Report( self::CATEGORY_SLUG, $this->shared_meta ) )->register();
}
/**
* Register the rank-math/get-post-links ability.
*
* @return void
*/
public function register_get_post_links(): void {
( new Get_Post_Links( self::CATEGORY_SLUG, $this->shared_meta ) )->register();
}
}
@@ -0,0 +1 @@
<?php // Silence is golden.