| 1 |
<?php |
| 2 |
|
| 3 |
class Meow_MWSEO_API { |
| 4 |
|
| 5 |
public $core; |
| 6 |
|
| 7 |
public function __construct( $core ) { |
| 8 |
$this->core = $core; |
| 9 |
} |
| 10 |
|
| 11 |
#region SEO Settings |
| 12 |
|
| 13 |
public function get_seo_title( $post_id ) { |
| 14 |
if ( ! $post_id ) { return [ "success" => false, "message" => "Post ID is required." ]; } |
| 15 |
|
| 16 |
$post = get_post( $post_id ); |
| 17 |
if ( ! $post ) { return [ "success" => false, "message" => "Post not found." ]; } |
| 18 |
|
| 19 |
$seo_title = $this->core->get_seo_title( $post ); |
| 20 |
return [ "success" => true, "data" => $seo_title ]; |
| 21 |
} |
| 22 |
|
| 23 |
public function set_seo_title( $post_id, $title ) { |
| 24 |
if ( ! $post_id || ! $title ) { return [ "success" => false, "message" => "Post ID and title are required." ]; } |
| 25 |
|
| 26 |
$post = get_post( $post_id ); |
| 27 |
if ( ! $post ) { return [ "success" => false, "message" => "Post not found." ]; } |
| 28 |
|
| 29 |
$result = $this->core->set_seo_title( $post, $title ); |
| 30 |
return [ "success" => true, "data" => $result ]; |
| 31 |
} |
| 32 |
|
| 33 |
public function get_seo_excerpt( $post_id ) { |
| 34 |
if ( ! $post_id ) { return [ "success" => false, "message" => "Post ID is required." ]; } |
| 35 |
|
| 36 |
$post = get_post( $post_id ); |
| 37 |
if ( ! $post ) { return [ "success" => false, "message" => "Post not found." ]; } |
| 38 |
|
| 39 |
$seo_excerpt = $this->core->get_seo_excerpt( $post ); |
| 40 |
return [ "success" => true, "data" => $seo_excerpt ]; |
| 41 |
} |
| 42 |
|
| 43 |
public function set_seo_excerpt( $post_id, $excerpt ) { |
| 44 |
if ( ! $post_id || ! $excerpt ) { return [ "success" => false, "message" => "Post ID and excerpt are required." ]; } |
| 45 |
|
| 46 |
$post = get_post( $post_id ); |
| 47 |
if ( ! $post ) { return [ "success" => false, "message" => "Post not found." ]; } |
| 48 |
|
| 49 |
$result = $this->core->set_seo_excerpt( $post, $excerpt ); |
| 50 |
return [ "success" => true, "data" => $result ]; |
| 51 |
} |
| 52 |
|
| 53 |
#endregion |
| 54 |
|
| 55 |
#region SEO Score |
| 56 |
|
| 57 |
public function get_seo_score( $post_id ) { |
| 58 |
if ( ! $post_id ) { return [ "success" => false, "message" => "Post ID is required." ]; } |
| 59 |
|
| 60 |
$post = get_post( $post_id ); |
| 61 |
if ( ! $post ) { return [ "success" => false, "message" => "Post not found." ]; } |
| 62 |
|
| 63 |
$result = $this->core->get_seo_engine_post_meta( $post ); |
| 64 |
return $result; |
| 65 |
} |
| 66 |
|
| 67 |
public function do_seo_scan( $post_id ) { |
| 68 |
if ( ! $post_id ) { return [ "success" => false, "message" => "Post ID is required." ]; } |
| 69 |
|
| 70 |
$post = get_post( $post_id ); |
| 71 |
if ( ! $post ) { return [ "success" => false, "message" => "Post not found." ]; } |
| 72 |
|
| 73 |
$result = $this->core->calculate_seo_score( $post ); |
| 74 |
return $result; |
| 75 |
} |
| 76 |
|
| 77 |
public function get_scored_posts() { |
| 78 |
return $this->core->get_all_posts_with_seo_score(); |
| 79 |
} |
| 80 |
|
| 81 |
#endregion |
| 82 |
|
| 83 |
#region Insights |
| 84 |
|
| 85 |
public function get_insights( $post_id ) { |
| 86 |
if ( ! $post_id ) { return [ "success" => false, "message" => "Post ID is required." ]; } |
| 87 |
|
| 88 |
$post = get_post( $post_id ); |
| 89 |
if ( ! $post ) { return [ "success" => false, "message" => "Post not found." ]; } |
| 90 |
|
| 91 |
$insights = $this->core->get_speed_and_vitals( $post_id ); |
| 92 |
if ( ! $insights ) { |
| 93 |
return [ "success" => false, "message" => "No insights available for this post." ]; |
| 94 |
} |
| 95 |
|
| 96 |
return [ |
| 97 |
"success" => true, |
| 98 |
"data" => $insights, |
| 99 |
"message" => "Insights retrieved successfully." |
| 100 |
]; |
| 101 |
} |
| 102 |
|
| 103 |
#endregion |
| 104 |
|
| 105 |
#region Robots.txt |
| 106 |
|
| 107 |
public function get_robots_txt() { |
| 108 |
$robots = $this->core->get_robots_txt(); |
| 109 |
$content = $robots['content'] ?? ''; |
| 110 |
|
| 111 |
return $content; |
| 112 |
} |
| 113 |
|
| 114 |
public function set_robots_txt( $content ) { |
| 115 |
$result = $this->core->set_robots_txt( $content ); |
| 116 |
return $result; |
| 117 |
} |
| 118 |
|
| 119 |
#endregion |
| 120 |
|
| 121 |
#region Google Analytics |
| 122 |
|
| 123 |
public function get_google_analytics_monthly_summary( ) { |
| 124 |
$summary = $this->core->get_google_analytics_summary( ); |
| 125 |
return [ "success" => !empty( $summary ), "data" => $summary ]; |
| 126 |
} |
| 127 |
|
| 128 |
public function get_google_analytics_range_summary( $start_date, $end_date ) { |
| 129 |
if ( ! $start_date || ! $end_date ) { |
| 130 |
return [ "success" => false, "message" => "Start date and end date are required." ]; |
| 131 |
} |
| 132 |
|
| 133 |
$summary = $this->core->get_google_analytics_summary( $start_date, $end_date ); |
| 134 |
return [ "success" => !empty( $summary ), "data" => $summary ]; |
| 135 |
} |
| 136 |
|
| 137 |
public function get_google_analytics_monthly_top_posts( ) { |
| 138 |
$top_posts = $this->core->get_google_analytics_top_posts( ); |
| 139 |
|
| 140 |
return [ "success" => !empty( $top_posts ), "data" => $top_posts ]; |
| 141 |
} |
| 142 |
|
| 143 |
public function get_google_analytics_range_top_posts( $start_date, $end_date ) { |
| 144 |
if ( ! $start_date || ! $end_date ) { |
| 145 |
return [ "success" => false, "message" => "Start date and end date are required." ]; |
| 146 |
} |
| 147 |
|
| 148 |
$args = [ |
| 149 |
'start_date' => $start_date, |
| 150 |
'end_date' => $end_date, |
| 151 |
]; |
| 152 |
|
| 153 |
$top_posts = $this->core->get_google_analytics_top_posts( $args ); |
| 154 |
return [ "success" => !empty( $top_posts ), "data" => $top_posts ]; |
| 155 |
} |
| 156 |
|
| 157 |
public function get_google_analytics_monthly_top_posts_by_country( $country = null ) { |
| 158 |
$top_posts = $this->core->get_google_analytics_top_posts( ); |
| 159 |
|
| 160 |
if( !empty( $country ) && $country != 'all' ) { |
| 161 |
$top_posts = array_filter( $top_posts, function( $post ) use ( $country ) { |
| 162 |
return isset( $post['country'] ) && $post['country'] === $country; |
| 163 |
}); |
| 164 |
} |
| 165 |
|
| 166 |
return [ "success" => !empty( $top_posts ), "data" => $top_posts ]; |
| 167 |
} |
| 168 |
|
| 169 |
public function get_google_analytics_top_countries( ) { |
| 170 |
$top_posts = $this->core->get_google_analytics_top_posts( ); |
| 171 |
|
| 172 |
$countries = []; |
| 173 |
foreach ( $top_posts as $post ) { |
| 174 |
if ( isset( $post['country'] ) && ! in_array( $post['country'], $countries ) ) { |
| 175 |
$countries[] = $post['country']; |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
return [ "success" => !empty( $countries ), "data" => $countries ]; |
| 180 |
} |
| 181 |
|
| 182 |
#endregion |
| 183 |
|
| 184 |
} |