Skip to content
This repository has been archived by the owner on Jan 26, 2021. It is now read-only.

Commit

Permalink
fix(purifier): fixes warnings when running purifier
Browse files Browse the repository at this point in the history
fixes #88
  • Loading branch information
Eduardo Campaña committed Jan 18, 2019
1 parent 6f91dd6 commit 5e8f75c
Showing 1 changed file with 39 additions and 22 deletions.
61 changes: 39 additions & 22 deletions wp-pwa.php
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ function fix_forbidden_media($id) {
}

// Add data-attachment-id to content images.
function add_image_ids($data, $post_type, $request) {
function add_image_ids($response, $post_type, $request) {
global $wpdb;

$purge = $request->get_param('purgeContentMediaTransients') === 'true';
Expand All @@ -395,11 +395,18 @@ function add_image_ids($data, $post_type, $request) {

// fix featured media if necessary
if ($fixForbiddenMedia)
$this->fix_forbidden_media($data->data['featured_media']);
$this->fix_forbidden_media($response->data['featured_media']);

$dom = new simple_html_dom();
$dom->load($data->data['content']['rendered']);

$dom->load(
isset($response->data['content']['rendered']) ?
$response->data['content']['rendered'] :
""
);

$imgIds = [];

foreach ($dom->find('img') as $image) {
$dataAttachmentId = $image->getAttribute('data-attachment-id');
$class = $image->getAttribute('class');
Expand Down Expand Up @@ -433,44 +440,54 @@ function add_image_ids($data, $post_type, $request) {
),
rest_url('wp/v2/media')
);
$data->add_links(array(
$response->add_links(array(
'wp:contentmedia' => array(
'href' => $media_url,
'embeddable' => true,
)
));
}
$html = $dom->save();
if ($html) $data->data['content']['rendered'] = $html;
$data->data['content_media'] = $imgIds;
return $data;
if ($html) $response->data['content']['rendered'] = $html;
$response->data['content_media'] = $imgIds;
return $response;
}

// Use HTML Purifier in the content.
function purify_html($data, $post_type, $request) {
function purify_html($response, $post_type, $request) {
$disableHtmlPurifier = $request->get_param('disableHtmlPurifier');
$settings = get_option('frontity_settings');

if (
$disableHtmlPurifier === 'true' ||
!$settings['html_purifier_active']
) {
return $data;
// Removes HTML tags from 'title.rendered' and
// saves the result in a new field called 'text'.
if (isset($response->data['title']['rendered'])) {
$response->data['title']['text'] =
strip_tags(html_entity_decode($response->data['title']['rendered']));
}

$data->data['title']['text'] =
strip_tags(html_entity_decode($data->data['title']['rendered']));
$data->data['excerpt']['text'] =
strip_tags(html_entity_decode($data->data['excerpt']['rendered']));
// Removes HTML tags from 'excerpt.rendered' and
// saves the result in a new field called 'text'.
if (isset($response->data['excerpt']['rendered'])) {
$response->data['excerpt']['text'] =
strip_tags(html_entity_decode($response->data['excerpt']['rendered']));
}

if ($disableHtmlPurifier === 'true' || !$settings['html_purifier_active']) {
return $response;
}

require_once(plugin_dir_path(__FILE__) . '/libs/purifier.php');
$purifier = load_purifier();
$purifiedContent = $purifier->purify($data->data['content']['rendered']);
if (!empty($purifiedContent)) {
$data->data['content']['rendered'] = $purifiedContent;

if (isset($response->data['content']['rendered'])) {
$purifier = load_purifier();
$purifiedContent = $purifier->purify($response->data['content']['rendered']);

if (!empty($purifiedContent)) {
$response->data['content']['rendered'] = $purifiedContent;
}
}

return $data;
return $response;
}

// Delete directory. Used when purging HTML Purifier files.
Expand Down

0 comments on commit 5e8f75c

Please sign in to comment.