Skip to content

Commit

Permalink
1.8.70-SE
Browse files Browse the repository at this point in the history
  • Loading branch information
minimus committed Nov 2, 2013
1 parent 0344881 commit 25a7f34
Show file tree
Hide file tree
Showing 44 changed files with 2,645 additions and 1,688 deletions.
166 changes: 12 additions & 154 deletions simple-ads-manager/ad.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,16 @@ class SamAdPlace {
private $useCodes = false;
private $crawler = false;
public $ad = '';
private $clauses;

public function __construct($args = null, $useCodes = false, $crawler = false) {
public function __construct($args = null, $useCodes = false, $crawler = false, $clauses = null) {
global $SAM_Query;

$this->args = $args;
$this->useCodes = $useCodes;
$this->crawler = $crawler;
if(is_null( $clauses )) $this->clauses = $SAM_Query['clauses'];
else $this->clauses = $clauses;
$this->ad = $this->buildAd($this->args, $this->useCodes);
}

Expand All @@ -151,19 +156,6 @@ private function getSize($ss, $width, $height) {
return array('width' => $aSize[0], 'height' => $aSize[1]);
}
}

private function getCustomPostTypes() {
$args = array('public' => true, '_builtin' => false);
$output = 'names';
$operator = 'and';
$post_types = get_post_types($args, $output, $operator);

return $post_types;
}

private function isCustomPostType() {
return (in_array(get_post_type(), $this->getCustomPostTypes()));
}

private function errorWrite($eTable, $rTable, $eSql = null, $eResult = null, $lastError = null) {
global $wpdb;
Expand Down Expand Up @@ -203,156 +195,22 @@ private function errorWrite($eTable, $rTable, $eSql = null, $eResult = null, $la
private function buildAd( $args = null, $useCodes = false ) {
if(is_null($args)) return '';
if(empty($args['id']) && empty($args['name'])) return '';
if( is_null($this->clauses) ) return '';

$settings = $this->getSettings();
if($settings['adCycle'] == 0) $cycle = 1000;
else $cycle = $settings['adCycle'];
$el = (integer)$settings['errorlogFS'];

global $wpdb, $current_user;
global $wpdb/*, $current_user*/;
$pTable = $wpdb->prefix . "sam_places";
$aTable = $wpdb->prefix . "sam_ads";
$eTable = $wpdb->prefix . "sam_errors";

$viewPages = 0;
//$cats = array();
//$wcul = '';
$wcu = '';
$wcc = '';
$wci = '';
$wca = '';
$wcx = '';
$wct = '';
$wcxc = '';
$wcxa = '';
$wcxt = '';

if(is_user_logged_in()) {
get_currentuserinfo();
$uSlug = $current_user->user_login;
$wcul = "IF($aTable.ad_users_reg = 1,
IF($aTable.x_ad_users = 1, NOT FIND_IN_SET(\"$uSlug\", $aTable.x_view_users), TRUE) AND
IF($aTable.ad_users_adv = 1, ($aTable.adv_nick <> \"$uSlug\"), TRUE),
FALSE)";
}
else {
$wcul = "($aTable.ad_users_unreg = 1)";
}
$wcu = "(IF($aTable.ad_users = 0, TRUE, $wcul)) AND";

if(is_home() || is_front_page()) $viewPages += SAM_IS_HOME;
if(is_singular()) {
$viewPages |= SAM_IS_SINGULAR;
if($this->isCustomPostType()) {
$viewPages |= SAM_IS_SINGLE;
$viewPages |= SAM_IS_POST_TYPE;

$postType = get_post_type();
$wct .= " AND IF($aTable.view_type < 2 AND $aTable.ad_custom AND IF($aTable.view_type = 0, $aTable.view_pages+0 & $viewPages, TRUE), FIND_IN_SET(\"$postType\", $aTable.view_custom), TRUE)";
$wcxt .= " AND IF($aTable.view_type < 2 AND $aTable.x_custom AND IF($aTable.view_type = 0, $aTable.view_pages+0 & $viewPages, TRUE), NOT FIND_IN_SET(\"$postType\", $aTable.x_view_custom), TRUE)";
}
if(is_single()) {
global $post;

$viewPages |= SAM_IS_SINGLE;
$categories = get_the_category($post->ID);
$tags = get_the_tags();
$postID = ((!empty($post->ID)) ? $post->ID : 0);

if(!empty($categories)) {
$wcc_0 = '';
$wcxc_0 = '';
$wcc = " AND IF($aTable.view_type < 2 AND $aTable.ad_cats AND IF($aTable.view_type = 0, $aTable.view_pages+0 & $viewPages, TRUE),";
$wcxc = " AND IF($aTable.view_type < 2 AND $aTable.x_cats AND IF($aTable.view_type = 0, $aTable.view_pages+0 & $viewPages, TRUE),";
foreach($categories as $category) {
if(empty($wcc_0)) $wcc_0 = " FIND_IN_SET(\"{$category->category_nicename}\", $aTable.view_cats)";
else $wcc_0 .= " OR FIND_IN_SET(\"{$category->category_nicename}\", $aTable.view_cats)";
if(empty($wcxc_0)) $wcxc_0 = " (NOT FIND_IN_SET(\"{$category->category_nicename}\", $aTable.x_view_cats))";
else $wcxc_0 .= " AND (NOT FIND_IN_SET(\"{$category->category_nicename}\", $aTable.x_view_cats))";
}
$wcc .= $wcc_0.", TRUE)";
$wcxc .= $wcxc_0.", TRUE)";
}

if(!empty($tags)) {
$wct_0 = '';
$wcxt_0 = '';
$wct .= " AND IF($aTable.view_type < 2 AND $aTable.ad_tags AND IF($aTable.view_type = 0, $aTable.view_pages+0 & $viewPages, TRUE),";
$wcxt .= " AND IF($aTable.view_type < 2 AND $aTable.x_tags AND IF($aTable.view_type = 0, $aTable.view_pages+0 & $viewPages, TRUE),";
foreach($tags as $tag) {
if(empty($wct_0)) $wct_0 = " FIND_IN_SET(\"{$tag->slug}\", $aTable.view_tags)";
else $wct_0 .= " OR FIND_IN_SET(\"{$tag->slug}\", $aTable.view_tags)";
if(empty($wcxt_0)) $wcxt_0 = " (NOT FIND_IN_SET(\"{$tag->slug}\", $aTable.x_view_tags))";
else $wcxt_0 .= " AND (NOT FIND_IN_SET(\"{$tag->slug}\", $aTable.x_view_tags))";
}
$wct .= $wct_0.", TRUE)";
$wcxt .= $wcxt_0.", TRUE)";
}

$wci = " OR ($aTable.view_type = 2 AND FIND_IN_SET({$postID}, $aTable.view_id))";
$wcx = " AND IF($aTable.x_id, NOT FIND_IN_SET({$postID}, $aTable.x_view_id), TRUE)";
$author = get_userdata($post->post_author);
$wca = " AND IF($aTable.view_type < 2 AND $aTable.ad_authors AND IF($aTable.view_type = 0, $aTable.view_pages+0 & $viewPages, TRUE), FIND_IN_SET(\"{$author->display_name}\", $aTable.view_authors), TRUE)";
$wcxa = " AND IF($aTable.view_type < 2 AND $aTable.x_authors AND IF($aTable.view_type = 0, $aTable.view_pages+0 & $viewPages, TRUE), NOT FIND_IN_SET(\"{$author->display_name}\", $aTable.x_view_authors), TRUE)";
}
if(is_page()) {
global $post;
$postID = ((!empty($post->ID)) ? $post->ID : 0);

$viewPages |= SAM_IS_PAGE;
$wci = " OR ($aTable.view_type = 2 AND FIND_IN_SET({$postID}, $aTable.view_id))";
$wcx = " AND IF($aTable.x_id, NOT FIND_IN_SET({$postID}, $aTable.x_view_id), TRUE)";
}
if(is_attachment()) $viewPages |= SAM_IS_ATTACHMENT;
}
if(is_search()) $viewPages |= SAM_IS_SEARCH;
if(is_404()) $viewPages |= SAM_IS_404;
if(is_archive()) {
$viewPages |= SAM_IS_ARCHIVE;
if(is_tax()) $viewPages |= SAM_IS_TAX;
if(is_category()) {
$viewPages |= SAM_IS_CATEGORY;
$cat = get_category(get_query_var('cat'), false);
$wcc = " AND IF($aTable.view_type < 2 AND $aTable.ad_cats AND IF($aTable.view_type = 0, $aTable.view_pages+0 & $viewPages, TRUE), FIND_IN_SET(\"{$cat->category_nicename}\", $aTable.view_cats), TRUE)";
$wcxc = " AND IF($aTable.view_type < 2 AND $aTable.x_cats AND IF($aTable.view_type = 0, $aTable.view_pages+0 & $viewPages, TRUE), NOT FIND_IN_SET(\"{$cat->category_nicename}\", $aTable.x_view_cats), TRUE)";
}
if(is_tag()) {
$viewPages |= SAM_IS_TAG;
$tag = get_tag(get_query_var('tag_id'));
$wct = " AND IF($aTable.view_type < 2 AND $aTable.ad_tags AND IF($aTable.view_type = 0, $aTable.view_pages+0 & $viewPages, TRUE), FIND_IN_SET('{$tag->slug}', $aTable.view_tags), TRUE)";
$wcxt = " AND IF($aTable.view_type < 2 AND $aTable.x_tags AND IF($aTable.view_type = 0, $aTable.view_pages+0 & $viewPages, TRUE), NOT FIND_IN_SET('{$tag->slug}', $aTable.x_view_tags), TRUE)";
}
if(is_author()) {
global $wp_query;

$viewPages |= SAM_IS_AUTHOR;
$author = $wp_query->get_queried_object();
$wca = " AND IF($aTable.view_type < 2 AND $aTable.ad_authors = 1 AND IF($aTable.view_type = 0, $aTable.view_pages+0 & $viewPages, TRUE), FIND_IN_SET('{$author->display_name}', $aTable.view_authors), TRUE)";
$wcxa = " AND IF($aTable.view_type < 2 AND $aTable.x_authors AND IF($aTable.view_type = 0, $aTable.view_pages+0 & $viewPages, TRUE), NOT FIND_IN_SET('{$author->display_name}', $aTable.x_view_authors), TRUE)";
}
if(is_post_type_archive()) {
$viewPages |= SAM_IS_POST_TYPE_ARCHIVE;
//$postType = post_type_archive_title( '', false );
$postType = get_post_type();
$wct = " AND IF($aTable.view_type < 2 AND $aTable.ad_custom AND IF($aTable.view_type = 0, $aTable.view_pages+0 & $viewPages, TRUE), FIND_IN_SET('{$postType}', $aTable.view_custom), TRUE)";
$wcxt = " AND IF($aTable.view_type < 2 AND $aTable.x_custom AND IF($aTable.view_type = 0, $aTable.view_pages+0 & $viewPages, TRUE), NOT FIND_IN_SET('{$postType}', $aTable.x_view_custom), TRUE)";
}
if(is_date()) $viewPages |= SAM_IS_DATE;
}

if(empty($wcc)) $wcc = " AND ($aTable.ad_cats = 0)";
if(empty($wca)) $wca = " AND ($aTable.ad_authors = 0)";

$whereClause = "$wcu (($aTable.view_type = 1)";
$whereClause .= " OR ($aTable.view_type = 0 AND ($aTable.view_pages+0 & $viewPages))";
$whereClause .= "$wci)";
$whereClause .= "$wcc $wca $wct $wcx $wcxc $wcxa $wcxt";
$whereClauseT = " AND IF($aTable.ad_schedule, CURDATE() BETWEEN $aTable.ad_start_date AND $aTable.ad_end_date, TRUE)";
$whereClauseT .= " AND IF($aTable.limit_hits, $aTable.hits_limit > $aTable.ad_hits, TRUE)";
$whereClauseT .= " AND IF($aTable.limit_clicks, $aTable.clicks_limit > $aTable.ad_clicks, TRUE)";

$whereClauseW = " AND IF($aTable.ad_weight > 0, ($aTable.ad_weight_hits*10/($aTable.ad_weight*$cycle)) < 1, FALSE)";
$whereClause2W = "AND ($aTable.ad_weight > 0)";
$whereClause = $this->clauses['WC'];
$whereClauseT = $this->clauses['WCT'];
$whereClauseW = $this->clauses['WCW'];
$whereClause2W = $this->clauses['WC2W'];

if(!empty($args['id'])) $pId = "$pTable.id = {$args['id']}";
else $pId = "$pTable.name = '{$args['name']}'";
Expand Down
Loading

1 comment on commit 25a7f34

@minimus
Copy link
Owner Author

@minimus minimus commented on 25a7f34 Nov 2, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Creation of SQL queries is optimized.
Third party scripts are renewed.
Some bugs are resolved.

Please sign in to comment.