From 04741a3a045a0ab09470cd9dd14ba33aaa634d66 Mon Sep 17 00:00:00 2001 From: abulerforrest Date: Fri, 22 Oct 2021 17:17:08 +0200 Subject: [PATCH] Improved compability with Polylang on import, added attachments widget feature for posts, fixed permalink for news feed --- .DS_Store | Bin 0 -> 6148 bytes admin/css/mfn-wp-plugin-admin.css | 2 +- admin/js/mfn-wp-plugin-admin.js | 2 + .../partials/mfn-wp-plugin-admin-display.php | 21 ++- api.php | 4 +- composer.json | 3 +- config.php | 16 +- lib.php | 27 ++- widgets.php | 155 ++++++++++++++++++ widgets/mfn_news_feed/class-mfn-news-feed.php | 7 +- widgets/mfn_news_feed/css/mfn-news-feed.css | 29 +++- 11 files changed, 246 insertions(+), 20 deletions(-) create mode 100644 .DS_Store diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..b6f39c6e19748afba04cb713752d82760239a407 GIT binary patch literal 6148 zcmeHKOHRW;41I=zg4lG)a<0G)qN+Us7a&a#suUDone!"); diff --git a/admin/partials/mfn-wp-plugin-admin-display.php b/admin/partials/mfn-wp-plugin-admin-display.php index 40079da..5d5bfd3 100644 --- a/admin/partials/mfn-wp-plugin-admin-display.php +++ b/admin/partials/mfn-wp-plugin-admin-display.php @@ -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"; @@ -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'; @@ -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"; @@ -408,7 +414,7 @@ function toggleQueryInput(e) {

- value="on" > + value="on" > plugin_name); ?>
@@ -449,6 +455,17 @@ function toggleQueryInput(e) {

+ + +

+ value="on" > + + plugin_name); ?> +
+ (plugin_name); ?>) Enabled by default. +

+ +
diff --git a/api.php b/api.php index 63fa864..bb9217f 100644 --- a/api.php +++ b/api.php @@ -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 @@ -392,4 +392,4 @@ function MFN_get_feed($lang = 'all', $year = "", $hasTags = array(), $hasNotTags } return $res; -} \ No newline at end of file +} diff --git a/composer.json b/composer.json index e0be103..e483c6d 100644 --- a/composer.json +++ b/composer.json @@ -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", diff --git a/config.php b/config.php index 18b24a3..56baf1f 100644 --- a/config.php +++ b/config.php @@ -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') { @@ -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) { @@ -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; } @@ -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'])) { diff --git a/lib.php b/lib.php index 780ff81..56afc0f 100644 --- a/lib.php +++ b/lib.php @@ -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; @@ -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(" @@ -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 ); } @@ -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, @@ -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) { diff --git a/widgets.php b/widgets.php index e9a6365..37a58bb 100644 --- a/widgets.php +++ b/widgets.php @@ -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'); @@ -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 ' + + '; + } + + 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 '
'; + 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 = ''; + } else { + $icon = ''; + } + $link = '' . $icon . $attachment->file_title . ''; + echo '
' . $link . '
'; + } + echo '
'; + } + + 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 { @@ -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(); diff --git a/widgets/mfn_news_feed/class-mfn-news-feed.php b/widgets/mfn_news_feed/class-mfn-news-feed.php index f5fbcea..444b05c 100644 --- a/widgets/mfn_news_feed/class-mfn-news-feed.php +++ b/widgets/mfn_news_feed/class-mfn-news-feed.php @@ -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 ? '?' : '&'; diff --git a/widgets/mfn_news_feed/css/mfn-news-feed.css b/widgets/mfn_news_feed/css/mfn-news-feed.css index 0d592ca..2a702b4 100644 --- a/widgets/mfn_news_feed/css/mfn-news-feed.css +++ b/widgets/mfn_news_feed/css/mfn-news-feed.css @@ -61,4 +61,31 @@ .mfn-newsfeed-pagination { display: flex; justify-content: space-between; -} \ No newline at end of file +} + +.mfn-attachments-container .mfn-attachment { + display: flex; + align-items: center; + min-height: 80px; + height: 80px; +} + +.mfn-attachments-container .mfn-attachment .mfn-attachment-link { + display: inline-block; + vertical-align: middle; +} + +.mfn-attachments-container .mfn-attachment .mfn-attachment-icon { + margin-right: 20px; +} + +.mfn-attachments-container .mfn-attachment .mfn-attachment-icon img { + width: 55px; + border: 1px solid #eee; +} + +.mfn-attachments-container .mfn-attachment .mfn-attachment-icon .dash { + width: 55px; + font-size: 30px; + text-align: center; +}