cakephp : getting pagination link in json format - json

I am developing a REST API using CakePHP and want to implement the Instagram API pagination style which looks something like this:
{
...
"pagination": {
"next_url": "https://api.instagram.com/v1/tags/puppy/media/recent?access_token=fb2e77d.47a0479900504cb3ab4a1f626d174d2d&max_id=13872296",
"next_max_id": "13872296"
}
}
I have not used any authorization or whatever, so the access_token part can be ignored. My main motive is to get the pagination links (jump links preferably) as JSON data, so I can use it in my JSON serialized view. Because the usual code:
echo $this->Paginator->prev('< ' . __('previous'), array(), null, array('class' => 'prev disabled'));
echo $this->Paginator->numbers(array('separator' => ''));
echo $this->Paginator->next(__('next') . ' >', array(), null, array('class' => 'next disabled'));
doesn't work and simply displays the equivalent HTML code.
Is there any way I can get it like the Instagram API?

Thanks to noslone, i have got the answer. here is the final edited code:
$url = 'http://yourapp.com/pages/index/limit:7';
$returnArray = array(
.....
'pagination' => array(
'next_url' => null,
'prev_url' => null
)
);
if($this->Paginator->hasNext()) {
$temp = intval($this->Paginator->current())+1;
$returnArray['pagination']['next_url'] = $url."/page:".$temp;
}
if($this->Paginator->hasPrev()) {
$temp = intval($this->Paginator->current())-1;
$returnArray['pagination']['next_url'] = $url."/page:".$temp;
}
echo json_encode($returnArray);
just two changes : first don't forget to add limit:value" to your url and secondly use intval with the Paginator->current.

Try something as follow
$url = 'http://yourapp.com/pages/index';
$returnArray = array(
.....
'pagination' => array(
'next_url' => null,
'prev_url' => null
)
);
if($this->Paginator->hasNext()) {
$returnArray['pagination']['next_url'] = $url.'/page:'. ($this->Paginator->current()+1);
}
if($this->Paginator->hasPrev()) {
$returnArray['pagination']['prev_url'] = $url.'/page:'. ($this->Paginator->current()-1);
}
echo json_encode($returnArray);
If you want, you can add more named params to the url:
$params['pagination']['next_url'] = $url.'/page:'.$this->Paginator->current()+1.'/order:name/direction:asc';

As far as I know, Paginator helper generates the front-end link, and when you click on it, it will generate next link.
I do not think you can use it directly to form the Instagram alike API.

Related

Populate WordPress Advanced Custom Fields with remote JSON in backend

I have custom post types called "Products". and using the AFC(Advanced Custom Fields) plugin with this post type.
Below is what ACF has in fields group
- one filed called 'Product Description' as text area
- three text fields called 'Feature 1, Feature 2,Feature 3'
What I want to achieve is to get the data from external JSON file and populate the above ACF fields in the backend. I did some research and found Wordpress offers wp_remote_get() function to request the remote file. But I have no clue where to begin with to use this function or any other approach to use external JSON and populate these fields. Will really appreciate it someone points me to the right direction or any tutorial that shows how to achieve that. Thanks
I figured it out. View the working code below.
// Get JSON and Decode
$json_request = wp_remote_get( 'http://wp-test/test/data.json');
if( is_wp_error( $json_request ) ) {
return false;
}
$json_body = wp_remote_retrieve_body( $json_request );
$json_data = json_decode( $json_body );
// Create the new post and populate the fields
foreach( $json_data->products as $item ) {
$title = $item->title;
$desc = $item->content;
$status = $item->status;
$new_post = array(
'post_title' => $title,
'post_content' => $desc,
'post_status' => $status,
'post_author' => $userID,
'post_type' => 'products'
);
$post_id = post_exists( $title );
if (!$post_id) {
$post_id = wp_insert_post($new_post);
}
}

Is there a way to put "subject" into the function:mailto in a Datacolumn in yii2?

The problem is the following: I have an Html::mailto() in my Datacolumn where I wanna give the value of the subject with it.
I can't use swiftmailer or some other extensions because I don't want to generate an email, instead, I want to open outlook, by clicking the hyperlink, and have the subject pre-written there.
This is the function:
public static function mailto($text, $email = null, $options = [])
This is my code:
[
'class'=>'\kartik\grid\DataColumn',
'attribute' => 'email',
'label' => 'E-Mail',
'format' => 'raw',
'value' => function($model){
$email = SucheBiete::find()
->select(['email'])
->join('INNER JOIN', 'user', 'user.user_id = suche_biete.user_user_id')
->scalar();
return Html::mailto('Kontaktaufnahme mit: ' . $email,$email, ['subject' => 'Hi There']);
}
]
It works but without getting the subject:
is the $option parameter the right one to give a subject,textbody or cc?
Try to attach the subject to the second parameter:
return Html::mailto('...: ' . $email,"$email?subject=HiThere" );
Maybe you need to encode() the subject for preserving spaces.

Wordpress custom page template returns the theme headers

I want to return a JSON array from a Wordpress website. I know this can be done using plugins like WP REST API and JSON API, but I am dealing with custom post types so I want to avoid complexity and write the queries myself.
I have overridden one of my pages by writing a new php file in my theme directory called page-345.php.
Inside my PHP file I have the following code
$args = array(
'post_type' => 'partner',
'post_status' => 'publish',
'posts_per_page' => -1 // all
);
$query = new WP_Query( $args );
$cars = array();
while( $query->have_posts() ) : $query->the_post();
// Add a car entry
$cars[] = array(
'name' => get_the_title()
);
endwhile;
wp_reset_query();
wp_send_json( $cars );
I code works and I get output in JSON. However, JSON in returned alongside the header of the template. (See screenshot)
I also tried entering the code
header( 'Content-type: application/json' );
at the top of my page, but then Wordpress returns an error saying that headers have already been set.

cakephp Image escape false not working

I am making a Image link to user profile, but it is not working as it should be
this is my code.with this i want to add contoroller,function and id. How i can do it.
<?php
$pic = $User['User']['url'];
if(!$pic){
echo $this->Html->link($this->Html->image('pro.jpg'), array('alt'=>$User['User']['handle'],'title' => $User['User']['handle']),array('escape'=>false) ,array('class'=>'inner_image'));
}else{
echo $this->Html->link($this->Html->image($User['User']['url']),array('alt' => $User['User']['handle']),array('escape'=>false),array('class'=>'inner_image'));
}
?>
This code is making image a link but i can't define a link and it is not accepting the class
.I want to pass this url
$this->Html->link('', array('controller'=>'User','action'=>'view','id'=>$User['User']['id']));
This is how I did it
echo $this->Html->link($this->Html->image($pic, array('class'=>'inner_image')), $url_array, array('alt' => $User['User']['handle'], 'escape'=>false));
What cake version are you using? You don't seem to be following the documentation for Html::link.
HtmlHelper::link(string $title, mixed $url = null, array $options =
array(), string $confirmMessage = false)
alt, escape and class should be indexes in the options array, but you're not defininf the url parameter anywhere.
It should be something like this
if(!$pic){
echo $this->Html->link($this->Html->image('pro.jpg'), $url_array, array('alt'=>$User['User']['handle'],'title' => $User['User']['handle'],'escape'=>false,'class'=>'inner_image'));
} else {
echo $this->Html->link($this->Html->image($User['User']['url']), $url_array, array('alt' => $User['User']['handle'], 'escape'=>false, 'class'=>'inner_image'));
(don't know what url you want to point this at, so replace $url_array to your convenience.

How can I add a "Link Destination" field in the WordPress image attachment editor?

I'm using the WordPress "attachment" feature to allow end users of my theme to upload images that will appear above the post content (not inserted into the post itself).
The only problem I have is that there is not a field to allow the end user to specify the link that should be loaded when the end user clicks on one of the attached images. I'd like to add this field to the post attachment editor (the one that lists the "Gallery" of images attached to the post).
Alternately, and perhaps in addition, I'd like to be able to do the same thing when viewing images via the Media manager listing.
Currently, I'm using the "description" field to store the hyperlink to the image. and retrieving it like so (works perfectly but description is not semantic to link destination):
if ($images = get_children(array('post_parent' => get_the_ID(),'post_type' => 'attachment','post_mime_type' => 'image', 'orderby' => 'menu_order ASC, ID', 'order' => 'DESC' )))
{
foreach( $images as $image ) :
echo "<a href='".$image->post_content."'><img src='".wp_get_attachment_url($image->ID, 'medium')."' /></a>";
endforeach;
}
}
function my_image_attachment_fields_to_edit($form_fields, $post) {
$form_fields["custom1"] = array(
"label" => __("Image Links To"),
"input" => "text", // this is default if "input" is omitted
"value" => get_post_meta($post->ID, "_custom1", true)
);
return $form_fields;
}
function my_image_attachment_fields_to_save($post, $attachment) {
if( isset($attachment['custom1']) ){
update_post_meta($post['ID'], '_custom1', $attachment['custom1']);
}
return $post;
}
add_filter("attachment_fields_to_edit", "my_image_attachment_fields_to_edit", null, 2);
add_filter("attachment_fields_to_save", "my_image_attachment_fields_to_save", null, 2);