Skip to content

Commit 23a9b65

Browse files
emeaguiarravichdev
authored andcommitted
Add endpoint for auto updates
1 parent 8c971b6 commit 23a9b65

File tree

3 files changed

+63
-1
lines changed

3 files changed

+63
-1
lines changed

plugin/php/api/class-update-fonts.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ class Update_Fonts extends Updates_API_Base {
4141

4242
const LAST_UPDATED = 'google-fonts-last-updated';
4343

44+
const AUTO_UPDATE_SLUG = 'google-fonts-auto-update';
45+
4446
/**
4547
* API option name
4648
*
@@ -243,4 +245,14 @@ public static function get_last_updated() {
243245
public static function get_api_slug() {
244246
return self::API_KEY_SLUG;
245247
}
248+
249+
/**
250+
* Add auto update option in database
251+
* Allow or restrict auto updates
252+
*
253+
* @param bool $activate Wheter to auto update items. Defaults to false
254+
*/
255+
public function toggle_auto_updates( $activate = false ) {
256+
return update_option( self::AUTO_UPDATE_SLUG, $activate );
257+
}
246258
}

plugin/php/api/class-update-icons.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ class Update_Icons extends Updates_API_Base {
3939

4040
const LAST_UPDATED = 'google-icons-last-updated';
4141

42+
const AUTO_UPDATE_SLUG = 'google-icons-auto-update';
43+
4244
/**
4345
* URL for holding the codepoints data
4446
*

plugin/php/class-design-assets-rest-controller.php

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,22 @@ public function register_routes() {
118118
],
119119
'schema' => [ $this, 'get_item_schema' ],
120120
]
121-
);
121+
);
122+
123+
register_rest_route(
124+
$this->namespace,
125+
'/' . $this->rest_base . '/toggle-auto-updates',
126+
[
127+
[
128+
'methods' => \WP_REST_Server::CREATABLE,
129+
'callback' => [ $this, 'toggle_auto_updates' ],
130+
'permission_callback' => function( WP_REST_Request $request ) {
131+
return current_user_can( 'manage_options' );
132+
},
133+
],
134+
'schema' => [ $this, 'get_item_schema' ],
135+
]
136+
);
122137
}
123138

124139
/**
@@ -296,4 +311,37 @@ public static function get_api_status() {
296311

297312
return 'ok';
298313
}
314+
315+
/**
316+
* Toggle auto updates option in database
317+
* Usage:
318+
* /wp-json/material-design/v1/design-assets/toggle-auto-updates
319+
*
320+
* @param WP_REST_Request $request REST request object.
321+
*
322+
* @return mixed
323+
*/
324+
public function toggle_auto_updates( $request ) {
325+
$type = $request->get_param( 'type' );
326+
$slug = '';
327+
328+
if ( 'FONTS' === $type ) {
329+
$slug = Update_Fonts::AUTO_UPDATE_SLUG;
330+
} elseif ( 'ICONS' === $type ) {
331+
$slug = Update_Icons::AUTO_UPDATE_SLUG;
332+
}
333+
334+
if ( empty( $slug ) ) {
335+
$response = new WP_Error( 'material-auto-updates', __( 'No auto update to activate', 'material-design' ) );
336+
} else {
337+
$value = intval( get_option( $slug ) );
338+
$response = update_option( $slug, ! $value );
339+
}
340+
341+
if ( is_wp_error( $response ) ) {
342+
return $response;
343+
}
344+
345+
return new WP_REST_Response( $response, 200 );
346+
}
299347
}

0 commit comments

Comments
 (0)