I've a following code snippet from smarty template:
<div class="pagination">
{pagination_link_01 values=$pagination_links}
</div>
The following is the function body:
<?php
function smarty_function_pagination_link_01($params, &$smarty)
{
if ( !is_array($params['values']) )
{
return "is not array";
}
if ( 0 == count($params['values']) )
{
return "Empty Array";
}
if ( empty($params['values']['current_page']) )
{
return "Invalid Request";
}
$values = $params['values'];
//Seperator Used Betwinn Pagination Links
$seprator = empty( $params['seperator'] ) ? " " : $params['seperator'];
//Class Name For Links
$extra = empty( $params['extra'] ) ? "" : $params['extra'];
$current_page = (int)$values['current_page'];
if ( !empty($values['first']) )
{
//$ret[] = "<a $extra href='{$values['first']}' ><First</a>";
}
if ( !empty($values['previous'] ) )
{
$ret[] = "<a $extra href='{$values['previous']}' class='prev active'><span></span></a>";
}
$ret[] = "<ul>";
foreach( $values as $k => $v )
{
if( is_numeric( $k ) )
{
if ( $k == $current_page)
{
$ret[] = "<li><a $extra class='active'>$k</a></li>";
}
else
{
$ret[] = "<li><a $extra href='$v'>$k </a></li>";
}
}
}
if ( !empty($values['next'] ) )
{
$ret[] = "</ul><a $extra href='{$values['next']}' class='next active'><span></span></a>";
}
if ( !empty($values['last'] ) )
{
//$ret[] = "<a $extra href='{$values['last']}' >Last></a>";
}
//$str_ret = $first . $previous . $str_ret . $next . $last;
if ( $ret )
{
return implode( $seprator, $ret );
}
}
?>
If I print $params I'm getting the values I passed by means of an array $pagination_links. Similarly I want to add one more argument named $total_pages to the above function call and use it in the function body. I tried many ways but it couldn't happen. Can anyone please guide me in this regard please. Thanks in advance.
You call it like this, where $pages is the variable you want to pass:
<div class="pagination">
{pagination_link_01 values=$pagination_links total_pages=$pages}
</div>
and then in the function
if isset($params['total_pages'])
{
... do something with $params['total_pages']...
}
You didn't post much of your code but can't you just pass it to the function?
call your function with your variable. smarty_function_pagination_link_01($params,&$smarty, $total_pages);
e.g.
function smarty_function_pagination_link_01($params, &$smarty, $total_pages)
{
Related
I am trying to make some information widget for the console on client sites (wordpress). The code below works as I need it, perfectly.
$url = 'https://example.site/wp-json/';
function _dashboard_clients_info()
{
global $url;
$request = wp_remote_get(esc_url_raw($url));
if (is_wp_error($request)) {
return false;
}
$html = wp_remote_retrieve_body($request);
$data = json_decode($html);
$head = current(array_filter($data, function ($current) {
return $current->_ID == 2;
}));
$posts = $data;
$foot = current(array_filter($data, function ($current) {
return $current->_ID == 3;
}));
$exclude = array(1, 2, 3, 4);
if (!empty($head->dash_description)) {
echo wpautop('<div class="dash_head">' . $head->dash_description . '</div>');
echo $head->html_css_js;
} else {
};
foreach ($posts as $post) {
if (!in_array($post->_ID, $exclude)) {
if (!empty($posts)) {
echo '<div class="dash_post">';
echo '<h3 class="dash_title">' . $post->dash_title . '</h3>';
echo wpautop('<div class="dash_description">' . $post->dash_description . '</div>');
echo $post->html_css_js;
echo '</div>';
}
} else {
};
}
if (!empty($foot->dash_description)) {
echo wpautop('<div class="dash_foot">' . $foot->dash_description . '</div>');
echo $foot->html_css_js;
} else {
};
}
function _add_dashboard_clients_widget()
{
global $url;
$request = wp_remote_get(esc_url_raw($url));
if (is_wp_error($request)) {
return false;
}
$html = wp_remote_retrieve_body($request);
$data = json_decode($html);
$title = current(array_filter($data, function ($current) {
return $current->_ID == 1;
}));
if (!empty($title->dash_description)) {
$title = '<div class="dash_title">' . $title->dash_description . '</div>';
} else {
};
add_meta_box('dashboard-clients-info', '' . $title . '', '_dashboard_clients_info', $screen, 'normal', 'high');
}
add_action('wp_dashboard_setup', '_add_dashboard_clients_widget');
But I understand that it is not perfect. In particular, I have to include $url twice to get the widget title and body.
I would like to make the $data variable global in order to get the $url once, and then take what me need. I tried it like this, but for some reason it doesn't work, it doesn't return anything.
$url = 'https://example.site/wp-json/';
$request = wp_remote_get(esc_url_raw($url));
if (is_wp_error($request)) {
return false;
}
$html = wp_remote_retrieve_body($request);
$data = json_decode($html);
function _dashboard_clients_info()
{
global $data;
$head = current(array_filter($data, function ($current) {
return $current->_ID == 2;
}));
$posts = $data;
$foot = current(array_filter($data, function ($current) {
return $current->_ID == 3;
}));
$exclude = array(1, 2, 3, 4);
if (!empty($head->dash_description)) {
echo wpautop('<div class="dash_head">' . $head->dash_description . '</div>');
echo $head->html_css_js;
} else {
};
foreach ($posts as $post) {
if (!in_array($post->_ID, $exclude)) {
if (!empty($posts)) {
echo '<div class="dash_post">';
echo '<h3 class="dash_title">' . $post->dash_title . '</h3>';
echo wpautop('<div class="dash_description">' . $post->dash_description . '</div>');
echo $post->html_css_js;
echo '</div>';
}
} else {
};
}
if (!empty($foot->dash_description)) {
echo wpautop('<div class="dash_foot">' . $foot->dash_description . '</div>');
echo $foot->html_css_js;
} else {
};
}
function _add_dashboard_clients_widget()
{
global $data;
$title = current(array_filter($data, function ($current) {
return $current->_ID == 1;
}));
if (!empty($title->dash_description)) {
$title = '<div class="dash_title">' . $title->dash_description . '</div>';
} else {
};
add_meta_box('dashboard-clients-info', 'test', '_dashboard_clients_info', $screen, 'normal', 'high');
}
add_action('wp_dashboard_setup', '_add_dashboard_clients_widget');
I will be grateful for any help in improving this. I'm just learning, I try to get knowledge in this way)))
You can easily define your API URL in wp-config.php, in the theme's functions.php or in a plugin's 'root' file (my-plugin.php for ex.):
define( 'DASHBOARD_API_URL', 'https://example.site/wp-json/' );
Then, create a method to receive the dashboard data making use of the defined DASHBOARD_API_URL:
function wp68412621_get_dashboard_data() {
$response = wp_remote_get( DASHBOARD_API_URL );
if ( is_wp_error( $response ) ) {
return false;
}
return wp_remote_retrieve_body( $response );
}
You should make use of transients to cache the API response in order to avoid excessive API calls. Let's adjust the previous method to:
function wp68412621_get_dashboard_data() {
$transient_key = 'dashboard_data';
$dashboard_data = get_transient( $transient_key );
if ( false === $dashboard_data ) {
$response = wp_remote_get( DASHBOARD_API_URL );
if ( is_wp_error( $response ) ) {
return false;
}
$dashboard_data = wp_remote_retrieve_body( $response );
set_transient( $transientKey, $dashboard_data, 900 );
}
return $dashboard_data;
}
Then, you can call the data method from any other method:
function wp68412621_dashboard_clients_info()
{
$data = wp68412621_get_dashboard_data();
if ( empty( $data ) ) {
return false;
}
// ...
}
function wp68412621_add_dashboard_clients_widget()
{
$data = wp68412621_get_dashboard_data();
if ( empty( $data ) ) {
return false;
}
// ...
}
add_action( 'wp_dashboard_setup', 'wp68412621_add_dashboard_clients_widget' );
I created a custom functionality in WooCommerce and it is about the filtering. If you choose any filter from the sidebar of the eshop and then browse to a single product page you'll have navigation arrows based on your previous selection filtering. It works fine but only in a specific product i get the error
Notice: Trying to get property 'ID' of non-object in functions.php on line 355
function filter_single_post_pagination($output, $format, $link, $post){
$variable = $_GET;
if (count($variable) == 0) {
$url = get_permalink($post->ID);
if (isset($post->ID)) {
if('previous_post_link' === current_filter()){
return "<a href='$url' rel='next'>→</a>";
} else {
return "<a href='$url' rel='prev'>←</a>";
}
}
}
else {
$pids = array();
$product_ids = "";
foreach ($variable as $key => $value) {
$product_ids .= $key."&";
array_push($pids, $key);
}
global $product;
$id = $product->get_id();
$key_val = array_search($id,$pids);
$custom_link = "?".$product_ids;
if (isset($pids[$key_val+1])) {
if('previous_post_link' === current_filter()){
$rel = 'next';
$next_url = get_permalink($pids[$key_val+1]);
return "<a href='$next_url.$custom_link' rel='$rel'>→</a>";
}
}
if (isset($pids[$key_val-1])) {
if('next_post_link' === current_filter()){
$rel = 'prev';
$previous_url = get_permalink($pids[$key_val-1]);
return "<a href='$previous_url.$custom_link' rel='$rel'>←</a>";
}
}
}
}
I've faced funny bug when modifying the e-store engine:
// ...
$this->toolbar_title[] = 'Products';
// ...
print_r($this->toolbar_title);
/*
Array (
[0] => Products
)
*/
$this->toolbar_title[] = 'Filtered by: name';
print_r($this->toolbar_title);
/*
Array
(
[0] => Products
[2] => Filtered by: name
)
*/
// ...
wat??? where is the "1" index??
Tried to reproduce this in clean stand-alone php script - nope! the added element has index "1" as expected. but when doing the same within the engine code - new element has index "2".
There are no setters, no "[]" overloading found, even no any access to the $this->toolbar_title elements by index, only pushing via [];
What the magic is this? What and where should I seek to find the reason?
PHP 5.6, PrestaShop 1.6 engine.
Thanks a lot in advance for any clue.
UPD: the exact code fragment from engine
if ($filter = $this->addFiltersToBreadcrumbs()) {
echo'131-';print_r($this->toolbar_title);
$this->toolbar_title[] = $filter;
echo'132-';var_dump($this->toolbar_title);
}
where addFiltersToBreadcrumbs returns the string and make NO any access to toolbar_title
UPD2:
public function addFiltersToBreadcrumbs()
{
if ($this->filter && is_array($this->fields_list)) {
$filters = array();
foreach ($this->fields_list as $field => $t) {
if (isset($t['filter_key'])) {
$field = $t['filter_key'];
}
if (($val = Tools::getValue($this->table.'Filter_'.$field)) || $val = $this->context->cookie->{$this->getCookieFilterPrefix().$this->table.'Filter_'.$field}) {
if (!is_array($val)) {
$filter_value = '';
if (isset($t['type']) && $t['type'] == 'bool') {
$filter_value = ((bool)$val) ? $this->l('yes') : $this->l('no');
} elseif (isset($t['type']) && $t['type'] == 'date' || isset($t['type']) && $t['type'] == 'datetime') {
$date = Tools::unSerialize($val);
if (isset($date[0])) {
$filter_value = $date[0];
if (isset($date[1]) && !empty($date[1])) {
$filter_value .= ' - '.$date[1];
}
}
} elseif (is_string($val)) {
$filter_value = htmlspecialchars($val, ENT_QUOTES, 'UTF-8');
}
if (!empty($filter_value)) {
$filters[] = sprintf($this->l('%s: %s'), $t['title'], $filter_value);
}
} else {
$filter_value = '';
foreach ($val as $v) {
if (is_string($v) && !empty($v)) {
$filter_value .= ' - '.htmlspecialchars($v, ENT_QUOTES, 'UTF-8');
}
}
$filter_value = ltrim($filter_value, ' -');
if (!empty($filter_value)) {
$filters[] = sprintf($this->l('%s: %s'), $t['title'], $filter_value);
}
}
}
}
if (count($filters)) {
return sprintf($this->l('filter by %s'), implode(', ', $filters));
}
}
}
For php, if you unset one index in array, the index will appear discontinuous growth.
$this->toolbar_title = array_values($this->toolbar_title);
will rebuild the index.
I'm using pods admin plugin and I want to change the value in array. raspored.meta_value are days from 0-6 or from Sunday to Monday.
I want that the value in raspored.meta_value = "dynamic" is changing how the days are going.
Btw: I'm new and not so good in English.Hope you understand :)
$params = array( 'limit' => -1, 'where' => 'raspored.meta_value = "4"' );
$pods = pods( 'raspored', $params );
if ( $pods->total() > 0 ) {
while( $pods->fetch() ) {
//reset id
$pods->id = $pods->id();
//get the template
$temp = $pods->template( 'Probni' );
//output template if it exists
if ( isset( $temp ) ) {
echo $pods->display( 'some_other_field' );
echo $temp;
}
}
//pagination
echo $pods->pagination();
}
else {
echo 'No content found.';
}
I found the solution.
I added if statement and change the array value for "where clause".
Example:
$params = array( 'limit' => -1, 'where' => 'raspored.meta_value = "1"', 'orderby' => 'vrijeme_start ASC' );
if (date("l") === "Monday") {
$params["where"] = array ('1');
}
elseif (date("l") === "Tuesday") {
$params["where"] = array ('2');
}
elseif (date("l") === "Wednesday") {
$params["where"] = array ('3');
}
elseif (date("l") === "Thursday") {
$params["where"] = array ('4');
}
elseif (date("l") === "Friday") {
$params["where"] = array ('5');
}
elseif (date("l") === "Saturday") {
$params["where"] = array ('6');
}
elseif (date("l") === "Sunday") {
$params["where"] = array ('0');
}
else {
echo 'Nema sadržaja';
}
Hope somebody will need this :)
I want to count the rows based on like and unlike value in an array.I want to count the rows based on like and unlike value.I need to display like:3 and unlike:1.Now it display like:0,unlike:0 '$content' value is either {"userid":"1","like":"1"} or {"userid":"1","unlike":"1"}
$like_count = 0;
$unlike_count = 0;
while($like_fet = mysql_fetch_assoc($query))
{
$content = $like_fet['CONTENT_VALUE'];
$datas = json_decode($content);
foreach($datas as $item)
{
$like = $item['like'];
$unlike = $item['unlike'];
if($like != '' && $unlike == '')
{
echo "like";
$like_count++;
}
if($unlike != '' && $like == '')
{
echo "unlike";
$unlike_count++;
}
}
}
$like_count=0;
$unlike_count=0;
while($like_fet=mysql_fetch_assoc($query)) {
$json = json_decode($like_fet['CONTENT_VALUE'], true);
if ( isset($json['like']) ) {
$like_count += $json['like'];
}
if ( isset($json['unlike']) ) {
$unlike_count += $json['unlike'];
}
}
depending on how CONTENT_VALUE actually works this can probably simplified to
$like_count=0;
$unlike_count=0;
while($like_fet=mysql_fetch_assoc($query)) {
$json = json_decode($like_fet['CONTENT_VALUE'], true);
if ( isset($json['like']) ) {
$like_count++;
}
else if ( isset($json['unlike']) ) {
$unlike_count++;
}
else {
trigger_error('CONTENT_VALUE without like/unlike element', E_USER_NOTICE);
}
}