Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions inc/Base/BaseController.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,14 @@ public function __construct() {
$this->plugin_url = plugin_dir_url( dirname( __FILE__, 2 ) );
$this->plugin = plugin_basename( dirname( __FILE__, 3 ) );
}
public function getDistantTerms( $api = '' ) {
$response = wp_remote_get( $api );
if(is_wp_error($response)) {
return array();
}
return json_decode(wp_remote_retrieve_body($response));
}
public function baseUrlWithoutHttp(){
return preg_replace('#^https?://#', '', get_home_url());
}
}
71 changes: 71 additions & 0 deletions inc/Blocks/SCDetail.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php
/**
* @package CEGOV
*/

namespace CEGOV\Blocks;

use CEGOV\Base\BaseController;

// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Enqueue Gutenberg block assets for both frontend + backend.
*
* Assets enqueued:
* 1. blocks.style.build.css - Frontend + Backend.
* 2. blocks.build.js - Backend.
* 3. blocks.editor.build.css - Backend.
*
* @uses {wp-blocks} for block type registration & related functions.
* @uses {wp-element} for WP Element abstraction — structure of blocks.
* @uses {wp-i18n} to internationalize the block's text.
* @uses {wp-editor} for WP editor styles.
* @since 1.0.0
*/

class SCDetail extends BaseController
{
public function register() {
add_action( "init", array( $this, "registerBlock" ) );
}
public function registerBlock() {
register_block_type(
$this->plugin_name . '/sc-detail', array(
'style' => $this->plugin_name . '-style',
'editor_script' => $this->plugin_name . '-js',
'editor_style' => $this->plugin_name . '-editor-css',
'render_callback' => array( $this, 'renderPostsBlock' ),
'attributes' => array(
'api' => array(
'type' => 'string',
'default' => ''
)
)
)
);
}
public function renderPostsBlock( $attributes ) {
if ( ! isset( $_GET['service_id'] ) ) {
return;
}
$id = $_GET['service_id'];
$data = $this->getDistantTerms( 'https://demo.cambodia.gov.kh/wp-json/wp/v2/service/'.$id );
ob_start();
echo '
<section class="mb-2 mb-md-6">
<header class="block-heading text-left">
<h4 class="text-danger font-weight-bold">
'.$data->title->rendered.'
</h4>
</header>
</section>
';
echo $data->content->rendered;
return ob_get_clean();
}

}

119 changes: 119 additions & 0 deletions inc/Blocks/SCFor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
<?php
/**
* @package CEGOV
*/

namespace CEGOV\Blocks;

use CEGOV\Base\BaseController;

// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Enqueue Gutenberg block assets for both frontend + backend.
*
* Assets enqueued:
* 1. blocks.style.build.css - Frontend + Backend.
* 2. blocks.build.js - Backend.
* 3. blocks.editor.build.css - Backend.
*
* @uses {wp-blocks} for block type registration & related functions.
* @uses {wp-element} for WP Element abstraction — structure of blocks.
* @uses {wp-i18n} to internationalize the block's text.
* @uses {wp-editor} for WP editor styles.
* @since 1.0.0
*/

class SCFor extends BaseController
{
public function register() {
add_action( "init", array( $this, "registerBlock" ) );
}
public function registerBlock() {
register_block_type(
$this->plugin_name . '/sc-for', array(
'style' => $this->plugin_name . '-style',
'editor_script' => $this->plugin_name . '-js',
'editor_style' => $this->plugin_name . '-editor-css',
'render_callback' => array( $this, 'renderPostsBlock' ),
'attributes' => array(
'api' => array(
'type' => 'string',
'default' => ''
),
'service_for' => array(
'type' => 'string',
'default' => ''
),
'service_level' => array(
'type' => 'string',
'default' => ''
),
'page' => array(
'type' => 'string',
'default' => ''
)
)
)
);
}
public function renderPostsBlock( $attributes ) {
ob_start();

$column = 3;
$service_for = $attributes['service_for'];
$service_level = $attributes['service_level'];
$api = $attributes['api'];
$get_fetch = $this->getDistantTerms( $api.'/for='.$service_for.'/level='.$service_level );
echo (
'
<div class="">
<div class="block-topic">
<table class="table table-bordered">
<tbody>
<tr>
'
);

foreach ( $get_fetch as $key => $term ) {
printf(
'
<td>
<article>
<figure class="d-lg-flex">
<div class="mr-lg-2">
<img width="34" src="%s" />
</div>
<figcaption>
<a href="%s?topic=%s&for=%s&level=%s">
<h5>%s</h5>
</a>
<p class="d-none d-lg-block">%s</p>
</figcaption>
</figure>
</article>
</td>
',
$term->icon,
$attributes['page'],
$term->id,
$service_for,
$service_level,
$term->name,
$term->description
);
if ( ( ( $key + 1 ) % $column ) === 0 ) {
echo '</tr><tr>';
}

}

echo ( '</tr></tbody></table></div></div>' );

return ob_get_clean();
}

}

70 changes: 70 additions & 0 deletions inc/Blocks/SCUnion.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php
/**
* @package CEGOV
*/

namespace CEGOV\Blocks;

use CEGOV\Base\BaseController;

// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Enqueue Gutenberg block assets for both frontend + backend.
*
* Assets enqueued:
* 1. blocks.style.build.css - Frontend + Backend.
* 2. blocks.build.js - Backend.
* 3. blocks.editor.build.css - Backend.
*
* @uses {wp-blocks} for block type registration & related functions.
* @uses {wp-element} for WP Element abstraction — structure of blocks.
* @uses {wp-i18n} to internationalize the block's text.
* @uses {wp-editor} for WP editor styles.
* @since 1.0.0
*/

class SCUnion extends BaseController
{
public function register() {
add_action( "init", array( $this, "registerBlock" ) );
}
public function registerBlock() {
register_block_type(
$this->plugin_name . '/sc-union', array(
'style' => $this->plugin_name . '-style',
'editor_script' => $this->plugin_name . '-js',
'editor_style' => $this->plugin_name . '-editor-css',
'render_callback' => array( $this, 'renderPostsBlock' ),
'attributes' => array(
'api' => array(
'type' => 'string',
'default' => ''
),
'page' => array(
'type' => 'string',
'default' => ''
)
)
)
);
}
public function renderPostsBlock( $attributes ) {
$api = $attributes['api'];
$service_topic = isset( $_GET['topic'] ) ? $_GET['topic'] : '';
$service_for = isset( $_GET['for'] ) ? $_GET['for'] : '';
$service_level = isset( $_GET['level'] ) ? $_GET['level'] : '';
$des_page_slug = $attributes['page'];
if ( !$api || !$service_for || !$service_topic || !$des_page_slug ) {
return ob_get_clean();
}
ob_start();

echo ( $this->getDistantTerms( $api.'/topic='.$service_topic.'/for='.$service_for.'/level='.$service_level.'/base='.$this->baseUrlWithoutHttp().'/page='.$des_page_slug ));

return ob_get_clean();
}
}

3 changes: 3 additions & 0 deletions inc/Init.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ public static function getServices() {
LoadSettings::class,
RegisterAsset::class,
Blocks\Services::class,
Blocks\SCDetail::class,
Blocks\SCFor::class,
Blocks\SCUnion::class
);
}

Expand Down
6 changes: 4 additions & 2 deletions src/blocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,7 @@
* Webpack is compiling as the input file.
*/

import './services/block';
// import './block-service-content/block.js';
import './services/block'
import './sc-detail/block'
import './sc-for/block'
import './sc-union/block'
2 changes: 1 addition & 1 deletion src/common.scss
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ $black: rgb(41, 41, 41);
$white: #f4f4f4;
$gray: #dedede;
$green: #bada55;
$red: orangered;
$red: orangered;
16 changes: 16 additions & 0 deletions src/components/MarkText.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const { __ } = wp.i18n
const { TextControl } = wp.components

const MarkText = ( { attributes, setAttributes } ) => {
const { mark_text } = attributes

return (
<TextControl
label= { __( 'Mark Text', 'egov' ) }
value={ mark_text }
onChange={ ( value ) => setAttributes( { mark_text: value } ) }
/>
)
}

export default MarkText
53 changes: 53 additions & 0 deletions src/sc-detail/block.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import './style.scss'
import './editor.scss'
const { __ } = wp.i18n
import MarkText from '../components/MarkText.jsx'
const { registerBlockType } = wp.blocks
const { InspectorControls } = wp.blockEditor
const { PanelBody, __experimentalNumberControl : NumberControl, __experimentalInputControl : InputControl } = wp.components
const { Fragment } = wp.element
registerBlockType( 'digital-blocks/sc-detail', {
title: __( 'Serivce Cambodia Detail', 'CEGOV' ),
icon: 'admin-page',
category: 'digital-blocks',
keywords: [
__( 'service', 'CEGOV' ),
__( 'cambodia', 'CEGOV' ),
__( 'egov block', 'CEGOV' )
],
attributes: {
mark_text: {
type: 'string',
default: 'Service Cambodia Detail'
},
toggle_panel: {
type: 'boolean',
default: false
}
},
edit: ( { attributes, setAttributes } ) => {
const {
mark_text
} = attributes

return (
<Fragment>
<InspectorControls>
<PanelBody>
<MarkText
attributes={attributes}
setAttributes={setAttributes}
/>
</PanelBody>
</InspectorControls>
<div className={ 'border p-3' }>
<small>{ mark_text }</small>
</div>
</Fragment>
)
},

save: ( { attributes } ) => {
return null
}
} )
16 changes: 16 additions & 0 deletions src/sc-detail/editor.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* #.# Editor Styles
*
* CSS for just Backend enqueued after style.scss
* which makes it higher in priority.
*/

.wp-block-cgb-block-digital-blocks {
background: $green;
border: 0.2rem solid $black;
color: $black;
margin: 0 auto;
max-width: 740px;
padding: 2rem;
}

Loading