Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Check for metadata #135

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions doctor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,5 @@ php-in-upload:
check: PHP_In_Upload
language-update:
check: Language_Update
meta-tags:
check: Check_Meta_Tags
68 changes: 68 additions & 0 deletions inc/checks/class-check-meta-tags.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

namespace runcommand\Doctor\Checks;

use WP_CLI;

/**
* Warns when there are greater than %threshold_count% plugins activated.
*/
class Check_Meta_Tags extends Plugin {

// Meta tags reg. ex.
private $metatags_expression = "/<meta[^>]+(?:name|property)=\"([^\"]*)\"[^>]+content=\"([^\"]*)\"[^>]*>/";

public function run() {

// Fetch homepage resposne.
$response = wp_remote_get(site_url());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than make a remote request, we should instead render and parse the template.


if ( ! is_wp_error( $response ) ) {

// Get the homepage source.
$body = wp_remote_retrieve_body( $response );

$meta_tags = $this->getMetaTags( $body );

}

$this->set_status( 'success' );
$this->set_message( 'Meta tags checking initiated.' );

}

/**
* Extract all metatags sources from content
*
* @param string $content full body source of webpage.
* @return array, an array of extracted metatags
*/

public function getMetaTags( $content = '' ) {

$metatags = array();

if ( empty( $content ) ) {
return;
}

preg_match_all( $this->metatags_expression, $content, $match_tags );

if ( isset( $match_tags[2] ) && count( $match_tags[2]) ) {

foreach ( $match_tags[2] as $key => $match_tag ) {

$key = trim( $match_tags[1][ $key ] );
$match_tag = trim( $match_tag );

if ( $match_tag ) {
$metatags[] = array( $key, $match_tag );
}
}
}

return $metatags;

}

}