| 1 |
<?php |
| 2 |
/** |
| 3 |
* Flow shortcodes |
| 4 |
* |
| 5 |
* @package CartFlows |
| 6 |
*/ |
| 7 |
|
| 8 |
/** |
| 9 |
* Initialization |
| 10 |
* |
| 11 |
* @since 1.0.0 |
| 12 |
*/ |
| 13 |
class Cartflows_Flow_Shortcodes { |
| 14 |
|
| 15 |
|
| 16 |
/** |
| 17 |
* Member Variable |
| 18 |
* |
| 19 |
* @var instance |
| 20 |
*/ |
| 21 |
private static $instance; |
| 22 |
|
| 23 |
/** |
| 24 |
* Initiator |
| 25 |
*/ |
| 26 |
public static function get_instance() { |
| 27 |
if ( ! isset( self::$instance ) ) { |
| 28 |
self::$instance = new self; |
| 29 |
} |
| 30 |
return self::$instance; |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Constructor |
| 35 |
*/ |
| 36 |
public function __construct() { |
| 37 |
|
| 38 |
add_shortcode( 'cartflows_next_step_link', array( $this, 'next_step_link' ) ); |
| 39 |
|
| 40 |
add_shortcode( 'cartflows_navigation', array( $this, 'navigation_shortcode' ) ); |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Returns next step link |
| 45 |
* |
| 46 |
* @param array $atts attributes. |
| 47 |
* @return string |
| 48 |
*/ |
| 49 |
function next_step_link( $atts ) { |
| 50 |
|
| 51 |
global $post; |
| 52 |
|
| 53 |
$output = '#'; |
| 54 |
|
| 55 |
if ( $post && CARTFLOWS_STEP_POST_TYPE === $post->post_type ) { |
| 56 |
|
| 57 |
$navigation = false; |
| 58 |
|
| 59 |
$step_id = intval( $post->ID ); |
| 60 |
$flow_id = get_post_meta( $step_id, 'wcf-flow-id', true ); |
| 61 |
|
| 62 |
if ( ! $flow_id ) { |
| 63 |
return $output; |
| 64 |
} |
| 65 |
|
| 66 |
$steps = get_post_meta( $flow_id, 'wcf-steps', true ); |
| 67 |
|
| 68 |
if ( ! is_array( $steps ) || ( is_array( $steps ) && empty( $steps ) ) ) { |
| 69 |
return $output; |
| 70 |
} |
| 71 |
|
| 72 |
foreach ( $steps as $i => $step ) { |
| 73 |
|
| 74 |
if ( intval( $step['id'] ) === $step_id ) { |
| 75 |
|
| 76 |
$next_i = $i + 1; |
| 77 |
|
| 78 |
if ( isset( $steps[ $next_i ] ) ) { |
| 79 |
$navigation = $steps[ $next_i ]; |
| 80 |
} |
| 81 |
|
| 82 |
break; |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
if ( $navigation && is_array( $navigation ) ) { |
| 87 |
|
| 88 |
$output = get_permalink( $navigation['id'] ); |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
return $output; |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Navigation shortcode callback |
| 97 |
* |
| 98 |
* @param array $atts attributes for shortcode. |
| 99 |
* @return string |
| 100 |
*/ |
| 101 |
function navigation_shortcode( $atts ) { |
| 102 |
$atts = shortcode_atts( |
| 103 |
array( |
| 104 |
'label' => '', |
| 105 |
'icon' => '', |
| 106 |
'icon_position' => '', |
| 107 |
), |
| 108 |
$atts |
| 109 |
); |
| 110 |
|
| 111 |
global $post; |
| 112 |
|
| 113 |
$output = ''; |
| 114 |
|
| 115 |
if ( $post && CARTFLOWS_STEP_POST_TYPE === $post->post_type ) { |
| 116 |
|
| 117 |
$navigation = false; |
| 118 |
|
| 119 |
$step_id = intval( $post->ID ); |
| 120 |
$flow_id = get_post_meta( $step_id, 'wcf-flow-id', true ); |
| 121 |
|
| 122 |
if ( ! $flow_id ) { |
| 123 |
return $output; |
| 124 |
} |
| 125 |
|
| 126 |
$steps = get_post_meta( $flow_id, 'wcf-steps', true ); |
| 127 |
|
| 128 |
if ( ! is_array( $steps ) || ( is_array( $steps ) && empty( $steps ) ) ) { |
| 129 |
return $output; |
| 130 |
} |
| 131 |
|
| 132 |
foreach ( $steps as $i => $step ) { |
| 133 |
|
| 134 |
if ( intval( $step['id'] ) === $step_id ) { |
| 135 |
|
| 136 |
$next_i = $i + 1; |
| 137 |
|
| 138 |
if ( isset( $steps[ $next_i ] ) ) { |
| 139 |
$navigation = $steps[ $next_i ]; |
| 140 |
} |
| 141 |
|
| 142 |
break; |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
if ( $navigation && is_array( $navigation ) ) { |
| 147 |
|
| 148 |
$label = ( '' != $atts['label'] ) ? $atts['label'] : __( 'Next Step', 'cartflows' ); |
| 149 |
$before = ''; |
| 150 |
$after = ''; |
| 151 |
|
| 152 |
if ( '' != $atts['icon'] ) { |
| 153 |
if ( '' != $atts['icon_position'] ) { |
| 154 |
if ( 'before' == $atts['icon_position'] ) { |
| 155 |
$before = '<span class="wcf-nextstep-icon wcf-nextstep-icon-before"><i class="' . $atts['icon'] . '" aria-hidden="true"></i></span>'; |
| 156 |
} else { |
| 157 |
$after = '<span class="wcf-nextstep-icon wcf-nextstep-icon-after"><i class="' . $atts['icon'] . '" aria-hidden="true"></i></span>'; |
| 158 |
} |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
$output = '<div><a target="_self" href="' . get_permalink( $navigation['id'] ) . '">' . $before . $label . $after . '</a></div>'; |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
return $output; |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
/** |
| 171 |
* Kicking this off by calling 'get_instance()' method |
| 172 |
*/ |
| 173 |
Cartflows_Flow_Shortcodes::get_instance(); |
| 174 |
|