Skip to content

Commit

Permalink
v0.3.1
Browse files Browse the repository at this point in the history
  • Loading branch information
matthiasott committed Apr 2, 2017
1 parent 977c388 commit 131008c
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 22 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,11 @@ The Webmention plugin validates and processes the request and then returns HTTP

## Changelog

### 0.3.1

- Changed the retrieval method for links within an entry to fix a bug where a very long article with many links would lead to a PHP execution timeout
- Minor bugfixes and improvements

### 0.3.0

- Webmention sending functionality implemented
Expand Down
4 changes: 2 additions & 2 deletions webmention/WebmentionPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function getName()

public function getVersion()
{
return '0.1.0';
return '0.3.1';
}

public function getDeveloper()
Expand All @@ -51,7 +51,7 @@ public function getDocumentationUrl()
}
public function getDescription()
{
return 'Receive Webmentions and show them on your site.';
return 'Send and receive Webmentions and show them on your site.';
}

public function hasCpSection()
Expand Down
73 changes: 53 additions & 20 deletions webmention/services/WebmentionService.php
Original file line number Diff line number Diff line change
Expand Up @@ -234,10 +234,10 @@ private function _checkResponseType(&$result, $entry, $src, $useBridgy) {
$result['type'] = 'rsvp';
}
} else {
if (isset($entry[properties][like-of]) || isset($entry[properties][like])) {
if (isset($entry['properties']['like-of']) || isset($entry['properties']['like'])) {
$result['type'] = 'like';
}
if (isset($entry[properties][repost-of]) || isset($entry[properties][repost])) {
if (isset($entry['properties']['repost-of']) || isset($entry['properties']['repost'])) {
$result['type'] = 'repost';
}
}
Expand Down Expand Up @@ -321,17 +321,26 @@ public function validateWebmention( $settings, $src, $target ) {
$xpath = new \DOMXPath($doc);

foreach($xpath->query('//a[@href]') as $href) {
$url = $href->getAttribute('href');
if($url == $target) {
$url = $href->getAttribute('href');
$longurl = "";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$a = curl_exec($ch);
if(preg_match('#Location: (.*)#', $a, $r))
$longurl = trim($r[1]);
if($url == $target || $longurl == $target) {
// FOUND THE LINK!
$found = true;
break;
}
}

if(!$found) {
if(empty($found)) {
craft()->userSession->setError(Craft::t("It seems like the source you provided does not include a link to the target."));
/* Stop and render endpoint */
// Stop and render endpoint
if (function_exists('http_response_code')) {
http_response_code(400);
}
Expand Down Expand Up @@ -418,27 +427,27 @@ public function parseWebmention($html, $settings, $src, $target) {
}

/* If author name is empty use the one from the representative h-card */
if(!$result['author']['name']){
if(empty($result['author']['name'])){
if ($representative){
$result['author']['name'] = $representative['properties']['name'][0];
}
}
/* If author url is empty use the one from the representative h-card */
if(!$result['author']['url']){
if(empty($result['author']['url'])){
if ($representative){
$result['author']['url'] = $representative['properties']['url'][0];
}
}
/* If url is empty use source url */
if(!$result['url']){
if(empty($result['url'])){
$result['url'] = $src;
}
/* Use domain if 'site' ∉ {twitter, facebook, googleplus, instagram, flickr} */
if(!$result['site']){
if(empty($result['site'])){
$result['site'] = parse_url($result['url'], PHP_URL_HOST);
}
/* If no author photo is defined, check gravatar for image */
if(!$result['author']['photo']){
if(empty($result['author']['photo'])){
if ($representative['properties']['photo'][0]) {
$result['author']['photo'] = $representative['properties']['photo'][0];
} else {
Expand Down Expand Up @@ -607,6 +616,8 @@ public function onSaveEntry($event) {
$values = array();
$webmentionSetting = false;

$str = "";

// check if Webmention sending is allowed for this entry type (CP settings)
// first check if entry type is known
if (array_key_exists($entryType, $settings->entryTypes)) {
Expand Down Expand Up @@ -639,21 +650,43 @@ public function onSaveEntry($event) {

// only send Webmentions if entry is enabled
if ($entry->getStatus() == 'live' && $webmentionSetting == true) {

// first we get all values from all fields
$str = $str . $entry->title;
// get all values from all fields
foreach ($entry->getFieldLayout()->getFields() as $fieldLayoutField) {
// get the FieldModel
$field = $fieldLayoutField->getField();
$fieldhandle = $field->handle;
$fieldcontent = $entry->$fieldhandle;

// Now get the prepped field value
$value = $entry->getFieldValue($field->handle);

$values[$field->handle] = $value;
if (is_string($fieldcontent)) {
$str = $str . $entry->$fieldhandle;
} else if (is_bool($fieldcontent)) {
$str = $str . $entry->$fieldhandle;
} else {
switch (get_class($fieldcontent)) {
case 'Craft\ElementCriteriaModel':
if (get_class($fieldcontent->getElementType()) == 'Craft\MatrixBlockElementType') {
foreach($fieldcontent as $block) {
foreach ($block->getFieldLayout()->getFields() as $blockLayoutField){
$class = get_class($blockLayoutField->getField()->getFieldType());
if ($class == 'Craft\RichTextFieldType' || $class == 'Craft\PlainTextFieldType' || $class == '') {
$nomen = $blockLayoutField->getField()->handle;
$str = $str . $block->$nomen;
}
}
}
}
break;
case 'Craft\RichTextData':
$str = $str . $fieldcontent->getRawContent();
break;
default:
break;
}
}

}

// Print array values to string
$str = print_r($values, true);

// Get the URLs!
$targets = $this->_extractUrls( $str );

Expand Down

0 comments on commit 131008c

Please sign in to comment.