PluginProbe
NotificationX – FOMO, Live Sales Notification, WooCommerce Sales Popup, GDPR, Social Proof, Announcement Banner & Floating Notification Bar / 3.3.3
NotificationX – FOMO, Live Sales Notification, WooCommerce Sales Popup, GDPR, Social Proof, Announcement Banner & Floating Notification Bar v3.3.3
3.3.3 3.3.2 3.3.1 3.3.0 3.2.14 3.2.13 3.2.12 3.2.11 3.2.10 3.2.9 3.2.8 3.2.7 trunk 0.2.5.5 0.2.5.6 0.2.5.7 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 All 158 releases
← All changes | includes/Features/ShortcodeInline.php +171 -0 0.2.5.5 → 3.3.3 View file →
@@ -1,0 +1,171 @@
1 +<?php
2 +namespace NotificationX\Core;
3 +
4 +use NotificationX\Core\PostType;
5 +use NotificationX\FrontEnd\FrontEnd;
6 +use NotificationX\GetInstance;
7 +
8 +/**
9 + * Class Shortcode For NotificationX
10 + * @method static ShortcodeInline get_instance($args = null)
11 + *
12 + * @since 1.2.3
13 + */
14 +class ShortcodeInline {
15 + /**
16 + * Instance of ShortcodeInline
17 + *
18 + * @var ShortcodeInline
19 + */
20 + use GetInstance;
21 +
22 + public $shortcode_nx_ids = [];
23 +
24 + /**
25 + * __construct__ is for revoke first time to get ready
26 + *
27 + * @return void
28 + */
29 + public function __construct() {
30 + add_shortcode( 'notificationx_inline', array( $this, 'shortcode_inline' ), 999 );
31 + add_filter('nx_inline_notifications_data',array( $this, 'nx_inlineshortcode' ),10,4);
32 + }
33 +
34 + public function nx_inlineshortcode( $return, $source, $id, $settings ){
35 + if( !empty( $settings['shortcodeinline'] ) ) {
36 + return FrontEnd::get_instance()->get_notifications_data(
37 + array(
38 + 'shortcode' => [$settings['nx_id']],
39 + 'inline_shortcode' => true,
40 + )
41 + );
42 + }
43 + return $return;
44 + }
45 +
46 + /**
47 + * this method is responsible for output the shortcode.
48 + *
49 + * @param array $atts
50 + */
51 + public function shortcode_inline( $atts, $content = null ) {
52 + $atts = shortcode_atts( array(
53 + 'id' => '',
54 + 'product_id' => '',
55 + 'post_type' => '',
56 + 'show_link' => true,
57 + ), $atts, 'notificationx_inline'
58 + );
59 +
60 + $nx_id = $atts['id'];
61 +
62 + if ( empty( $nx_id ) ) {
63 + if ( ! current_user_can( 'administrator' ) ) {
64 + return;
65 + }
66 + return '<p class="nx-shortcode-notice">' . __( 'Choose a Notification from the dropdown.', 'notificationx' ) . '</p>';
67 + }
68 +
69 + if ( ! PostType::get_instance()->is_enabled( $nx_id ) ) {
70 + if ( ! current_user_can( 'administrator' ) ) {
71 + return;
72 + }
73 + return '<p class="nx-shortcode-notice">' . __( 'Make sure you have enabled the notification which ID you have given.', 'notificationx' ) . '</p>';
74 + }
75 +
76 + // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- Reviewed for the NotificationX codebase: acceptable in this context.
77 + do_action( 'nx_inline' );
78 + $settings = PostType::get_instance()->get_post($nx_id);
79 + if( $settings['type'] == 'inline' || $settings['source'] == 'woocommerce_sales_inline' ){
80 + $settings['shortcodeinline'] = true;
81 + /**
82 + * @var WooInline|EDDInline
83 + */
84 + $extension = \NotificationX\Extensions\ExtensionFactory::get_instance()->get($settings['source']);
85 + // The inline renderers live in NotificationX Pro; without it (or with the
86 + // module disabled) the extension is a stub or missing.
87 + if ( ! $extension || ! method_exists( $extension, 'show_inline_notification' ) ) {
88 + return '';
89 + }
90 + $output = $extension->show_inline_notification( $atts, $settings);
91 + if( !empty( $output ) ) {
92 + $output = "<div id='notificationx-shortcode-inline-{$atts['id']}' class='notificationx-shortcode-inline-wrapper nx-shortcode-notice'>$output</div>";
93 + }
94 + return $output;
95 + }
96 +
97 + $result = FrontEnd::get_instance()->get_notifications_data( [ 'shortcode' => [ $nx_id ] ] );
98 +
99 + $output = '';
100 + if ( ! empty( $result['shortcode'][ $nx_id ]['entries'] ) ) {
101 + $entries = $result['shortcode'][ $nx_id ]['entries'];
102 + $entries = array_values( $entries );
103 + $settings = $result['shortcode'][ $nx_id ]['post'];
104 +
105 + $logged_in = is_user_logged_in();
106 + $show_on_display = isset($settings['show_on_display']) ? $settings['show_on_display'] : '';
107 + if ( ! ( ( $logged_in && 'logged_out_user' === $show_on_display ) || ( ! $logged_in && 'logged_in_user' === $show_on_display ) ) ) {
108 +
109 + $col = array_column( $entries, 'timestamp' );
110 + if ( count( $col ) == count( $entries ) ) {
111 + array_multisort( $col, SORT_ASC, $entries );
112 + }
113 + $entry = end( $entries );
114 + $template = Inline::get_instance()->get_template( $settings );
115 + // Freemius themes return their lines as an array (for the popup
116 + // layout); the shortcode renders one string, so join them.
117 + if ( is_array( $template ) ) {
118 + $template = implode( ' ', array_filter( array_map( 'trim', array_filter( $template, 'is_scalar' ) ), 'strlen' ) );
119 + }
120 + $_template = $template;
121 +
122 + foreach ( $entry as $key => $val ) {
123 + if ( ! is_array( $val ) ) {
124 + if ( 'rating' === $key ) {
125 + $count = $val;
126 + $val = "<span style='white-space: nowrap'>";
127 + for ( $i = 1; $i <= 5; $i++ ) {
128 + $val .= $i <= $count
129 + ? '<span style="color:#ffc107">★</span>'
130 + : '<span style="color:#eeeeee">★</span>';
131 + }
132 + $val .= '</span>';
133 + } elseif (
134 + ! empty( $entry['link'] ) &&
135 + in_array( $key, [ 'plugin_name', 'product_title', 'post_title', 'plugin_theme_name', 'course_title', 'title' ], true ) &&
136 + 'form' !== $settings['type'] &&
137 + 'email_subscription' !== $settings['type'] &&
138 + 'page_analytics' !== $settings['type']
139 + ) {
140 + $link = $entry['link'];
141 + $target = ! empty( $settings['link_open'] ) ? 'target="_blank"' : '';
142 + if ( 'false' === $atts['show_link'] ) {
143 + $target = '';
144 + $link = 'javascript:void(0)';
145 + }
146 + $val = "<a href='$link' $target>$val</a>";
147 + }
148 + $_template = str_replace( "{{{$key}}}", $val, $_template );
149 + }
150 + if ( ! empty( $entry['timestamp'] ) && strpos( $_template, '{{time}}' ) !== false ) {
151 + $timestamp = $entry['timestamp'];
152 + if ( $timestamp ) {
153 + $diff_for_humans = sprintf(
154 + /* translators: time */
155 + _x( '%s ago', 'Inline Shortcode', 'notificationx' ),
156 + human_time_diff( $timestamp )
157 + );
158 + $_template = str_replace( '{{time}}', $diff_for_humans, $_template );
159 + }
160 + }
161 + }
162 + $output .= $_template;
163 + // $this->shortcode_nx_ids[] = $atts['id'];
164 + $output = "<div id='notificationx-shortcode-inline-{$atts['id']}' class='notificationx-shortcode-inline-wrapper nx-shortcode-notice'>$output</div>";
165 + }
166 + }
167 +
168 + return $output;
169 + }
170 +
171 +}