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