Skip to content

Commit

Permalink
Some changes for 2.4.89
Browse files Browse the repository at this point in the history
The quantity of SQL of requests for each ad was reduced.
Indices for plugin's database tables were added.
The bug of data loading into the grid is fixed.
  • Loading branch information
minimus committed Sep 24, 2014
1 parent b6bcbbf commit d503c41
Show file tree
Hide file tree
Showing 20 changed files with 16,459 additions and 10,819 deletions.
14 changes: 10 additions & 4 deletions .idea/simple-ads-manager.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 1 addition & 20 deletions .idea/webResources.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

203 changes: 201 additions & 2 deletions ad.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ class SamAdPlace {
public $cid = null;
private $clauses;
private $force;
public $sql = '';

public function __construct($args = null, $useCodes = false, $crawler = false, $clauses = null, $ajax = false) {
global $SAM_Query;
Expand All @@ -159,7 +160,7 @@ public function __construct($args = null, $useCodes = false, $crawler = false, $
else $this->clauses = $clauses;
$this->force = $ajax;

$this->ad = $this->buildAd($this->args, $this->useCodes);
$this->ad = $this->buildAd2($this->args, $this->useCodes);
}

private function getSettings() {
Expand Down Expand Up @@ -417,7 +418,7 @@ private function buildAd( $args = null, $useCodes = false ) {
$output = ob_get_contents();
ob_end_clean();
}
else $output = self::prepareCodes($ad['ad_code'], $rId);
else self::prepareCodes($ad['ad_code'], $rId);
}
//if(!$this->crawler && !is_admin())
//$wpdb->query("UPDATE $aTable SET $aTable.ad_hits = $aTable.ad_hits+1, $aTable.ad_weight_hits = $aTable.ad_weight_hits+1 WHERE $aTable.id = {$ad['id']}");
Expand All @@ -433,6 +434,204 @@ private function buildAd( $args = null, $useCodes = false ) {
elseif($useCodes) $output = $place['code_before'].$output.$place['code_after'];
return $output;
}

private function buildAd2( $args = null, $useCodes = false ) {
if(is_null($args)) return '';
if(empty($args['id']) && empty($args['name'])) return '';
if( is_null($this->clauses) ) return '';

$settings = self::getSettings();
$data = intval($useCodes);
$rId = rand(1111, 9999);
if($settings['adCycle'] == 0) $cycle = 1000;
else $cycle = $settings['adCycle'];
$el = isset($settings['errorlogFS']);

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

$whereClause = $this->clauses['WC'];
$whereClauseT = $this->clauses['WCT'];
$whereClauseW = $this->clauses['WCW'];
$whereClause2W = $this->clauses['WC2W'];

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

$output = "";

$aSql = "
SELECT
@pid := sp.id AS pid,
0 AS aid,
sp.name,
sp.patch_source AS code_mode,
@code_before := sp.code_before AS code_before,
@code_after := sp.code_after AS code_after,
@ad_size := IF(sp.place_size = \"custom\", CONCAT(CAST(sp.place_custom_width AS CHAR), \"x\", CAST(sp.place_custom_height AS CHAR)), sp.place_size) AS ad_size,
sp.patch_code AS ad_code,
sp.patch_img AS ad_img,
\"\" AS ad_alt,
0 AS ad_no,
sp.patch_link AS ad_target,
0 AS ad_swf,
\"\" AS ad_swf_flashvars,
\"\" AS ad_swf_params,
\"\" AS ad_swf_attributes,
sp.patch_adserver AS ad_adserver,
sp.patch_dfp AS ad_dfp,
0 AS count_clicks,
0 AS code_type,
IF((sp.patch_source = 1 AND sp.patch_adserver) OR sp.patch_source = 2, -1, 1) AS ad_cycle,
@aca := IFNULL((SELECT AVG(wsa.ad_weight_hits*10/(wsa.ad_weight*$cycle)) FROM $aTable wsa WHERE wsa.pid = @pid AND wsa.trash IS NOT TRUE), 0) AS aca
FROM $pTable sp
WHERE $pId AND sp.trash IS FALSE
UNION
SELECT
sa.pid,
sa.id AS aid,
sa.name,
sa.code_mode,
@code_before AS code_before,
@code_after AS code_after,
@ad_size AS ad_size,
sa.ad_code,
sa.ad_img,
sa.ad_alt,
sa.ad_no,
sa.ad_target,
sa.ad_swf,
sa.ad_swf_flashvars,
sa.ad_swf_params,
sa.ad_swf_attributes,
0 AS ad_adserver,
0 AS ad_dfp,
sa.count_clicks,
sa.code_type,
IF(sa.ad_weight, (sa.ad_weight_hits*10/(sa.ad_weight*$cycle)), 0) AS ad_cycle,
@aca AS aca
FROM $aTable sa
WHERE sa.pid = @pid AND sa.trash IS FALSE AND $whereClause $whereClauseT $whereClauseW
ORDER BY ad_cycle
LIMIT 1;";

$ad = $wpdb->get_row($aSql, ARRAY_A);

if($ad === false) {
if($el) self::errorWrite($eTable, $aTable, $aSql, $ad, $wpdb->last_error);
return '';
}

if((integer)$ad['aca'] == 1) {
$wpdb->update($aTable, array('ad_weight_hits' => 0), array('pid' => $ad['pid']), array("%d"), array("%d"));
$ad = $wpdb->get_row($aSql, ARRAY_A);
}

$this->pid = $ad['pid'];
$this->id = $ad['aid'];
$this->cid = "c{$rId}_{$this->id}_{$this->pid}";
//$this->sql = $aSql;

// DFP
if($ad['code_mode'] == 2) {
if(($settings['useDFP'] == 1) && !empty($settings['dfpPub'])) {
$output = "<!-- {$ad['ad_dfp']} -->"."\n";
$output .= "<script type='text/javascript'>"."\n";
$output .= " GA_googleFillSlot('{$ad['ad_dfp']}');"."\n";
$output .= "</script>"."\n";
if($useCodes)
$output = (is_array($useCodes)) ? $useCodes['before'].$output.$useCodes['after'] : $ad['code_before'].$output.$ad['code_after'];

$output = "<div id='c{$rId}_{$ad['aid']}_{$ad['pid']}' class='sam-container sam-ad'>{$output}</div>";
}
else $output = '';

return $output;
}

// Ad Server (Blocking output of contained ads)
if(($ad['code_mode'] == 1) && (abs($ad['ad_adserver']) == 1)) {
$output = self::prepareCodes($ad['ad_code'], $rId);
if($useCodes)
$output = (is_array($useCodes)) ? $useCodes['before'].$output.$useCodes['after'] : $ad['code_before'].$output.$ad['code_after'];
$output = "<div id='c{$rId}_{$ad['aid']}_{$ad['pid']}' class='sam-container sam-ad'>{$output}</div>";

return $output;
}

// JS Loading
if(isset($settings['adShow']) && ($settings['adShow'] == 'js') && !$this->force)
return "<div id='c{$rId}_0_{$ad['pid']}' class='sam-container sam-place' data-sam='{$data}'></div>";

// Image and Code Modes
if($ad['code_mode'] == 0) {
if((int)$ad['ad_swf']) {
$id = "ad-".$ad['aid'].'-'.$rId;
$file = $ad['ad_img'];
$sizes = self::getSize($ad['ad_size'], null, null);
$width = $sizes['width'];
$height = $sizes['height'];
$flashvars = (!empty($ad['ad_swf_flashvars'])) ? $ad['ad_swf_flashvars'] : '{}';
$params = (!empty($ad['ad_swf_params'])) ? $ad['ad_swf_params'] : '{}';
$attributes = (!empty($ad['ad_swf_attributes'])) ? $ad['ad_swf_attributes'] : '{}';
$text = 'Flash ad ID:'.$ad['aid']; //__('Flash ad').' ID:'.$ad['aid'];
$output = "
<script type='text/javascript'>
var
flashvars = $flashvars,
params = $params,
attributes = $attributes;
attributes.id = '$id';
attributes.styleclass = 'sam_ad';
swfobject.embedSWF('$file', '$id', '$width', '$height', '9.0.0', '', flashvars, params, attributes);
</script>
<div id='$id'>$text</div>
";
}
else {
$outId = ((int) $ad['count_clicks'] == 1) ? " id='a".rand(10, 99)."_".$ad['aid']."' class='sam_ad'" : '';
$aStart ='';
$aEnd ='';
$iTag = '';
if(!empty($settings['adDisplay'])) $target = '_'.$settings['adDisplay'];
else $target = '_blank';
if(!empty($ad['ad_target'])) {
//$aStart = ((in_array((integer)$ad['ad_no'], array(2,3))) ? '<noindex>' : '')."<a href='{$ad['ad_target']}' target='$target' ".((in_array((integer)$ad['ad_no'], array(1,3))) ? " rel='nofollow'" : '').">";
//$aEnd = "</a>".(in_array((integer)$ad['ad_no'], array(2,3))) ? '</noindex>' : '';
$aStart = "<a $outId href='{$ad['ad_target']}' target='$target' ".">";
$aEnd = "</a>";
}
if(!empty($ad['ad_img'])) $iTag = "<img src='{$ad['ad_img']}' ".((!empty($ad['ad_alt'])) ? " alt='{$ad['ad_alt']}' " : " alt='' ")." />";
$output = $aStart.$iTag.$aEnd;
}
}
elseif($ad['code_mode'] == 1) {
if($ad['code_type'] == 1) {
ob_start();
eval('?>'.$ad['ad_code'].'<?');
$output = ob_get_contents();
ob_end_clean();
}
else $output = self::prepareCodes($ad['ad_code'], $rId);
}

//$this->sql = $output;

$output = "<div id='c{$rId}_{$ad['aid']}_{$ad['pid']}' class='sam-container sam-place' data-sam='{$data}'>{$output}</div>";

if(is_array($useCodes)) $output = $useCodes['before'].$output.$useCodes['after'];
elseif($useCodes) $output = $ad['code_before'].$output.$ad['code_after'];

// Updating Display Cycle
if(!$this->crawler && !is_admin()) {
$sSql = "UPDATE $aTable sa SET sa.ad_weight_hits = sa.ad_weight_hits + 1 WHERE sa.id = %d;";
$wpdb->query($wpdb->prepare($sSql, $ad['aid']));
}

return $output;
}
}
}

Expand Down
58 changes: 49 additions & 9 deletions admin.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ class SimpleAdsManagerAdmin extends SimpleAdsManager {
public function __construct() {
parent::__construct();

global $wp_version, $sam_tables_defs;
global $wp_version, $sam_tables_defs, $wpdb;

if ( function_exists( 'load_plugin_textdomain' ) )
load_plugin_textdomain( SAM_DOMAIN, false, basename( SAM_PATH ) . '/langs/' );

if(!is_dir(SAM_AD_IMG)) mkdir(SAM_AD_IMG);

$this->settingsTabs = array();
$sam_tables_defs = self::getTablesDefs();
$sam_tables_defs = self::getTablesDefs($wpdb->prefix);

register_activation_hook(SAM_MAIN_FILE, array(&$this, 'onActivate'));
register_deactivation_hook(SAM_MAIN_FILE, array(&$this, 'onDeactivate'));
Expand Down Expand Up @@ -247,7 +247,7 @@ private function updateDB() {
$this->getVersions(true);
}

public function getTablesDefs() {
public function getTablesDefs( $prefix = 'wp_' ) {
$pTableDef = array(
'id' => array('Type' => "int(11)", 'Null' => 'NO', 'Key' => 'PRI', 'Default' => '', 'Extra' => 'auto_increment'),
'name' => array('Type' => "varchar(255)", 'Null' => 'NO', 'Key' => '', 'Default' => '', 'Extra' => ''),
Expand Down Expand Up @@ -389,12 +389,50 @@ public function getTablesDefs() {
'event_type' => array('Type' => 'tinyint(1)', 'Null' => 'YES', 'Key' => '', 'Default' => '', 'Extra' => '')
);

$pIndexDef = array(
'UK_'.$prefix.'places' => array(
'id' => array('Non_unique' => 0, 'Seq_in_index' => 1, 'Column_name' => 'id')
)
);

$aIndexDef = array(
'UK_'.$prefix.'ads' => array(
'pid' => array('Non_unique' => 0, 'Seq_in_index' => 1, 'Column_name' => 'pid'),
'id' => array('Non_unique' => 0, 'Seq_in_index' => 2, 'Column_name' => 'id')
)
);

$zIndexDef = array(
'UK_'.$prefix.'zones' => array(
'id' => array('Non_unique' => 0, 'Seq_in_index' => 1, 'Column_name' => 'id')
)
);

$bIndexDef = array(
'UK_'.$prefix.'blocks' => array(
'id' => array('Non_unique' => 0, 'Seq_in_index' => 1, 'Column_name' => 'id')
)
);

$sIndexDef = array(
'IDX_'.$prefix.'stats' => array(
'id' => array('Non_unique' => 1, 'Seq_in_index' => 1, 'Column_name' => 'id'),
'pid' => array('Non_unique' => 1, 'Seq_in_index' => 2, 'Column_name' => 'pid'),
'event_time' => array('Non_unique' => 1, 'Seq_in_index' => 3, 'Column_name' => 'event_time')
)
);

return array(
'places' => $pTableDef,
'ads' => $aTableDef,
'zones' => $zTableDef,
'blocks' => $bTableDef,
'stats' => $sTableDef
'stats' => $sTableDef,
'idxPlaces' => $pIndexDef,
'idxAds' => $aIndexDef,
'idxZones' => $zIndexDef,
'idxBlocks' => $bIndexDef,
'idxStats' => $sIndexDef
);
}

Expand Down Expand Up @@ -803,7 +841,7 @@ public function loadScripts($hook) {
wp_enqueue_style('ComboGrid', SAM_URL.'css/jquery.ui.combogrid.css', false, '1.6.3');
wp_enqueue_style('wp-pointer');
wp_enqueue_style('colorButtons', SAM_URL.'css/color-buttons.css', false, SAM_VERSION);
wp_enqueue_style('W2UI', SAM_URL . 'css/w2ui.min.css', false, '1.3');
wp_enqueue_style('W2UI', SAM_URL . 'css/w2ui.min.css', false, '1.4.1');
wp_enqueue_style('jqPlot', SAM_URL . 'css/jquery.jqplot.min.css', false, '1.0.2');

$options = parent::getSettings();
Expand All @@ -820,7 +858,7 @@ public function loadScripts($hook) {
if($options['useSWF']) wp_enqueue_script('swfobject');
wp_enqueue_script('jquery');
wp_enqueue_media(array('post' => null));
wp_enqueue_script('W2UI', SAM_URL . 'js/w2ui.min.js', array('jquery'), '1.3');
wp_enqueue_script('W2UI', SAM_URL . 'js/w2ui.min.js', array('jquery'), '1.4.1');
wp_enqueue_script('jquery-ui-core');
wp_enqueue_script('jquery-effects-core');
//wp_enqueue_script('jquery-ui-mouse');
Expand All @@ -846,7 +884,7 @@ public function loadScripts($hook) {
wp_enqueue_script('pointLabels', SAM_URL . 'js/jqplot.pointLabels.min.js', array('jquery', 'jqPlot'), '1.0.2');

wp_enqueue_script('wp-pointer');
wp_enqueue_script('adminEditScript', SAM_URL.'js/sam-admin-edit-item.min.js', array('jquery', 'jquery-ui-core', 'jquery-ui-widget', 'jquery-ui-position'), SAM_VERSION);
wp_enqueue_script('adminEditScript', SAM_URL.'js/sam-admin-edit-item.js', array('jquery', 'jquery-ui-core', 'jquery-ui-widget', 'jquery-ui-position'), SAM_VERSION);
wp_localize_script('adminEditScript', 'samEditorOptions', array(
'places' => array('enabled' => $pointers['places'], 'title' => __('Name of Ads Place', SAM_DOMAIN), 'content' => __('This is not required parameter. But it is strongly recommended to define it if you plan to use Ads Blocks, plugin\'s widgets or autoinserting of ads.', SAM_DOMAIN)),
'ads' => array('enabled' => $pointers['ads'], 'title' => __('Name of Ad', SAM_DOMAIN), 'content' => __('This is not required parameter. But it is strongly recommended to define it if you plan to use Ads Blocks or plugin\'s widgets.', SAM_DOMAIN)),
Expand Down Expand Up @@ -1479,8 +1517,10 @@ function affiliateLink(str){ str = unescape(str); var r = ''; for(var i = 0; i <
</div>
<div id="post-body">
<div id="post-body-content">
<?php settings_fields('samOptions'); ?>
<?php $this->doSettingsSections('sam-settings', $this->settingsTabs); ?>
<?php
settings_fields('samOptions');
$this->doSettingsSections('sam-settings', $this->settingsTabs);
?>
<p class="submit">
<button id="submit-button" class="color-btn color-btn-left" name="Submit" type="submit">
<b style="background-color: #21759b"></b>
Expand Down
Loading

0 comments on commit d503c41

Please sign in to comment.