| @@ -1,0 +1,161 @@ | ||
| 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 | + $output = $extension->show_inline_notification( $atts, $settings); | |
| 86 | + if( !empty( $output ) ) { | |
| 87 | + $output = "<div id='notificationx-shortcode-inline-{$atts['id']}' class='notificationx-shortcode-inline-wrapper nx-shortcode-notice'>$output</div>"; | |
| 88 | + } | |
| 89 | + return $output; | |
| 90 | + } | |
| 91 | + | |
| 92 | + $result = FrontEnd::get_instance()->get_notifications_data( [ 'shortcode' => [ $nx_id ] ] ); | |
| 93 | + | |
| 94 | + $output = ''; | |
| 95 | + if ( ! empty( $result['shortcode'][ $nx_id ]['entries'] ) ) { | |
| 96 | + $entries = $result['shortcode'][ $nx_id ]['entries']; | |
| 97 | + $entries = array_values( $entries ); | |
| 98 | + $settings = $result['shortcode'][ $nx_id ]['post']; | |
| 99 | + | |
| 100 | + $logged_in = is_user_logged_in(); | |
| 101 | + $show_on_display = isset($settings['show_on_display']) ? $settings['show_on_display'] : ''; | |
| 102 | + if ( ! ( ( $logged_in && 'logged_out_user' === $show_on_display ) || ( ! $logged_in && 'logged_in_user' === $show_on_display ) ) ) { | |
| 103 | + | |
| 104 | + $col = array_column( $entries, 'timestamp' ); | |
| 105 | + if ( count( $col ) == count( $entries ) ) { | |
| 106 | + array_multisort( $col, SORT_ASC, $entries ); | |
| 107 | + } | |
| 108 | + $entry = end( $entries ); | |
| 109 | + $template = Inline::get_instance()->get_template( $settings ); | |
| 110 | + $_template = $template; | |
| 111 | + | |
| 112 | + foreach ( $entry as $key => $val ) { | |
| 113 | + if ( ! is_array( $val ) ) { | |
| 114 | + if ( 'rating' === $key ) { | |
| 115 | + $count = $val; | |
| 116 | + $val = "<span style='white-space: nowrap'>"; | |
| 117 | + for ( $i = 1; $i <= 5; $i++ ) { | |
| 118 | + $val .= $i <= $count | |
| 119 | + ? '<span style="color:#ffc107">★</span>' | |
| 120 | + : '<span style="color:#eeeeee">★</span>'; | |
| 121 | + } | |
| 122 | + $val .= '</span>'; | |
| 123 | + } elseif ( | |
| 124 | + ! empty( $entry['link'] ) && | |
| 125 | + in_array( $key, [ 'plugin_name', 'product_title', 'post_title', 'plugin_theme_name', 'course_title', 'title' ], true ) && | |
| 126 | + 'form' !== $settings['type'] && | |
| 127 | + 'email_subscription' !== $settings['type'] && | |
| 128 | + 'page_analytics' !== $settings['type'] | |
| 129 | + ) { | |
| 130 | + $link = $entry['link']; | |
| 131 | + $target = ! empty( $settings['link_open'] ) ? 'target="_blank"' : ''; | |
| 132 | + if ( 'false' === $atts['show_link'] ) { | |
| 133 | + $target = ''; | |
| 134 | + $link = 'javascript:void(0)'; | |
| 135 | + } | |
| 136 | + $val = "<a href='$link' $target>$val</a>"; | |
| 137 | + } | |
| 138 | + $_template = str_replace( "{{{$key}}}", $val, $_template ); | |
| 139 | + } | |
| 140 | + if ( ! empty( $entry['timestamp'] ) && strpos( $_template, '{{time}}' ) !== false ) { | |
| 141 | + $timestamp = $entry['timestamp']; | |
| 142 | + if ( $timestamp ) { | |
| 143 | + $diff_for_humans = sprintf( | |
| 144 | + /* translators: time */ | |
| 145 | + _x( '%s ago', 'Inline Shortcode', 'notificationx' ), | |
| 146 | + human_time_diff( $timestamp ) | |
| 147 | + ); | |
| 148 | + $_template = str_replace( '{{time}}', $diff_for_humans, $_template ); | |
| 149 | + } | |
| 150 | + } | |
| 151 | + } | |
| 152 | + $output .= $_template; | |
| 153 | + // $this->shortcode_nx_ids[] = $atts['id']; | |
| 154 | + $output = "<div id='notificationx-shortcode-inline-{$atts['id']}' class='notificationx-shortcode-inline-wrapper nx-shortcode-notice'>$output</div>"; | |
| 155 | + } | |
| 156 | + } | |
| 157 | + | |
| 158 | + return $output; | |
| 159 | + } | |
| 160 | + | |
| 161 | +} | |