Skip to content

Commit

Permalink
chore: save progress
Browse files Browse the repository at this point in the history
  • Loading branch information
HardeepAsrani committed Feb 5, 2024
1 parent e537ccc commit 7afae81
Show file tree
Hide file tree
Showing 11 changed files with 396 additions and 174 deletions.
184 changes: 104 additions & 80 deletions inc/class-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public function send( \WP_REST_Request $request ) {
$request = wp_remote_post(
QUICKWP_APP_API . 'wizard/send',
array(
'timeout' => 10, // phpcs:ignore WordPressVIPMinimum.Performance.RemoteRequestTimeout.timeout_timeout
'timeout' => 20, // phpcs:ignore WordPressVIPMinimum.Performance.RemoteRequestTimeout.timeout_timeout
'body' => array(
'step' => $data['step'],
'message' => $data['message'],
Expand Down Expand Up @@ -189,7 +189,7 @@ public function status( \WP_REST_Request $request ) {
$request = wp_safe_remote_get(
$request_url,
array(
'timeout' => 10, // phpcs:ignore WordPressVIPMinimum.Performance.RemoteRequestTimeout.timeout_timeout
'timeout' => 20, // phpcs:ignore WordPressVIPMinimum.Performance.RemoteRequestTimeout.timeout_timeout
)
);

Expand Down Expand Up @@ -232,7 +232,7 @@ public function get( \WP_REST_Request $request ) {
$request = wp_safe_remote_get(
$request_url,
array(
'timeout' => 10, // phpcs:ignore WordPressVIPMinimum.Performance.RemoteRequestTimeout.timeout_timeout
'timeout' => 20, // phpcs:ignore WordPressVIPMinimum.Performance.RemoteRequestTimeout.timeout_timeout
)
);

Expand Down Expand Up @@ -275,7 +275,7 @@ public function templates( \WP_REST_Request $request ) {
$request = wp_safe_remote_get(
$request_url,
array(
'timeout' => 10, // phpcs:ignore WordPressVIPMinimum.Performance.RemoteRequestTimeout.timeout_timeout
'timeout' => 20, // phpcs:ignore WordPressVIPMinimum.Performance.RemoteRequestTimeout.timeout_timeout
)
);

Expand All @@ -297,82 +297,25 @@ public function templates( \WP_REST_Request $request ) {
$items = self::process_json_from_response( $response->data );

if ( ! $items ) {
return new \WP_REST_Response( array( 'error' => __( 'Error', 'quickwp' ) ), 500 );
return new \WP_REST_Response( array( 'error' => __( 'Error Parsing JSON', 'quickwp' ) ), 500 );
}

$patterns_used = array();

foreach ( $items as $item ) {
if ( ! isset( $item['slug'] ) || ! isset( $item['order'] ) || ! isset( $item['strings'] ) ) {
continue;
}

$patterns_used[] = array(
'order' => $item['order'],
'slug' => $item['slug'],
);

$strings = $item['strings'];

foreach ( $strings as $string ) {
add_filter(
'quickwp/' . $string['slug'],
function () use( $string ) {
return esc_html( $string['value'] );
}
);
}

if ( isset( $item['images'] ) ) {
$images = $item['images'];
$result = array();

foreach ( $images as $image ) {
add_filter(
'quickwp/' . $image['slug'],
function ( $value ) use( $image ) {
// Check if image slug is a valid URL.
if ( filter_var( $image['src'], FILTER_VALIDATE_URL ) ) {
return esc_url( $image['src'] );
}
foreach( $items as $item ) {
$pattern = self::extract_patterns( $item );

Check failure on line 306 in inc/class-api.php

View workflow job for this annotation

GitHub Actions / PHPStan

Parameter #1 $items of static method ThemeIsle\QuickWP\API::extract_patterns() expects array<object>, object given.

return $value;
}
);
}
}
}

usort(
$patterns_used,
function ( $a, $b ) {
return $a['order'] <=> $b['order'];
}
);

$theme_path = get_stylesheet_directory();

$filtered_patterns = array();

foreach ( $patterns_used as $pattern ) {
$pattern['slug'] = str_replace( 'quickwp/', '', $pattern['slug'] );

$pattern_path = $theme_path . '/patterns/' . $pattern['slug'] . '.php';

if ( ! file_exists( $pattern_path ) ) {
if ( ! $pattern ) {
continue;
}

ob_start();
include $pattern_path;
$pattern_content = ob_get_clean();

$filtered_patterns[] = $pattern_content;
$result[] = $pattern;
}

return new \WP_REST_Response(
array(
'status' => 'success',
'data' => implode( '', $filtered_patterns ),
'data' => $result,
),
200
);
Expand All @@ -399,7 +342,7 @@ public function images( \WP_REST_Request $request ) {
$request = wp_safe_remote_get(
$request_url,
array(
'timeout' => 10, // phpcs:ignore WordPressVIPMinimum.Performance.RemoteRequestTimeout.timeout_timeout
'timeout' => 20, // phpcs:ignore WordPressVIPMinimum.Performance.RemoteRequestTimeout.timeout_timeout
)
);

Expand All @@ -414,12 +357,10 @@ public function images( \WP_REST_Request $request ) {
*/
$response = json_decode( wp_remote_retrieve_body( $request ) );

if ( ! isset( $response->photos ) || count( $response->photos ) === 0 ) {
if ( ! isset( $response->photos ) ) {
return new \WP_REST_Response( array( 'error' => __( 'Error', 'quickwp' ) ), 500 );
}



return new \WP_REST_Response( $response, 200 );
}

Expand Down Expand Up @@ -448,15 +389,12 @@ private static function process_json_from_response( $data ) {
return $json_object;
}

return false;
throw new \Exception( 'Invalid JSON' );
} catch ( \Exception $e ) {
// If parsing failed, try to find a JSON array in the string.
preg_match( '/\[(.|\n)*\]/', $json_string, $matches );

if ( ! empty( $matches ) ) {
$json_array_string = $matches[0];
$json_object = json_decode( $json_array_string, true );

if ( substr( $json_string, 0, 7 ) === '```json' && substr( trim( $json_string ), -3 ) === '```' ) {
$cleaned_json = trim( str_replace( [ '```json', '```' ], '', $json_string ) );
$json_object = json_decode( $cleaned_json, true );

if ( is_array( $json_object ) ) {
return $json_object;
}
Expand All @@ -465,4 +403,90 @@ private static function process_json_from_response( $data ) {

return false;
}

/**
* Extract Patterns.
*
* @param array<object> $items Items.
*
* @return array
*/
private static function extract_patterns( $items ) {

Check failure on line 414 in inc/class-api.php

View workflow job for this annotation

GitHub Actions / PHPStan

Method ThemeIsle\QuickWP\API::extract_patterns() return type has no value type specified in iterable type array.
if ( ! isset( $items['slug'] ) || ! isset( $items['data'] ) ) {
return;

Check failure on line 416 in inc/class-api.php

View workflow job for this annotation

GitHub Actions / PHPStan

Method ThemeIsle\QuickWP\API::extract_patterns() should return array but empty return statement found.
}

$patterns_used = array();

foreach ( $items['data'] as $item ) {

Check failure on line 421 in inc/class-api.php

View workflow job for this annotation

GitHub Actions / PHPStan

Argument of an invalid type object supplied for foreach, only iterables are supported.
if ( ! isset( $item['slug'] ) || ! isset( $item['order'] ) || ! isset( $item['strings'] ) ) {
continue;
}

$patterns_used[] = array(
'order' => $item['order'],
'slug' => $item['slug'],
);

$strings = $item['strings'];

foreach ( $strings as $string ) {
add_filter(
'quickwp/' . $string['slug'],
function () use( $string ) {
return esc_html( $string['value'] );
}
);
}

if ( isset( $item['images'] ) ) {
$images = $item['images'];

foreach ( $images as $image ) {
add_filter(
'quickwp/' . $image['slug'],
function ( $value ) use( $image ) {
if ( filter_var( $image['src'], FILTER_VALIDATE_URL ) && ( strpos( $image['src'], 'pexels.com' ) !== false ) ) {
return esc_url( $image['src'] );
}

return $value;
}
);
}
}
}

usort(
$patterns_used,
function ( $a, $b ) {
return $a['order'] <=> $b['order'];
}
);

$theme_path = get_stylesheet_directory();

$filtered_patterns = array();

foreach ( $patterns_used as $pattern ) {
$pattern['slug'] = str_replace( 'quickwp/', '', $pattern['slug'] );

$pattern_path = $theme_path . '/patterns/' . $pattern['slug'] . '.php';

if ( ! file_exists( $pattern_path ) ) {
continue;
}

ob_start();
include $pattern_path;
$pattern_content = ob_get_clean();

$filtered_patterns[] = $pattern_content;
}

return array(
'slug' => $items['slug'],
'patterns' => implode( '', $filtered_patterns ),
);
}
}
58 changes: 58 additions & 0 deletions inc/class-main.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ private function register_hooks() {
add_action( 'admin_init', array( $this, 'guided_access' ) );
add_filter( 'show_admin_bar', '__return_false' ); // phpcs:ignore WordPressVIPMinimum.UserExperience.AdminBarRemoval.RemovalDetected
}

add_action( 'wp_print_footer_scripts', array( $this, 'print_footer_scripts' ) );
}

/**
Expand Down Expand Up @@ -114,6 +116,62 @@ public function enqueue_frontend_assets() {
);
}

/**
* Print footer scripts.
*
* @return void
*/
public function print_footer_scripts() {
if ( ! is_admin() ) {
return;
}

$current_screen = get_current_screen();

if (
! current_user_can( 'manage_options' ) ||
! isset( $current_screen->id ) ||
'site-editor' !== $current_screen->id
) {
return;
}

?>
<script>
/**
* We use this script to adjust the vh units in the DOM to match the actual viewport height in the editor.
* This is necessary because the editor's viewport height is not the same as the browser's viewport height.
* In some blocks we use the vh unit to set the height of elements, and we need to adjust it to match the editor's viewport height.
*/
const adjustVHUnits = () => {
const parentVHValue = getComputedStyle( document.documentElement).getPropertyValue( '--parent-vh' );
const parentVH = parseInt( parentVHValue, 10 );

if ( isNaN( parentVH ) ) {
return;
}

const convertVHtoPixels = ( vhValue ) => ( vhValue / 100 ) * parentVH;

document.querySelectorAll( '*' ).forEach( el => {
const style = el.getAttribute( 'style' );

if ( style && style.includes( 'vh' ) ) {
const updatedStyle = style.replace( /(\d+(\.\d+)?)vh/g, ( match, vhValue ) => {
const pixelValue = convertVHtoPixels( parseFloat( vhValue ) );
return `${ pixelValue }px`;
});

el.setAttribute( 'style', updatedStyle );
}
});
}

document.addEventListener( 'DOMContentLoaded', adjustVHUnits );
</script>
<?php
}

/**
* Guided access.
*
Expand Down
4 changes: 2 additions & 2 deletions src/components/Loader.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* External dependencies.
*/
import classnames from 'classnames';
import classNames from 'classnames';

/**
* WordPress dependencies.
Expand Down Expand Up @@ -36,7 +36,7 @@ const Loader = () => {
{ Array.from({ length: numberOfPages }).map( ( _, page ) => (
<li
key={ page }
className={ classnames(
className={ classNames(
'inline-flex m-0 justify-center items-center text-fg h-6 w-6 !min-w-0 !min-h-0 transition-all',
{
'opacity-30': page !== currentPage
Expand Down
Loading

0 comments on commit 7afae81

Please sign in to comment.