diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..15962b9 --- /dev/null +++ b/.gitignore @@ -0,0 +1,17 @@ +# IDE +.idea +.vscode + +# dependency +bower_components +node_modules + +# dist +*.min.js + +# log +*.log + +# lock +yarn.lock +package-lock.json diff --git a/css/main.css b/css/main.css new file mode 100644 index 0000000..7eac836 --- /dev/null +++ b/css/main.css @@ -0,0 +1,15 @@ +.ppy-input-desc { + padding-top: 4px; + padding-left: 4px; + color: #afafaf; +} + +.ppy-qrcode { + max-width: 340px; + max-height: 340px; +} + +.ppy-qrcode svg { + width: 100%; + height: 100%; +} diff --git a/js/main.js b/js/main.js new file mode 100644 index 0000000..e63062d --- /dev/null +++ b/js/main.js @@ -0,0 +1,38 @@ +const qrcode = require('qrcode'); +const generatePayload = require('promptpay-qr'); + +const opt = { + type: 'svg' +}; + +jQuery(document).ready(function () { + const $body = jQuery('body'); + const isAdmin = $body.hasClass('wp-admin'); + + if (isAdmin) { + // do live edit + } else { + const $qrcodes = jQuery('.ppy-qrcode'); + jQuery.each($qrcodes, function () { + const $ele = jQuery(this); + /** @type string */ + const id = jQuery(this).data('promptpay-id').toString(); + /** @type number */ + const amount = parseFloat(jQuery(this).data('amount')); + + if (id) { + const payload = generatePayload(id, { + amount: 0 + }); + + qrcode.toString(payload, opt, function (err, svg) { + if (err) { + console.log('err', err); + } else { + $ele.html(svg); + } + }) + } + }); + } +}); diff --git a/package.json b/package.json new file mode 100644 index 0000000..a5cf274 --- /dev/null +++ b/package.json @@ -0,0 +1,11 @@ +{ + "scripts": { + "build.watch": "webpack js/main.js js/main.min.js -d --watch", + "build.prod": "webpack js/main.js js/main.min.js -p" + }, + "dependencies": { + "promptpay-qr": "^0.4.3", + "qrcode": "^0.9.0", + "webpack": "^3.5.6" + } +} diff --git a/promptpay.php b/promptpay.php new file mode 100644 index 0000000..c2f9fda --- /dev/null +++ b/promptpay.php @@ -0,0 +1,248 @@ +is_debug = false; + $this->menu_page = 'promptpay'; + $this->option_group_name = 'ppy_option_group'; + $this->option_field_name = 'ppy_option_field'; + $this->setting_section_id = 'ppy_setting_section_id'; + + $this->options = get_option( $this->option_field_name ); + + // set default prop + // for only + // - first time + // - no submitting form + $this->set_default_prop(); + + // backend: menu + add_action( 'admin_menu', array( $this, 'admin_menu' ) ); + add_action( 'admin_init', array( $this, 'admin_init' ) ); + + // backend: plugin + add_filter( 'plugin_action_links', array( $this, 'plugin_action_links' ), 10, 4 ); + + // script + add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) ); + add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) ); + + // shortcode + add_shortcode( 'promptpayqr', array( $this, 'shortcode_qrcode' ) ); + } + + /** ================================================================ shortcode + */ + + public function shortcode_qrcode() { + $options = $this->options; + $promptpay_id = $options['field_promptpay_id']; + $amount = 0; + $html = sprintf( '
', + $promptpay_id, + $amount + ); + + return $html; + } + + /** ================================================================ backend: menu + */ + + public function admin_menu() { + add_options_page( + PPY_PLUGIN_NAME, + PPY_PLUGIN_NAME, + 'manage_options', + $this->menu_page, + array( $this, 'admin_page' ) + ); + } + + public function admin_page() { + $this->dump(); ?> +