-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.php
206 lines (171 loc) · 7.02 KB
/
functions.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
<?php
// register the CPT Movies
function register_movie_cpt() {
register_post_type( 'movies', array(
'label' => 'Movies',
'show_in_rest' => true,
'public' => true,
'capability_type' => 'post',
));
}
add_action( 'init', 'register_movie_cpt' );
// update cron job
if ( ! wp_next_scheduled( 'update_movie_list' ) ) {
wp_schedule_event( time(), 'weekly', 'update_movie_list' );
}
// run function wether not logged in or logged in
add_action('wp_ajax_nopriv_get_movies_api', 'get_movies_api');
add_action('wp_ajax_get_movies_api', 'get_movies_api');
function get_movies_api () {
// $current_page = ( ! empty( $_POST['current_page'] ) ) ? $_POST['current_page'] : 1;
// returning array of objects
// HTTP request
$getresults = wp_remote_retrieve_body( wp_remote_get('https://api.themoviedb.org/3/movie/now_playing?api_key=5e6c8a2634f9053bc5bd33d1cc80a33d&language=en-US&page=1' ) );
// decode json , if set to true will retun array , if false - string
$getresults = json_decode($getresults, true);
// loop trough json array
//get the titlte
$results = $getresults['results'];
// title loop
foreach($results as $result) {
$original_title = $result['original_title'];
// overview loop
$results = $getresults['results'];
foreach($results as $overviews){
$overview = $overviews['overview'];
}
// release date loop
$results = $getresults['results'];
foreach($results as $releases){
$release = $releases['release_date'];
}
// poster loop
$results = $getresults['results'];
foreach($results as $posters){
$poster = $posters['poster_path'];
}
// appending the title,poster,release date to the posts and inserting posts
$movie_slug = ($result['original_title'] );
$overview = ($result['overview'] );
$poster = ($result['poster_path'] );
$release = ($result['release_date'] );
$my_post = wp_insert_post([
'original_title' => $movie_slug,
'post_title' => $movie_slug,
'post_type' => 'movies',
'post_status' => 'publish',
'status' => 'publish',
'slug' =>'slug',
]);
// if error don't continue || continue
if( is_wp_error( $my_post ) || $my_post === 0 ) {
die();
// continue;
}
//ACF key array
$acf_keys = [
'field_634a9c08dcae4' => 'overview',
];
//loop trough array and update the fields
foreach($acf_keys as $key => $name) {
update_field($key, $overview , $my_post);
}
$date = [
'field_6350541b9e90d' => 'release_date',
];
foreach($date as $keys => $name) {
update_field($keys, $release , $my_post);
}
$image = [
'field_6355552df2bbf' => 'poster',
];
foreach($image as $images => $name){
update_field($images, $poster, $my_post);
}
}
// either array with results or empty array
// if empty array return false stop getting results
if (! is_array($getresults) || empty($getresults)) {
return false;
}
// call the api until there are empty results
// wp_remote_post (admin_url('admin-ajax.php?action=get_movies_api'), [
// // no blocking by WP
// 'blocking' => false,
// 'sslverify' => false,
// // argument intake and adding to current page variable
// 'body' => [
// // 'current_page' => $current_page
// ]
// ]);
}
/*
|--------------------------------------------------------------------------
| Register The Auto Loader
|--------------------------------------------------------------------------
|
| Composer provides a convenient, automatically generated class loader for
| our theme. We will simply require it into the script here so that we
| don't have to worry about manually loading any of our classes later on.
|
*/
if (! file_exists($composer = __DIR__.'/vendor/autoload.php')) {
wp_die(__('Error locating autoloader. Please run <code>composer install</code>.', 'sage'));
}
require $composer;
/*
|--------------------------------------------------------------------------
| Register The Bootloader
|--------------------------------------------------------------------------
|
| The first thing we will do is schedule a new Acorn application container
| to boot when WordPress is finished loading the theme. The application
| serves as the "glue" for all the components of Laravel and is
| the IoC container for the system binding all of the various parts.
|
*/
try {
\Roots\bootloader();
} catch (Throwable $e) {
wp_die(
__('You need to install Acorn to use this theme.', 'sage'),
'',
[
'link_url' => 'https://docs.roots.io/acorn/2.x/installation/',
'link_text' => __('Acorn Docs: Installation', 'sage'),
]
);
}
/*
|--------------------------------------------------------------------------
| Register Sage Theme Files
|--------------------------------------------------------------------------
|
| Out of the box, Sage ships with categorically named theme files
| containing common functionality and setup to be bootstrapped with your
| theme. Simply add (or remove) files from the array below to change what
| is registered alongside Sage.
|
*/
collect(['setup', 'filters'])
->each(function ($file) {
if (! locate_template($file = "app/{$file}.php", true, true)) {
wp_die(
/* translators: %s is replaced with the relative file path */
sprintf(__('Error locating <code>%s</code> for inclusion.', 'sage'), $file)
);
}
});
/*
|--------------------------------------------------------------------------
| Enable Sage Theme Support
|--------------------------------------------------------------------------
|
| Once our theme files are registered and available for use, we are almost
| ready to boot our application. But first, we need to signal to Acorn
| that we will need to initialize the necessary service providers built in
| for Sage when booting.
|
*/
add_theme_support('sage');
// ///////////////////////////