Skip to content

Commit

Permalink
Improved compability with Polylang on import, added attachments widge…
Browse files Browse the repository at this point in the history
…t feature for posts, fixed permalink for news feed
  • Loading branch information
abulerforrest committed Oct 22, 2021
1 parent 5fb4eba commit 04741a3
Show file tree
Hide file tree
Showing 11 changed files with 246 additions and 20 deletions.
Binary file added .DS_Store
Binary file not shown.
2 changes: 1 addition & 1 deletion admin/css/mfn-wp-plugin-admin.css
Original file line number Diff line number Diff line change
Expand Up @@ -274,4 +274,4 @@ form {

input.wide {
width: 100%;
}
}
2 changes: 2 additions & 0 deletions admin/js/mfn-wp-plugin-admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@
syncTax();
} else {
registerSyncLatestClick();
// also sync taxonomy
syncTax();
}

$('#sync-status').append("<span class=\"dashicons dashicons-yes mfn-success-icon\"></span>Done!");
Expand Down
21 changes: 19 additions & 2 deletions admin/partials/mfn-wp-plugin-admin-display.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@
// Grab all options
$options = get_option($this->plugin_name);

if ($options === false || (is_array($options) && sizeof($options) === 0)) {
$options['verify_signature'] = 'on';
$options['enable_attachments'] = 'on';
}

// Cleanup
$hub_url = isset($options['hub_url']) && $options['hub_url'] !== "" ? $options['hub_url'] : "https://feed.mfn.se/v1";
$sync_url = isset($options['sync_url']) && $options['sync_url'] !== "" ? $options['sync_url'] : "https://feed.mfn.se/v1";
Expand All @@ -43,7 +48,7 @@
$cus_query = isset($options['cus_query']) ? $options['cus_query'] : "";

$disable_archive = isset($options['disable_archive']) ? $options['disable_archive'] : 'off';
$verify_signature = isset($options['verify_signature']) ? $options['verify_signature'] : 'on';
$verify_signature = isset($options['verify_signature']) ? $options['verify_signature'] : 'off';
$use_wpml = isset($options['use_wpml']) ? $options['use_wpml'] : 'off';
$use_pll = isset($options['use_pll']) ? $options['use_pll'] : 'off';

Expand All @@ -52,6 +57,7 @@
$thumbnail_on = isset($options['thumbnail_on']) ? $options['thumbnail_on'] : 'off';
$thumbnail_allow_delete = isset($options['thumbnail_allow_delete']) ? $options['thumbnail_allow_delete'] : 'off';

$enable_attachments = isset($options['enable_attachments']) ? $options['enable_attachments'] : 'off';

$subscription_id = isset($options['subscription_id']) ? $options['subscription_id'] : "N/A";
$posthook_secret = isset($options['posthook_secret']) ? $options['posthook_secret'] : "N/A";
Expand Down Expand Up @@ -408,7 +414,7 @@ function toggleQueryInput(e) {
<tr>
<td>
<p>
<input type="checkbox" id="<?php echo $this->plugin_name; ?>-verify_signature" name="<?php echo $this->plugin_name; ?>[verify_signature]" <?php checked($verify_signature, "on"); ?> value="on" <?php echo $is_disabled; ?>>
<input type="checkbox" id="<?php echo $this->plugin_name; ?>-verify_signature" name="<?php echo $this->plugin_name; ?>[verify_signature]" <?php checked("on", $verify_signature); ?> value="on" <?php echo $is_disabled; ?>>
<label for="<?php echo $this->plugin_name; ?>-verify_signature"><?php _e('Verify Signature', $this->plugin_name); ?></label>
<legend class="screen-reader-text"><?php _e('Verify Signature', $this->plugin_name); ?></legend>
<br>
Expand Down Expand Up @@ -449,6 +455,17 @@ function toggleQueryInput(e) {
</p>
<td>
</tr>
<tr>
<td>
<p>
<input type="checkbox" id="<?php echo $this->plugin_name; ?>-enable_attachments" name="<?php echo $this->plugin_name; ?>[enable_attachments]" <?php checked("on", $enable_attachments); ?> value="on" <?php echo $is_disabled; ?>>
<label for="<?php echo $this->plugin_name; ?>-enable_attachments"><?php _e('Enable Attachments Widget', $this->plugin_name); ?></label>
<legend class="screen-reader-text"><?php _e('Enable Attachments Widget', $this->plugin_name); ?></legend>
<br>
<small>(<?php _e('If enabled, our plugin will handle the listing of attachments and will bypass the default mfn-attachment footer', $this->plugin_name); ?>) <strong>Enabled by default.</strong></small>
</p>
<td>
</tr>
</tbody>
</table>
<hr>
Expand Down
4 changes: 2 additions & 2 deletions api.php
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ function MFN_get_feed($lang = 'all', $year = "", $hasTags = array(), $hasNotTags
$params = array();

$query = "
SELECT post_date_gmt, p.post_title, tags, lang.meta_value lang, post_name" . ($include_content ? ', post_content' : '' ) . "
SELECT post_date_gmt, p.post_title, tags, lang.meta_value lang, post_id, post_name" . ($include_content ? ', post_content' : '' ) . "
FROM $wpdb->posts p
INNER JOIN $wpdb->postmeta lang
ON p.ID = lang.post_id
Expand Down Expand Up @@ -392,4 +392,4 @@ function MFN_get_feed($lang = 'all', $year = "", $hasTags = array(), $hasNotTags
}

return $res;
}
}
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"type": "wordpress-plugin",
"require": {
"composer/installers": ">=1.0.6",
"php": ">=7.0.0"
"php": ">=7.0.0",
"ext-json": "*"
},

"description": "A wordpress plugin for MFN integrations",
Expand Down
16 changes: 12 additions & 4 deletions config.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ function get_current_language(): string
if (isset(get_option(MFN_PLUGIN_NAME)['rewrite_post_type'])) {

// adding filter for rewriting the post_type from settings
add_filter('register_post_type_args', 'rewrite_post_type', 10, 2);
add_filter('register_post_type_args', 'rewrite_post_type', 10, 2);

function rewrite_post_type($args, $post_type) {
if ($post_type === 'mfn_news') {
Expand Down Expand Up @@ -496,7 +496,7 @@ function sync_mfn_taxonomy()
}
};

$upsert_pll = function ($enItem, $enTerm, $prefix = '') {
$upsert_pll = function ($enItem, $enTerm, $prefix = '', $pllLangMapping) {

$enParentTerm = null;
if ($enTerm->parent > 0) {
Expand All @@ -508,7 +508,10 @@ function sync_mfn_taxonomy()

$allowed = pll_the_languages( array( 'raw' => true));

foreach ($enItem['i10n'] as $lang => $name) {
foreach ($enItem['i10n'] as $i10nLang => $name) {

$lang = $pllLangMapping[$i10nLang];

if(!array_key_exists($lang, $allowed)){
continue;
}
Expand Down Expand Up @@ -624,8 +627,13 @@ function sync_mfn_taxonomy()
$upsert_wpml($item, $term, $prefix);
}
if ($has_pll && $use_pll == 'on') {
$pllLangMapping = array();
foreach (pll_languages_list(array('fields' => array())) as $pll_lang) {
$l = explode('_', $pll_lang->locale)[0];
$pllLangMapping[$l] = $pll_lang->slug;
};
pll_set_term_language($term->term_id, 'en');
$upsert_pll($item, $term, $prefix);
$upsert_pll($item, $term, $prefix, $pllLangMapping);
}

if (isset($item['children'])) {
Expand Down
27 changes: 24 additions & 3 deletions lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,13 @@ function createTags($item): array
}
}
if ($use_pll == 'on' && $lang != 'en') {
$pllLangMapping = array();
foreach (pll_languages_list(array('fields' => array())) as $pll_lang) {
$l = explode('_', $pll_lang->locale)[0];
$pllLangMapping[$l] = $pll_lang->slug;
};
foreach ($newtag as $i => $t) {
$newtag[$i] = $t . "_" . $lang;
$newtag[$i] = $t . "_" . $pllLangMapping[$lang];
}
}
return $newtag;
Expand Down Expand Up @@ -192,7 +197,12 @@ function upsertLanguage($post_id, $groupId, $lang)
$use_pll = isset($options['use_pll']) ? $options['use_pll'] : 'off';

if ($use_pll == 'on') {
pll_set_post_language($post_id, $lang);
$pllLangMapping = array();
foreach (pll_languages_list(array('fields' => array())) as $pll_lang) {
$l = explode('_', $pll_lang->locale)[0];
$pllLangMapping[$l] = $pll_lang->slug;
};
pll_set_post_language($post_id, $pllLangMapping[$lang]);

global $wpdb;
$q = $wpdb->prepare("
Expand All @@ -210,7 +220,7 @@ function upsertLanguage($post_id, $groupId, $lang)
foreach ($res as $i => $post){
$_post_id= $post->post_id;
$_lang = $post->lang;
$translations[$_lang] = $_post_id;
$translations[$pllLangMapping[$_lang]] = $_post_id;
}
pll_save_post_translations( $translations );
}
Expand Down Expand Up @@ -295,6 +305,12 @@ function upsertItem($item, $signature = '', $raw_data = '', $reset_cache = false
$outro($post_id);
return 0;
}

if (empty($html)) {
$html = '';
}
$html = "[mfn_before_post]\n" . $html . "\n[mfn_after_post]";

$post_id = wp_insert_post(array(
'post_content' => $html,
'post_title' => $title,
Expand Down Expand Up @@ -364,6 +380,11 @@ function upsertItemFull($item, $signature = '', $raw_data = '', $reset_cache = f

};

if (empty($html)) {
$html = '';
}
$html = "[mfn_before_post]\n" . $html . "\n[mfn_after_post]";

if ($post_id) {
$post = get_post($post_id);
if ($post !== null && $post->post_type === MFN_POST_TYPE) {
Expand Down
155 changes: 155 additions & 0 deletions widgets.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ function mfn_load_widget()
register_widget('mfn_archive_widget');
register_widget('mfn_subscription_widget');
register_widget('mfn_news_feed_widget');
register_widget('mfn_before_post');
register_widget('mfn_after_post');
}

add_action('widgets_init', 'mfn_load_widget');
Expand Down Expand Up @@ -55,6 +57,141 @@ function yearClass($year): string
return trim(str_replace('/', '-', str_replace('*', '', $year)));
}

class mfn_before_post extends WP_Widget
{
public function __construct()
{
parent::__construct(
'mfn_before_post',
__('MFN Before Post', 'mfn_before_post_domain'),
array('description' => __('Adds additional data before post.', 'mfn_before_post_domain'),)
);
}
function widget() {
// currently does nothing
}
}

// Creating the widget
class mfn_after_post extends WP_Widget
{
public function __construct()
{
parent::__construct(
'mfn_after_post',
__('MFN After Post', 'mfn_after_post_domain'),
array('description' => __('Adds additional data after post.', 'mfn_after_post_domain'),)
);
}

private function fetch_attachments(): array
{
$attachments = array();
foreach (get_post_meta(get_the_ID(), MFN_POST_TYPE . '_attachment_data') as $data)
{
$d = json_decode($data);
array_push($attachments, $d);
}
return $attachments;
}

private function remove_attachment_footer()
{
echo '
<script>
Array.prototype.slice.call(document.querySelectorAll(".mfn-attachment")).forEach(function (el) { el.remove() });
</script>
';
}

private function getProxiedUrl($url, $file_title, $content_type) {

$mime_to_ext = array(
"application/pdf" => "pdf",
"image/jpg" => "jpg",
"image/jpeg" => "jpg",
"image/png" => "png",
"image/tiff" => "tiff",
'audio/mpeg' => 'mp3',
'audio/mpeg3' => 'mp3',
'audio/mp3' => 'mp3',
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' => 'xlsx',
'application/vnd.openxmlformats-officedocument.wordprocessingml.document' => 'docx',
'application/vnd.openxmlformats-officedocument.presentationml.presentation' => 'pptx',
'application/x-zip' => 'zip',
'application/zip' => 'zip',
'application/x-zip-compressed' => 'zip',
'application/s-compressed' => 'zip',
'multipart/x-zip' => 'zip',
'video/mp4' => 'mp4',
'video/mpeg' => 'mpeg',
'video/quicktime' => 'mov',
);

$vanity_part = '';
if (isset($mime_to_ext[$content_type])) {
$ext = $mime_to_ext[$content_type];
$vanity_part = '/' . sanitize_title($file_title) . "." . $ext;
}

$ops = get_option('mfn-wp-plugin');
$storageUrl = isset($ops['sync_url'])
? ((strpos($ops['sync_url'], 'https://feed.mfn.') === 0)
? str_replace('//feed.mfn', '//storage.mfn', str_replace('/v1', '', $ops['sync_url']))
: str_replace('//mfn', '//storage.mfn', $ops['sync_url']))
: null;

if ($storageUrl === null || $storageUrl === '' || $content_type === null || $content_type === '') {
return array($url, '');
}

$isStorageAttachment = strpos($url, $storageUrl) === 0;

$outUrl = $isStorageAttachment
? $url
: "$storageUrl/proxy$vanity_part?url=" . urlencode($url);

$previewUrl = '';
if ($content_type === 'application/pdf') {
$previewUrl = $isStorageAttachment
? $url . '?type=jpg'
: $outUrl . '&type=jpg';
}
if (strpos($content_type, 'image/') === 0) {
$previewUrl = $isStorageAttachment
? $url . '?size=w-512'
: $outUrl . '&size=w-512';
}

return array($outUrl, $previewUrl);
}

private function list_attachments() {
echo '<div class="mfn-attachments-container">';
foreach ($this->fetch_attachments() as $attachment) {
$icon_type_slug = empty($attachment->content_type) ? 'admin-links' : 'media-default';

list ($url, $preview_url) = $this->getProxiedUrl($attachment->url, $attachment->file_title, $attachment->content_type);
if (empty($preview_url)) {
$icon = '<span class="mfn-attachment-icon"><span class="dash dashicons dashicons-' . $icon_type_slug . '"></span></span>';
} else {
$icon = '<span class="mfn-attachment-icon"><img src="' . $preview_url. '"></span>';
}
$link = '<a class="mfn-attachment-link" href="' . $url . '">' . $icon . $attachment->file_title . '</a>';
echo '<div class="mfn-attachment">' . $link . '</div>';
}
echo '</div>';
}

function widget() {
$ops = get_option('mfn-wp-plugin');
if (isset($ops['enable_attachments'])) {
$this->remove_attachment_footer();
$this->list_attachments();
}
}
}

// Creating the widget
class mfn_archive_widget extends WP_Widget
{
Expand Down Expand Up @@ -1132,6 +1269,24 @@ public function update($new_instance, $old_instance)
}
} //

function load_shortcode_mfn_before_post($atts)
{
ob_start();
the_widget('mfn_before_post', $atts);
return ob_get_clean();
}

add_shortcode('mfn_before_post', 'load_shortcode_mfn_before_post');

function load_shortcode_mfn_after_post($atts)
{
ob_start();
the_widget('mfn_after_post', $atts);
return ob_get_clean();
}

add_shortcode('mfn_after_post', 'load_shortcode_mfn_after_post');

function load_shortcode_mfn_archive_widget($atts)
{
ob_start();
Expand Down
7 changes: 1 addition & 6 deletions widgets/mfn_news_feed/class-mfn-news-feed.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,7 @@ public function list_news_items($feed, $tzLocation, $timestampFormat, $onlytagsa
$tags .= $html;
}

if (isset(get_post_type_object('mfn_news')->rewrite['slug'])) {
$item_url = get_home_url() . '/' . get_post_type_object('mfn_news')->rewrite['slug'] . '/' . $item->post_name;
}
else {
$item_url = get_home_url() . "/" . MFN_POST_TYPE . "/" . $item->post_name;
}
$item_url = get_permalink($item->post_id);

if ($is_disclaimer) {
$name_query_param = strpos($disclaimerurl, '?') === false ? '?' : '&';
Expand Down
Loading

0 comments on commit 04741a3

Please sign in to comment.