$GLOBALS['MH_RESULTS_PER_PAGE'] = 7;
$GLOBALS['MH_RECOMMENDED_PER_PAGE'] = 7;
function mh_G($name)
{
return $GLOBALS['MH_' . $name];
}
function mh_cat_desc($desc)
{
$pair = explode('<!--more-->', $desc);
if (is_category(mh_get_credit_types_id())) {
return count($pair) > 1 ? implode('<br/>', array_slice($pair, 1)) : $desc;
}
return $pair[0];
}
add_filter('category_description', 'mh_cat_desc');
function mh_get_credit_types($include=null)
{
$cats = array();
if (!is_array($include) || in_array( 1, $include)) $cats[] = get_category( 1);
if (!is_array($include) || in_array(24, $include)) $cats[] = get_category(24);
if (!is_array($include) || in_array(21, $include)) $cats[] = get_category(21);
if (!is_array($include) || in_array(635, $include)) $cats[] = get_category(635);
if (!is_array($include) || in_array(642, $include)) $cats[] = get_category(642);
if (!is_array($include) || in_array(646, $include)) $cats[] = get_category(646);
usort($cats, function($a, $b) {
if (!array_key_exists('term_order', $a) || !array_key_exists('term_order', $b))
{
return 0;
}
elseif ($a->term_order < $b->term_order)
{
return -1;
}
elseif ($a->term_order > $b->term_order) {
return 1;
}
return 0;
});
return $cats;
/*return get_categories(array(
'parent' => 18,
'hide_empty' => 0
));*/
}
add_action('pre_get_posts', 'my_pre_get_posts');
function my_pre_get_posts( $query )
{
if( is_admin() )
{
return $query;
}
if(($query->query_vars['cat']==1) || ($query->query_vars['cat']==24) || ($query->query_vars['cat']==642) || ($query->query_vars['cat']==21))
{
$query->set('orderby', 'meta_value_num');
$query->set('meta_key', 'offer_order');
$query->set('order', 'DESC');
}
//var_dump($query);
// always return
return $query;
}
function mh_get_credit_types_id()
{
$ids = array();
foreach (mh_get_credit_types() as $type)
{
$ids[] = $type->term_id;
}
return $ids;
}
function mh_get_card_props($boolprops=false, $before="", $after="")
{
$props = array();
$fields = get_fields();
if (!$boolprops)
{
$interval = mh_get_interval('credit_limit', $fields);
if (is_array($interval)) $props[] = sprintf('Кредитный лимит %s%s р.', $interval[0], $interval[1]);
$interval = mh_get_interval('credit_proc', $fields, '%');
if (is_array($interval)) $props[] = sprintf('Процентная ставка %s%s годовых', $interval[0], $interval[1]);
$interval = mh_get_interval('credit_date', $fields);
if (is_array($interval)) $props[] = sprintf('Срок кредитования %s%s мес.', $interval[0], $interval[1]);
}
if (mh_is_checked('credit_without_verification', $fields)) $props[] = 'Без справок о доходах';
if (mh_is_checked('credit_pasport', $fields)) $props[] = 'Нужен только паспорт';
if (mh_is_checked('credit_bad_history', $fields)) $props[] = 'С плохой кредитной историей';
if (mh_is_checked('credit_without_bail', $fields)) $props[] = 'Без поручителей';
foreach($props as $i => $prop)
{
$props[$i] = $before.$prop.$after;
}
return array_merge($props, mh_get_cretit_extra_options());
}
function mh_get_interval($f, $fields, $t='', $values_only=false)
{
$from = array_key_exists($f.'_from', $fields) ? $fields[$f.'_from'] : 0;
$to = array_key_exists($f.'_to', $fields) ? $fields[$f.'_to'] : 0;
if ($to == 9999) {
$to = 0;
}
if ($from || $to)
{
return $values_only
? array($from ? $from : 0, $to ? $to : 0)
: array($from ? "от $from".$t : '- ' , $to ? " до $to".$t : '');
}
return false;
}
function mh_WP_Query($args, $init_counter=true)
{
/* Будем считать выводимые посты по порядку. */
// Инициализация счетчика
$GLOBALS['mh_the_counter'] = $init_counter ? (mh_get_page_num() - 1) * mh_G('RESULTS_PER_PAGE') : 0;
add_filter('the_posts', function ($posts) {
$GLOBALS['mh_posts_count'] = count($posts);
return $posts;
});
// Считаем выводимые посты по порядку.
add_filter('the_post', function() {
$GLOBALS['mh_the_counter']++;
});
return new WP_Query($args);
}
/* Получаем поля поста сразу. */
add_filter('the_post', function() {
$GLOBALS['mh_the_credit_fields'] = get_fields();
});
function mh_get_posts_count()
{
global $posts;
return count($posts) ? count($posts) : $GLOBALS['mh_posts_count'];
}
function mh_is_even_post()
{
return ($GLOBALS['mh_the_counter'] % 2 != 1);
}
function mh_is_the_last()
{
return $GLOBALS['mh_the_counter'] == $GLOBALS['mh_posts_count'];
}
function mh_is_first_page()
{
return mh_get_page_num() == 1;
}
function mh_is_last_page()
{
global $mh_query;
$paged = get_query_var('p') ? get_query_var('p') : 1;
return $mh_query->max_num_pages == $paged;
}
function mh_get_page_num()
{
return isset($_GET['paged']) ? $_GET['paged'] : 1;
}
function mh_the_page_num()
{
echo mh_get_page_num();
}
function mh_get_credit_limit()
{
$interval = mh_get_interval('credit_limit', $GLOBALS['mh_the_credit_fields'], '', true);
$ret = '';
if ($interval[0]) $ret .= 'от ' . mh_fmt_sum($interval[0]);
if ($interval[1]) $ret .= ' до ' . mh_fmt_sum($interval[1]);
return trim($ret) . ' р.';
}
function mh_fmt_sum($sum)
{
return number_format($sum, 0, ',', ' ');
}
function mh_get_credit_proc()
{
$interval = mh_get_interval('credit_proc', $GLOBALS['mh_the_credit_fields'], '', true);
$ret = '';
if ($interval[0]) $ret .= 'от ' . $interval[0] . '%';
if ($interval[1]) $ret .= ' до ' . $interval[1] . '%';
$mes = $GLOBALS['mh_the_credit_fields']['credit_proc_unit'];
if (!empty($mes)) $ret .= ' ' . $mes;
return trim($ret);
}
function mh_get_credit_period()
{
$interval = mh_get_interval('credit_date', $GLOBALS['mh_the_credit_fields'], '%', true);
$ret = '';
if ($interval[0]) $ret .= 'от ' . $interval[0];
if ($interval[1]) $ret .= ' до ' . $interval[1];
$mes = $GLOBALS['mh_the_credit_fields']['credit_period_unit'];
if (!empty($mes)) $ret .= ' ' . $mes;
return trim($ret) . ' ';
}
function mh_is_credit_wo_certificate()
{
return mh_is_checked('credit_without_verification', $GLOBALS['mh_the_credit_fields']);
}
function mh_is_credit_pasport()
{
return mh_is_checked('credit_pasport', $GLOBALS['mh_the_credit_fields']);
}
function mh_is_credit_w_bad_history()
{
return mh_is_checked('credit_bad_history', $GLOBALS['mh_the_credit_fields']);
}
function mh_is_credit_wo_surety()
{
return mh_is_checked('credit_without_bail', $GLOBALS['mh_the_credit_fields']);
}
function mh_get_cretit_extra_options()
{
if (array_key_exists('credit_extra_options', $GLOBALS['mh_the_credit_fields'])
&& !empty($GLOBALS['mh_the_credit_fields']['credit_extra_options']))
{
return explode("\n", $GLOBALS['mh_the_credit_fields']['credit_extra_options']);
}
return array();
}
/* Теги */
function mh_the_credit_limit() { echo mh_get_credit_limit(); }
function mh_the_credit_proc() { echo mh_get_credit_proc(); }
function mh_the_credit_period() { echo mh_get_credit_period(); }
function mh_the_credit_wo_certificate()
{
if (mh_is_credit_wo_certificate())
echo 'Без справок о доходах';
}
function mh_the_credit_pasport()
{
if (mh_is_credit_pasport())
echo 'Нужен только паспорт';
}
function mh_the_credit_w_bad_history()
{
if (mh_is_credit_w_bad_history())
echo 'С плохой кредитной историей';
}
function mh_the_credit_wo_surety()
{
if (mh_is_credit_wo_surety())
echo 'Без поручителей';
}
function mh_the_offer_link()
{
global $mh_the_credit_fields;
if (array_key_exists('offer_link', $mh_the_credit_fields))
echo $mh_the_credit_fields['offer_link'];
}
function mh_get_bool_credit_props()
{
$props = array();
if ( mh_is_credit_wo_certificate() ) $props[] = 'Без справок о доходах';
if ( mh_is_credit_pasport() ) $props[] = 'Нужен только паспорт';
if ( mh_is_credit_w_bad_history() ) $props[] = 'С плохой кредитной историей';
if ( mh_is_credit_wo_surety() ) $props[] = 'Без поручителей';
return array_merge($props, mh_get_cretit_extra_options());
}
function mh_is_checked($f, $fields)
{
return array_key_exists($f, $fields) && $fields[$f];
}
function mh_add_cond_interval(&$meta_query, $field, $value)
{
$meta_query[] = array(
'key' => $field.'_from',
'value' => $value,
'compare' => '<=',
'type' => 'NUMERIC'
);
$meta_query[] = array(
'key' => $field.'_to',
'value' => $value,
'compare' => '>=',
'type' => 'NUMERIC'
);
}
function mh_add_cond_eq_true(&$meta_query, $field)
{
$meta_query[] = array(
'key' => $field,
'value' => true,
'type' => 'BOOLEAN'
);
}
function mh_widgets_init()
{
register_sidebar();
}
add_action( 'widgets_init', 'mh_widgets_init' );
remove_filter('the_content', 'wpautop');
remove_filter('the_content', 'wptexturize');
function mh_get_list_offers_args($query)
{
// [LIST-OFFERS &cat=1,2,3 &count=1 &random=1 &post=1]
$args = array();
$pairs = preg_split('/&(?:(?:amp|#038);){0,1}/', $query);
foreach($pairs as $pair)
{
$p = explode('=', $pair);
if ($p[0])
{
$args[trim($p[0])] = trim($p[1]);
}
}
return $args;
}
function mh_show_credit_info($args)
{
ob_start();
if (array_key_exists('type', $args) && $args['type'] == 2)
{
get_template_part( 'credit', 'info2' );
}
else
{
get_template_part( 'credit', 'info' );
}
$content = ob_get_contents();
ob_end_clean();
return $content;
}
function mh_get_inc_types()
{
return array_key_exists('mh_include_types', $GLOBALS) ? $GLOBALS['mh_include_types'] : null;
}
function mh_show_credit_types($args)
{
ob_start();
$GLOBALS['mh_include_types'] = array_key_exists('types', $args) ? explode(',', $args['types']) : null;
?><div><?
get_template_part( 'credit', 'types' );
?></div><?
{
?> <h4 class="main_h4"><?the_title()?></h4> <?
}
return $content;
}