[ Index ] |
MailPress 7.2 |
[ Index ] [ Classes ] [ Functions ] [ Variables ] [ Constants ] [ Statistics ] |
[Summary view] [Print] [Text view]
1 <?php 2 class MP_Query 3 { 4 5 var $query; 6 var $query_vars = array(); 7 var $queried_object; 8 var $queried_object_id; 9 var $request; 10 var $mails; 11 var $mail_count = 0; 12 var $current_mail = -1; 13 var $in_the_loop = false; 14 var $mail; 15 var $found_mails = 0; 16 17 function __construct( $query = '' ) 18 { 19 if ( !empty( $query ) ) $this->query( $query ); 20 } 21 22 function &query( $query = '' ) 23 { 24 $this->parse_query( $query ); 25 return $this->get_mails(); 26 } 27 28 function parse_query( $query ) 29 { 30 if ( !empty( $query ) || !isset( $this->query ) ) 31 { 32 $this->init(); 33 if ( is_array( $query ) ) $this->query_vars = $query; 34 else parse_str( $query, $this->query_vars ); 35 $this->query = $query; 36 } 37 38 $this->query_vars = $this->fill_query_vars( $this->query_vars ); 39 $qv = &$this->query_vars; 40 41 $absints = array( 42 'day' 43 , 'm' 44 , 'mail' 45 , 'monthnum' 46 , 'w' 47 , 'year' 48 ); 49 foreach ( $absints as $absint ) if ( isset( $qv[$absint] ) ) $qv[$absint] = absint( $qv[$absint] ); 50 if ( '' !== $qv['hour'] ) $qv['hour'] = absint( $qv['hour'] ); 51 if ( '' !== $qv['minute'] ) $qv['minute'] = absint( $qv['minute'] ); 52 if ( '' !== $qv['second'] ) $qv['second'] = absint( $qv['second'] ); 53 } 54 55 function init() 56 { 57 unset( $this->mails ); 58 unset( $this->query ); 59 $this->query_vars = array(); 60 unset( $this->queried_object ); 61 unset( $this->queried_object_id ); 62 $this->mail_count = 0; 63 $this->current_mail = -1; 64 $this->in_the_loop = false; 65 66 $this->init_query_flags(); 67 } 68 69 function init_query_flags() {} 70 71 function fill_query_vars( $array ) { 72 $keys = array( 73 'error' 74 75 , 'day' 76 , 'w' 77 , 'monthnum' 78 , 'year' 79 , 'm' 80 , 'hour' 81 , 'minute' 82 , 'second' 83 84 , 'mail' 85 , 'Theme' 86 , 'Template' 87 88 , 'fromemail' 89 , 'fromname' 90 , 'toemail' 91 , 'author_name' 92 93 , 'meta_key' 94 , 'meta_value' 95 ,'paged' 96 , 's' 97 ); 98 99 foreach ( $keys as $key ) if ( !isset( $array[$key] ) ) $array[$key] = ''; 100 101 $array_keys = array( 102 'emails' 103 , 'mail__in' 104 , 'mail__not_in' 105 , 'Theme__in' 106 , 'Theme__not_in' 107 , 'Template__in' 108 , 'Template__not_in' 109 ); 110 111 foreach ( $array_keys as $key ) if ( !isset( $array[$key] ) ) $array[$key] = array(); 112 113 return $array; 114 } 115 116 function parse_query_vars() 117 { 118 $this->parse_query( '' ); 119 } 120 121 function get( $query_var ) 122 { 123 return ( isset( $this->query_vars[$query_var] ) ) ? $this->query_vars[$query_var] : ''; 124 } 125 126 function set( $query_var, $value ) 127 { 128 $this->query_vars[$query_var] = $value; 129 } 130 131 function &get_mails() { 132 global $wpdb; 133 134 // Shorthand. 135 $q = &$this->query_vars; 136 137 $q = $this->fill_query_vars( $q ); 138 139 // First let's clear some variables 140 $distinct = ''; 141 $whichauthor = ''; 142 $where = ''; 143 $limits = ''; 144 $join = ''; 145 $search = ''; 146 $groupby = ''; 147 $fields = "$wpdb->mp_mails.*"; 148 $mail_status_join = false; 149 $page = 1; 150 151 if ( !isset( $q['caller_get_mails'] ) ) $q['caller_get_mails'] = false; 152 153 // if ( !isset( $q['suppress_filters'] ) ) 154 $q['suppress_filters'] = true; 155 156 if ( !isset( $q['mails_per_page'] ) || $q['mails_per_page'] == 0 ) $q['mails_per_page'] = get_option( 'posts_per_page' ); 157 if ( isset( $q['showmails'] ) && $q['showmails'] ) 158 { 159 $q['showmails'] = ( int ) $q['showmails']; 160 $q['mails_per_page'] = $q['showmails']; 161 } 162 if ( !isset( $q['nopaging'] ) ) $q['nopaging'] = ( $q['mails_per_page'] == -1 ); 163 164 $q['mails_per_page'] = ( int ) $q['mails_per_page']; 165 if ( $q['mails_per_page'] < -1 ) $q['mails_per_page'] = abs( $q['mails_per_page'] ); 166 elseif ( $q['mails_per_page'] == 0 ) $q['mails_per_page'] = 1; 167 168 // If true, forcibly turns off SQL_CALC_FOUND_ROWS even when limits are present. 169 if ( isset( $q['no_found_rows'] ) ) 170 $q['no_found_rows'] = ( bool ) $q['no_found_rows']; 171 else 172 $q['no_found_rows'] = false; 173 174 // If a month is specified in the querystring, load that month 175 if ( $q['m'] ) { 176 $q['m'] = '' . preg_replace( '|[^0-9]|', '', $q['m'] ); 177 $where .= " AND YEAR( $wpdb->mp_mails.sent )=" . substr( $q['m'], 0, 4 ); 178 if ( strlen( $q['m'] ) > 5 ) 179 $where .= " AND MONTH( $wpdb->mp_mails.sent )=" . substr( $q['m'], 4, 2 ); 180 if ( strlen( $q['m'] ) > 7 ) 181 $where .= " AND DAYOFMONTH( $wpdb->mp_mails.sent )=" . substr( $q['m'], 6, 2 ); 182 if ( strlen( $q['m'] ) > 9 ) 183 $where .= " AND HOUR( $wpdb->mp_mails.sent )=" . substr( $q['m'], 8, 2 ); 184 if ( strlen( $q['m'] ) > 11 ) 185 $where .= " AND MINUTE( $wpdb->mp_mails.sent )=" . substr( $q['m'], 10, 2 ); 186 if ( strlen( $q['m'] ) > 13 ) 187 $where .= " AND SECOND( $wpdb->mp_mails.sent )=" . substr( $q['m'], 12, 2 ); 188 } 189 190 if ( '' !== $q['hour'] ) 191 $where .= " AND HOUR( $wpdb->mp_mails.sent ) = '" . $q['hour'] . "'"; 192 193 if ( '' !== $q['minute'] ) 194 $where .= " AND MINUTE( $wpdb->mp_mails.sent ) = '" . $q['minute'] . "'"; 195 196 if ( '' !== $q['second'] ) 197 $where .= " AND SECOND( $wpdb->mp_mails.sent ) = '" . $q['second'] . "'"; 198 199 if ( $q['year'] ) 200 $where .= " AND YEAR( $wpdb->mp_mails.sent ) = '" . $q['year'] . "'"; 201 202 if ( $q['monthnum'] ) 203 $where .= " AND MONTH( $wpdb->mp_mails.sent ) = '" . $q['monthnum'] . "'"; 204 205 if ( $q['day'] ) 206 $where .= " AND DAYOFMONTH( $wpdb->mp_mails.sent ) = '" . $q['day'] . "'"; 207 208 if ( $q['w'] ) 209 $where .= ' AND ' . _wp_mysql_week( "`$wpdb->mp_mails`.`sent`" ) . " = '" . $q['w'] . "'"; 210 211 // If a mail number is specified, load that mail 212 if ( $q['mail'] ) 213 { 214 $where .= " AND {$wpdb->mp_mails}.id = " . $q['mail']; 215 } 216 elseif ( $q['mail__in'] ) 217 { 218 $mail__in = implode( ',', array_map( 'absint', $q['mail__in'] ) ); 219 $where .= " AND {$wpdb->mp_mails}.id IN ( $mail__in )"; 220 } 221 elseif ( $q['mail__not_in'] ) 222 { 223 $mail__not_in = implode( ',', array_map( 'absint', $q['mail__not_in'] ) ); 224 $where .= " AND {$wpdb->mp_mails}.id NOT IN ( $mail__not_in )"; 225 } 226 227 // If a Theme is specified, load that mail 228 if ( $q['Theme'] ) 229 { 230 $where .= " AND {$wpdb->mp_mails}.theme = '" . $q['Theme'] . "'"; 231 } 232 elseif ( $q['Theme__in'] ) 233 { 234 $Theme__in = implode( "','", $q['Theme__in'] ); 235 $where .= " AND {$wpdb->mp_mails}.theme IN ( '$Theme__in' )"; 236 } 237 elseif ( $q['Theme__not_in'] ) 238 { 239 $Theme__not_in = implode( "','", $q['Theme__not_in'] ); 240 $where .= " AND {$wpdb->mp_mails}.theme NOT IN ( '$Theme__not_in' )"; 241 } 242 243 // If a Template is specified, load that mail 244 if ( $q['Template'] ) 245 { 246 $where .= " AND {$wpdb->mp_mails}.template = '" . $q['Template'] . "'"; 247 } 248 elseif ( $q['Template__in'] ) 249 { 250 $Template__in = implode( "','", $q['Template__in'] ); 251 $where .= " AND {$wpdb->mp_mails}.template IN ( '$Template__in' )"; 252 } 253 elseif ( $q['Template__not_in'] ) 254 { 255 $Template__not_in = implode( "','", $q['Template__not_in'] ); 256 $where .= " AND {$wpdb->mp_mails}.template NOT IN ( '$Template__not_in' )"; 257 } 258 259 // If a fromemail is specified, load that mail 260 if ( $q['fromemail'] ) 261 { 262 $where .= " AND {$wpdb->mp_mails}.fromemail = '" . $q['fromemail'] . "'"; 263 } 264 265 // If a fromname is specified, load that mail 266 if ( $q['fromname'] ) 267 { 268 $where .= " AND {$wpdb->mp_mails}.fromname = '" . $q['fromname'] . "'"; 269 } 270 271 // If a fromemail is specified, load that mail 272 if ( $q['toemail'] ) 273 { 274 $where .= " AND {$wpdb->mp_mails}.toemail LIKE '%" . $q['toemail'] . "%'"; 275 } 276 277 // If a search pattern is specified, load the mails that match 278 if ( !empty( $q['s'] ) ) { 279 // added slashes screw with quote grouping when done early, so done later 280 $q['s'] = stripslashes( $q['s'] ); 281 if ( !empty( $q['sentence'] ) ) { 282 $q['search_terms'] = array( $q['s'] ); 283 } else { 284 preg_match_all( '/".*?("|$)|((?<=[\\s",+])|^)[^\\s",+]+/', $q['s'], $matches ); 285 $q['search_terms'] = array_map( '_search_terms_tidy', $matches[0] ); 286 } 287 $n = !empty( $q['exact'] ) ? '' : '%'; 288 $searchand = ''; 289 foreach( ( array ) $q['search_terms'] as $term ) { 290 $term = addslashes_gpc( $term ); 291 $search .= "{$searchand}( ( $wpdb->mp_mails.toemail LIKE '{$n}{$term}{$n}' ) OR ( $wpdb->mp_mails.toname LIKE '{$n}{$term}{$n}' ) ) OR ( $wpdb->mp_mails.subject LIKE '{$n}{$term}{$n}' ) ) OR ( $wpdb->mp_mails.html LIKE '{$n}{$term}{$n}' ) ) OR ( $wpdb->mp_mails.plaintext LIKE '{$n}{$term}{$n}' ) )"; 292 $searchand = ' AND '; 293 } 294 $term = esc_sql( $q['s'] ); 295 if ( empty( $q['sentence'] ) && count( $q['search_terms'] ) > 1 && $q['search_terms'][0] != $q['s'] ) 296 $search .= " OR ( $wpdb->mp_mails.toemail LIKE '{$n}{$term}{$n}' ) OR ( $wpdb->mp_mails.toname LIKE '{$n}{$term}{$n}' ) ) OR ( $wpdb->mp_mails.subject LIKE '{$n}{$term}{$n}' ) ) OR ( $wpdb->mp_mails.html LIKE '{$n}{$term}{$n}' ) ) OR ( $wpdb->mp_mails.plaintext LIKE '{$n}{$term}{$n}' ) )"; 297 298 if ( !empty( $search ) ) $search = " AND ( {$search} ) "; 299 } 300 301 // Allow plugins to contextually add/remove/modify the search section of the database query 302 $search = apply_filters_ref_array( 'MailPress_mails_search', array( $search, $this ) ); 303 304 // Author/user stuff 305 306 if ( empty( $q['author'] ) || ( $q['author'] == '0' ) ) { 307 $whichauthor = ''; 308 } else { 309 $q['author'] = ( string )urldecode( $q['author'] ); 310 $q['author'] = addslashes_gpc( $q['author'] ); 311 if ( strpos( $q['author'], '-' ) !== false ) { 312 $eq = '!='; 313 $andor = 'AND'; 314 $q['author'] = explode( '-', $q['author'] ); 315 $q['author'] = ( string )absint( $q['author'][1] ); 316 } else { 317 $eq = '='; 318 $andor = 'OR'; 319 } 320 $author_array = preg_split( '/[,\s]+/', $q['author'] ); 321 $_author_array = array(); 322 foreach ( $author_array as $key => $_author ) 323 $_author_array[] = "$wpdb->posts.post_author " . $eq . ' ' . absint( $_author ); 324 $whichauthor .= ' AND ( ' . implode( " $andor ", $_author_array ) . ' )'; 325 unset( $author_array, $_author_array ); 326 } 327 328 // Author stuff for nice URLs 329 330 if ( '' != $q['author_name'] ) { 331 if ( strpos( $q['author_name'], '/' ) !== false ) { 332 $q['author_name'] = explode( '/', $q['author_name'] ); 333 if ( $q['author_name'][ count( $q['author_name'] )-1 ] ) { 334 $q['author_name'] = $q['author_name'][count( $q['author_name'] )-1]; // no trailing slash 335 } else { 336 $q['author_name'] = $q['author_name'][count( $q['author_name'] )-2]; // there was a trailling slash 337 } 338 } 339 $q['author_name'] = sanitize_title( $q['author_name'] ); 340 $q['author'] = get_user_by( 'slug', $q['author_name'] ); 341 if ( $q['author'] ) 342 $q['author'] = $q['author']->ID; 343 $whichauthor .= " AND ( $wpdb->posts.post_author = " . absint( $q['author'] ) . ' )'; 344 } 345 346 $where .= $search . $whichauthor; 347 348 if ( empty( $q['order'] ) || ( ( strtoupper( $q['order'] ) != 'ASC' ) && ( strtoupper( $q['order'] ) != 'DESC' ) ) ) 349 $q['order'] = 'DESC'; 350 351 // Order by 352 if ( empty( $q['orderby'] ) ) 353 { 354 $q['orderby'] = "$wpdb->mp_mails.sent " . $q['order']; 355 } 356 elseif ( 'none' == $q['orderby'] ) 357 { 358 $q['orderby'] = ''; 359 } 360 else 361 { 362 // Used to filter values 363 $allowed_keys = array( 'id', 'author', 'sent', 'subject', 'rand' ); 364 if ( !empty( $q['meta_key'] ) ) { 365 $allowed_keys[] = $q['meta_key']; 366 $allowed_keys[] = 'meta_value'; 367 $allowed_keys[] = 'meta_value_num'; 368 } 369 $q['orderby'] = urldecode( $q['orderby'] ); 370 $q['orderby'] = addslashes_gpc( $q['orderby'] ); 371 $orderby_array = explode( ' ', $q['orderby'] ); 372 $q['orderby'] = ''; 373 374 foreach ( $orderby_array as $i => $orderby ) { 375 // Only allow certain values for safety 376 if ( ! in_array( $orderby, $allowed_keys ) ) 377 continue; 378 379 switch ( $orderby ) { 380 case 'id': 381 $orderby = "$wpdb->mp_mails.id"; 382 break; 383 case 'rand': 384 $orderby = 'RAND()'; 385 break; 386 case $q['meta_key']: 387 case 'meta_value': 388 $orderby = "$wpdb->mp_mailmeta.meta_value"; 389 break; 390 case 'meta_value_num': 391 $orderby = "$wpdb->mp_mailmeta.meta_value+0"; 392 break; 393 default: 394 $orderby = "$wpdb->mp_mails." . $orderby; 395 } 396 397 $q['orderby'] .= ( ( $i == 0 ) ? '' : ',' ) . $orderby; 398 } 399 400 // append ASC or DESC at the end 401 if ( !empty( $q['orderby'] ) ) 402 $q['orderby'] .= " {$q['order']}"; 403 404 if ( empty( $q['orderby'] ) ) 405 $q['orderby'] = "$wpdb->mp_mails.sent ".$q['order']; 406 } 407 408 // mailmeta queries 409 if ( ! empty( $q['meta_key'] ) || ! empty( $q['meta_value'] ) ) 410 $join .= " JOIN $wpdb->mp_mailmeta ON ( $wpdb->mp_mails.id = $wpdb->mp_mailmeta.mp_mail_id ) "; 411 if ( ! empty( $q['meta_key'] ) ) 412 $where .= $wpdb->prepare( " AND $wpdb->mp_mailmeta.meta_key = %s ", $q['meta_key'] ); 413 if ( ! empty( $q['meta_value'] ) ) { 414 if ( empty( $q['meta_compare'] ) || ! in_array( $q['meta_compare'], array( '=', '!=', '>', '>=', '<', '<=' ) ) ) 415 $q['meta_compare'] = '='; 416 417 $where .= $wpdb->prepare( "AND $wpdb->mp_mailmeta.meta_value {$q['meta_compare']} %s ", $q['meta_value'] ); 418 } 419 420 // Paging 421 if ( empty( $q['nopaging'] ) ) 422 { 423 $page = absint( $q['paged'] ); 424 if ( empty( $page ) ) 425 $page = 1; 426 427 if ( empty( $q['offset'] ) ) 428 { 429 $pgstrt = ''; 430 $pgstrt = ( $page - 1 ) * $q['mails_per_page'] . ', '; 431 $limits = 'LIMIT ' . $pgstrt . $q['mails_per_page']; 432 } 433 else 434 { 435 $q['offset'] = absint( $q['offset'] ); 436 $pgstrt = $q['offset'] . ', '; 437 $limits = 'LIMIT ' . $pgstrt . $q['mails_per_page']; 438 } 439 } 440 441 $orderby = $q['orderby']; 442 443 if ( ! empty( $groupby ) ) 444 $groupby = 'GROUP BY ' . $groupby; 445 if ( !empty( $orderby ) ) 446 $orderby = 'ORDER BY ' . $orderby; 447 $found_rows = ''; 448 if ( !$q['no_found_rows'] && !empty( $limits ) ) 449 $found_rows = 'SQL_CALC_FOUND_ROWS'; 450 451 $this->request = " SELECT $found_rows $distinct $fields FROM $wpdb->mp_mails $join WHERE $wpdb->mp_mails.status = 'archived' $where $groupby $orderby $limits"; 452 453 $this->mails = $wpdb->get_results( $this->request ); 454 455 $this->mail_count = count( $this->mails ); 456 457 if ( $this->mail_count > 0 ) $this->mail = $this->mails[0]; 458 459 return $this->mails; 460 } 461 462 function have_mails() 463 { 464 if ( $this->current_mail + 1 < $this->mail_count ) return true; 465 466 if ( $this->current_mail + 1 == $this->mail_count && $this->mail_count > 0 ) 467 $this->rewind_mails(); 468 469 $this->in_the_loop = false; 470 return false; 471 } 472 473 function rewind_mails() 474 { 475 $this->current_mail = -1; 476 if ( $this->mail_count > 0 ) $this->mail = $this->mails[0]; 477 } 478 479 function in_the_loop() 480 { 481 return $this->in_the_loop; 482 } 483 484 function the_mail() 485 { 486 $this->in_the_loop = true; 487 $this->next_mail(); 488 } 489 490 function next_mail() 491 { 492 $this->current_mail++; 493 $this->mail = $this->mails[$this->current_mail]; 494 } 495 496 function the_ID() 497 { 498 echo $this->get_the_ID(); 499 } 500 function get_the_ID() 501 { 502 return $this->mail->id; 503 } 504 505 function the_subject( $before = '', $after = '', $echo = true ) 506 { 507 $subject = $this->get_the_subject(); 508 509 if ( strlen( $subject ) == 0 ) return; 510 511 $subject = $before . $subject . $after; 512 513 if ( $echo ) echo $subject; 514 else return $subject; 515 } 516 function get_the_subject() 517 { 518 $metas = MP_Mail_meta::get( $this->mail->id, '_MailPress_replacements' ); 519 520 $subject_display = $this->mail->subject; 521 if ( $metas ) foreach( $metas as $k => $v ) $subject_display = str_replace( $k, $v, $subject_display ); 522 523 return apply_filters( 'MailPress_the_subject', $subject_display, $this->mail->id ); 524 } 525 526 function the_content( $format = 'html' ) 527 { 528 echo $this->get_the_content( $format ); 529 } 530 function get_the_content( $format = 'html' ) 531 { 532 if ( empty( $this->mail->html ) || ( $format == 'plaintext' ) ) return apply_filters( 'MailPress_the_content', $this->mail->plaintext ); 533 return $this->mail->html; 534 } 535 536 function the_date( $d = '' ) 537 { 538 echo apply_filters( 'the_date', $this->get_the_date( $d, true ), $d ); 539 } 540 function get_the_date( $d = '', $translate = false ) 541 { 542 if ( '' == $d ) $d = get_option( 'date_format' ); 543 return mysql2date( $d, $this->mail->sent, $translate ); 544 } 545 546 function the_time( $d = '' ) 547 { 548 echo apply_filters( 'the_time', $this->get_the_time( $d, true ), $d ); 549 } 550 function get_the_time( $d = '', $translate = false ) 551 { 552 if ( '' == $d ) $d = get_option( 'time_format' ); 553 return mysql2date( $d, $this->mail->sent, $translate ); 554 } 555 556 function the_Theme() 557 { 558 echo $this->get_the_Theme(); 559 } 560 function get_the_Theme() 561 { 562 return $this->mail->theme; 563 } 564 565 function the_Template() 566 { 567 echo $this->get_the_Template(); 568 } 569 function get_the_Template() 570 { 571 return $this->mail->template; 572 } 573 574 function the_permalink() 575 { 576 echo $this->get_the_permalink(); 577 } 578 function get_the_permalink() 579 { 580 return MP_User::get_arch_url( $this->mail->id ); 581 } 582 583 function get_mp_query_var( $var ) 584 { 585 return $this->get( $var ); 586 } 587 588 function set_query_var( $var, $value ) 589 { 590 return $this->set( $var, $value ); 591 } 592 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Tue May 19 15:55:14 2020 | Cross-referenced by PHPXref 0.7.1 |