| 1 |
<?php if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly |
| 2 |
|
| 3 |
|
| 4 |
function zotpress_strip_quotes($string) |
| 5 |
{ |
| 6 |
// Strip quotes and decode |
| 7 |
$string = str_replace("”", "", str_replace('"','', html_entity_decode( $string ))); |
| 8 |
return $string; |
| 9 |
} |
| 10 |
|
| 11 |
function Zotpress_zotpressInText ($atts) |
| 12 |
{ |
| 13 |
/* |
| 14 |
* GLOBAL VARIABLES |
| 15 |
* |
| 16 |
* $GLOBALS['zp_shortcode_instances'] {instantiated in zotpress.php} |
| 17 |
* |
| 18 |
*/ |
| 19 |
|
| 20 |
// extract(shortcode_atts(array( |
| 21 |
$atts = shortcode_atts(array( |
| 22 |
|
| 23 |
'item' => false, |
| 24 |
'items' => false, |
| 25 |
|
| 26 |
'pages' => false, |
| 27 |
'format' => "(%a%, %d%, %p%)", |
| 28 |
'brackets' => false, |
| 29 |
'etal' => false, // default (false), yes, no |
| 30 |
'separator' => false, // default (comma), semicolon |
| 31 |
'and' => false, // ampersand [default], and, comma, comma-amp, comma-and |
| 32 |
|
| 33 |
'userid' => false, |
| 34 |
'api_user_id' => false, |
| 35 |
'nickname' => false, |
| 36 |
'nick' => false |
| 37 |
|
| 38 |
), $atts); |
| 39 |
|
| 40 |
global $post, $wpdb; |
| 41 |
|
| 42 |
|
| 43 |
// +---------------------------+ |
| 44 |
// | FORMAT & CLEAN PARAMETERS | |
| 45 |
// +---------------------------+ |
| 46 |
|
| 47 |
if ( $atts['items'] ) |
| 48 |
$atts['items'] = zotpress_strip_quotes( str_replace(" ", "", $atts['items'] )); |
| 49 |
elseif ( $atts['item'] ) |
| 50 |
$atts['items'] = zotpress_strip_quotes( str_replace(" ", "", $atts['item'] )); |
| 51 |
|
| 52 |
// 7.4.3 via 3.9.10: Use the Zotpress_prep_ajax_request_vars() function on intext |
| 53 |
$zpr = Zotpress_prep_ajax_request_vars($wpdb, $atts); |
| 54 |
|
| 55 |
$items = $atts['items']; |
| 56 |
|
| 57 |
$pages = $zpr['pages']; // 7.4.3: Is this used? |
| 58 |
$format = $zpr['format']; |
| 59 |
$brackets = $zpr['brackets']; |
| 60 |
$etal = $zpr['etal']; |
| 61 |
$separator = $zpr['separator']; |
| 62 |
$and = $zpr['and']; |
| 63 |
|
| 64 |
$api_user_id = $zpr['api_user_id']; |
| 65 |
$nickname = $zpr['nickname']; |
| 66 |
|
| 67 |
|
| 68 |
// // PREPARE ATTRIBUTES |
| 69 |
// if ( $items ) |
| 70 |
// $items = zotpress_strip_quotes( str_replace(" ", "", $items )); |
| 71 |
// elseif ( $item ) |
| 72 |
// $items = zotpress_strip_quotes( str_replace(" ", "", $item )); |
| 73 |
|
| 74 |
// $pages = zotpress_strip_quotes( $pages ); |
| 75 |
// $format = zotpress_strip_quotes( $format ); |
| 76 |
// $brackets = zotpress_strip_quotes( $brackets ); |
| 77 |
|
| 78 |
// $etal = zotpress_strip_quotes( $etal ); |
| 79 |
// if ( $etal == "default" ) $etal = false; |
| 80 |
|
| 81 |
// $separator = zotpress_strip_quotes( $separator ); |
| 82 |
// if ( $separator == "default" ) $separator = false; |
| 83 |
|
| 84 |
// $and = zotpress_strip_quotes( $and ); |
| 85 |
// if ( $and == "default" ) $and = false; |
| 86 |
|
| 87 |
// if ( $userid ) $api_user_id = zotpress_strip_quotes( $userid ); |
| 88 |
// if ( $nickname ) $nickname = zotpress_strip_quotes( $nickname ); |
| 89 |
// if ( $nick ) $nickname = zotpress_strip_quotes( $nick ); |
| 90 |
|
| 91 |
|
| 92 |
// GET ACCOUNTS |
| 93 |
|
| 94 |
// global $wpdb; |
| 95 |
// global $post; |
| 96 |
|
| 97 |
// Turn on/off minified versions if testing/live |
| 98 |
$minify = ''; if ( ZOTPRESS_LIVEMODE ) $minify = '.min'; |
| 99 |
|
| 100 |
wp_enqueue_script( 'zotpress.shortcode.intext'.$minify.'.js' ); |
| 101 |
|
| 102 |
$zp_account = false; |
| 103 |
|
| 104 |
if ( $nickname !== false ) |
| 105 |
{ |
| 106 |
$zp_account = $wpdb->get_row( |
| 107 |
$wpdb->prepare( |
| 108 |
" |
| 109 |
SELECT * FROM `".$wpdb->prefix."zotpress` |
| 110 |
WHERE `nickname`=%s |
| 111 |
", |
| 112 |
array( $nickname ) |
| 113 |
), OBJECT |
| 114 |
); |
| 115 |
|
| 116 |
if ( $zp_account !== null ) |
| 117 |
$api_user_id = $zp_account->api_user_id; |
| 118 |
} |
| 119 |
elseif ( $api_user_id !== false ) |
| 120 |
{ |
| 121 |
$zp_account = $wpdb->get_row( |
| 122 |
$wpdb->prepare( |
| 123 |
" |
| 124 |
SELECT * FROM `".$wpdb->prefix."zotpress` |
| 125 |
WHERE `api_user_id`=%s |
| 126 |
", |
| 127 |
array( $api_user_id ) |
| 128 |
), OBJECT |
| 129 |
); |
| 130 |
|
| 131 |
if ( $zp_account !== null ) |
| 132 |
$api_user_id = $zp_account->api_user_id; |
| 133 |
} |
| 134 |
elseif ( $api_user_id === false |
| 135 |
&& $nickname === false ) |
| 136 |
{ |
| 137 |
if ( get_option("Zotpress_DefaultAccount") !== false ) |
| 138 |
{ |
| 139 |
$api_user_id = get_option("Zotpress_DefaultAccount"); |
| 140 |
|
| 141 |
$zp_account = $wpdb->get_row( |
| 142 |
$wpdb->prepare( |
| 143 |
" |
| 144 |
SELECT * FROM `".$wpdb->prefix."zotpress` |
| 145 |
WHERE `api_user_id`=%s |
| 146 |
", |
| 147 |
array( $api_user_id ) |
| 148 |
), OBJECT |
| 149 |
); |
| 150 |
} |
| 151 |
else // When all else fails ... assume one account |
| 152 |
{ |
| 153 |
$zp_account = $wpdb->get_row("SELECT * FROM ".$wpdb->prefix."zotpress LIMIT 1", OBJECT); |
| 154 |
$api_user_id = $zp_account->api_user_id; |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
// Format in-text items: |
| 159 |
// Handle the possible formats of item/s for in-text |
| 160 |
// |
| 161 |
// IN-TEXT FORMATS: |
| 162 |
// [zotpressInText item="NCXAA92F"] |
| 163 |
// [zotpressInText item="{NCXAA92F}"] |
| 164 |
// [zotpressInText item="{NCXAA92F,10-15}"] |
| 165 |
// [zotpressInText items="{NCXAA92F,10-15},{55MKF89B,1578},{3ITTIXHP}"] |
| 166 |
// [zotpressInText items="{000001:NCXAA92F,10-15},{000003:3ITTIXHP}"] |
| 167 |
// So no multiples without curlies or non-curlies in multiples |
| 168 |
|
| 169 |
$all_page_instances = array(); |
| 170 |
$all_page_instances_str = ""; |
| 171 |
|
| 172 |
// // Add `ppp` in front of pages, so we can ignore pages later |
| 173 |
// $intextitem["items"] = preg_replace( "/((?=[^}]),(?=[^{]))+/", ",ppp", $intextitem["items"] ); |
| 174 |
// REVIEW: Actually, let's just remove pages |
| 175 |
// $items = preg_replace( "/(((,))+([\w\d-]+(})+))++/", "}", $items ); |
| 176 |
// REVIEW: Actually, we need them for now, remove later |
| 177 |
// $items = preg_replace( "/((?=[^}]),(?=[^{]))+/", ",ppp", $items ); |
| 178 |
|
| 179 |
// Add api_user_id if not there |
| 180 |
// if ( strpos( $items, $api_user_id ) === false ) // WRONG: assumes default/global api_user_id rather than the one for this shortcode |
| 181 |
if ( strpos( $items, ":" ) === false ) |
| 182 |
{ |
| 183 |
if ( strpos( $items, "{" ) !== false ) { |
| 184 |
$items = str_replace( "{", "{".$api_user_id.":", $items ); |
| 185 |
} elseif ( strpos( $items, "," ) !== false ) { |
| 186 |
$items = "{".$api_user_id.":" . str_replace( ",", "},{".$api_user_id.":", $items )."}"; |
| 187 |
} else // assume unformatted and single, so place at front |
| 188 |
{ |
| 189 |
$items = "{".$api_user_id.":".$items."}"; |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
// Determine page instances and where |
| 194 |
$temp_items = explode( "},{", $items ); |
| 195 |
$all_np = true; |
| 196 |
|
| 197 |
foreach ( $temp_items as $id => $item ) |
| 198 |
{ |
| 199 |
// if ( strpos( $item, "," ) !== false ) // assume page/s |
| 200 |
// if ( preg_match( '/,(.)+\}/', $item, $match ) == 1 ) |
| 201 |
if ( preg_match( '/,(.)+/', $item, $match ) == 1 ) |
| 202 |
{ |
| 203 |
$temp_arr_page_ins = []; |
| 204 |
|
| 205 |
// First, check for multiple, non-contiguous page numbers in parentheses |
| 206 |
// NOTE: array_filter will filter out anything that is "false," including 0 |
| 207 |
if ( preg_match( '/(.)+/', $match[0], $matchm ) == 1 ) |
| 208 |
$temp_arr_page_ins = array_filter( explode(',', str_replace('(', '', str_replace(')', '', $match[0]))) ); |
| 209 |
else |
| 210 |
$temp_arr_page_ins = $match[0]; |
| 211 |
|
| 212 |
// Then go through all and format |
| 213 |
foreach ( $temp_arr_page_ins as $pid => $page_ins ) |
| 214 |
{ |
| 215 |
if ( $page_ins == '' ) |
| 216 |
continue; |
| 217 |
|
| 218 |
$temp_arr_page_ins[$pid] = str_replace( "}", "", str_replace( ",", "", $page_ins ) ); |
| 219 |
// $all_np = false; |
| 220 |
} |
| 221 |
$all_page_instances[$id] = $temp_arr_page_ins; |
| 222 |
|
| 223 |
if ( strlen($all_page_instances_str) > 0 ) |
| 224 |
$all_page_instances_str = $all_page_instances_str . '--'; |
| 225 |
$all_page_instances_str = $all_page_instances_str . join('++', $temp_arr_page_ins); |
| 226 |
|
| 227 |
// $all_page_instances[$id] = str_replace( "}", "", str_replace( ",", "", $match[0] ) ); |
| 228 |
$all_np = false; |
| 229 |
} |
| 230 |
else |
| 231 |
{ |
| 232 |
$all_page_instances[$id] = "np"; |
| 233 |
|
| 234 |
if ( strlen($all_page_instances_str) > 0 ) |
| 235 |
$all_page_instances_str = $all_page_instances_str . '--'; |
| 236 |
$all_page_instances_str = $all_page_instances_str . 'np'; |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
// REVIEW: Replace ndashes and mdashes with dashes (7.3) |
| 241 |
$items = str_replace("–", "-", str_replace("–", "-", $items)); |
| 242 |
|
| 243 |
// Remove pages from item key/s |
| 244 |
$items = preg_replace( "/(((,))+([\w\d-]+(})+))++/", "}", $items ); |
| 245 |
$items = preg_replace( "/,\([\w\d-]+,+[\w\d-]+\)}/", "}", $items ); |
| 246 |
unset($temp_items); |
| 247 |
|
| 248 |
|
| 249 |
// Generate instance id for shortcode |
| 250 |
// REVIEW: Changed for new item format |
| 251 |
// e.g., zp-ID--66010-FKNL6ECC-_-66010-FZF9BN8L--wp406 |
| 252 |
// $instance_id = "zp-ID-".$api_user_id."-" . str_replace( " ", "_", str_replace( "&", "_", str_replace( "+", "_", str_replace( "/", "_", str_replace( "{", "-", str_replace( "}", "-", str_replace( ",", "_", $items ) ) ) ) ) ) ) ."-".$post->ID; |
| 253 |
$instance_id = "zp-InText-zp-ID-" . str_replace( " ", "_", str_replace( "&", "_", str_replace( "+", "_", str_replace( "/", "_", str_replace( "{", "-", str_replace( "}", "-", str_replace( ":", "-", str_replace( ",", "_", $items ) ) ) ) ) ) ) ) ."-wp".$post->ID; |
| 254 |
|
| 255 |
// Set up array for this post, if it doesn't exist |
| 256 |
if ( ! isset( $GLOBALS['zp_shortcode_instances'][$post->ID] ) ) |
| 257 |
$GLOBALS['zp_shortcode_instances'][$post->ID] = array(); |
| 258 |
|
| 259 |
// Determine if all items are np |
| 260 |
if ( $all_np ) |
| 261 |
$all_page_instances_str = "np"; |
| 262 |
|
| 263 |
// Then, add the instance to the array |
| 264 |
// REVIEW: Don't need api_user_id ... or maybe need multiple? |
| 265 |
// $GLOBALS['zp_shortcode_instances'][$post->ID][] = array( "instance_id" => $instance_id, "api_user_id" =>$api_user_id, "items" => $items ); |
| 266 |
$GLOBALS['zp_shortcode_instances'][$post->ID][] = array( |
| 267 |
"instance_id" => $instance_id, |
| 268 |
"items" => $items, |
| 269 |
"page_instances" => $all_page_instances |
| 270 |
); |
| 271 |
|
| 272 |
// Show theme scripts |
| 273 |
$GLOBALS['zp_is_shortcode_displayed'] = true; |
| 274 |
|
| 275 |
// Output attributes and loading |
| 276 |
// REVIEW: Changed for new format |
| 277 |
// return '<span id="zp-InText-'.$instance_id."-".count($GLOBALS['zp_shortcode_instances'][$post->ID]).'" |
| 278 |
// class="zp-InText-Citation loading" |
| 279 |
// rel="{ \'api_user_id\': \''.$api_user_id.'\', \'pages\': \''.$pages.'\', \'items\': \''.$items.'\', \'format\': \''.$format.'\', \'brackets\': \''.$brackets.'\', \'etal\': \''.$etal.'\', \'separator\': \''.$separator.'\', \'and\': \''.$and.'\' }"></span>'; |
| 280 |
// $output = '<span class="'.$instance_id.' zp-InText-Citation loading" rel="{ \'pages\': \''.join("--", $all_page_instances).'\', \'items\': \''.$items.'\', \'format\': \''.$format.'\', \'brackets\': \''.$brackets.'\', \'etal\': \''.$etal.'\', \'separator\': \''.$separator.'\', \'and\': \''.$and.'\' }"></span>'; |
| 281 |
$output = '<span class="'.$instance_id.' zp-InText-Citation loading" rel="{ \'pages\': \''.$all_page_instances_str.'\', \'items\': \''.$items.'\', \'format\': \''.$format.'\', \'brackets\': \''.$brackets.'\', \'etal\': \''.$etal.'\', \'separator\': \''.$separator.'\', \'and\': \''.$and.'\' }"></span>'; |
| 282 |
|
| 283 |
return $output; |
| 284 |
} |
| 285 |
|
| 286 |
?> |