| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Plugin Name: Qyrr-Code |
| 5 |
* Plugin URI: https://patrickposner.com/plugins/qyrr |
| 6 |
* Description: QR-Code generation, management and tracking as it should be. |
| 7 |
* Version: 2.0.6 |
| 8 |
* Author: Patrick Posner |
| 9 |
* Author URI: https://patrickposner.com |
| 10 |
* License: GPL-2.0+ |
| 11 |
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt |
| 12 |
* Text Domain: qyrr-code |
| 13 |
* Domain Path: /languages |
| 14 |
* |
| 15 |
*/ |
| 16 |
define( 'QYRR_PATH', untrailingslashit( plugin_dir_path( __FILE__ ) ) ); |
| 17 |
define( 'QYRR_URL', untrailingslashit( plugin_dir_url( __FILE__ ) ) ); |
| 18 |
define( 'QYRR_VERSION', '2.0.6' ); |
| 19 |
// load setup. |
| 20 |
require_once QYRR_PATH . '/inc/freemius-setup.php'; |
| 21 |
add_action( 'plugins_loaded', 'qyrr_run_plugin' ); |
| 22 |
// run plugin. |
| 23 |
if ( !function_exists( 'qyrr_run_plugin' ) ) { |
| 24 |
add_action( 'plugins_loaded', 'qyrr_run_plugin' ); |
| 25 |
/** |
| 26 |
* Run plugin |
| 27 |
* |
| 28 |
* @return void |
| 29 |
*/ |
| 30 |
function qyrr_run_plugin() { |
| 31 |
require_once QYRR_PATH . '/inc/class-qyrr-block-editor.php'; |
| 32 |
require_once QYRR_PATH . '/inc/class-qyrr-admin.php'; |
| 33 |
require_once QYRR_PATH . '/inc/admin/inc/class-qyrr-settings.php'; |
| 34 |
require_once QYRR_PATH . '/inc/class-qyrr-meta.php'; |
| 35 |
require_once QYRR_PATH . '/inc/class-qyrr-rest.php'; |
| 36 |
require_once QYRR_PATH . '/inc/class-qyrr-shortcode.php'; |
| 37 |
qyrr\QYRR_Block_Editor::get_instance(); |
| 38 |
qyrr\QYRR_Admin::get_instance(); |
| 39 |
qyrr\QYRR_Settings::get_instance(); |
| 40 |
qyrr\QYRR_Meta::get_instance(); |
| 41 |
qyrr\QYRR_Rest::get_instance(); |
| 42 |
qyrr\QYRR_Shortcode::get_instance(); |
| 43 |
} |
| 44 |
|
| 45 |
// Register block. |
| 46 |
add_action( 'init', 'qyrr_register_qyrr_block' ); |
| 47 |
function qyrr_register_qyrr_block() { |
| 48 |
if ( qyrr_is_post_type() ) { |
| 49 |
register_block_type( __DIR__ . '/build/qr' ); |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
add_action( 'wp_enqueue_scripts', 'qyrr_register_scripts' ); |
| 54 |
function qyrr_register_scripts() { |
| 55 |
$qr_asset_file = (include plugin_dir_path( __FILE__ ) . 'build/qr/index.asset.php'); |
| 56 |
wp_register_script( |
| 57 |
'qyrr-code-script', |
| 58 |
plugins_url( 'build/qr/index.js', __FILE__ ), |
| 59 |
$qr_asset_file['dependencies'], |
| 60 |
$qr_asset_file['version'] |
| 61 |
); |
| 62 |
} |
| 63 |
|
| 64 |
// Register Block styles and scripts. |
| 65 |
add_action( 'enqueue_block_editor_assets', 'qyrr_add_block_editor_assets' ); |
| 66 |
function qyrr_add_block_editor_assets() { |
| 67 |
if ( qyrr_is_post_type() ) { |
| 68 |
$qr_asset_file = (include plugin_dir_path( __FILE__ ) . 'build/qr/index.asset.php'); |
| 69 |
$qyrr_options = get_option( 'qyrr' ); |
| 70 |
$script_args = array( |
| 71 |
'is_pro' => false, |
| 72 |
); |
| 73 |
if ( isset( $qyrr_options['fonts_api_key'] ) ) { |
| 74 |
$script_args['google_fonts_api_key'] = $qyrr_options['fonts_api_key']; |
| 75 |
} |
| 76 |
// Ad home URL to args. |
| 77 |
$script_args['home_url'] = home_url(); |
| 78 |
// Handle rerender on update. |
| 79 |
if ( qyrr_is_post_type() ) { |
| 80 |
wp_enqueue_script( |
| 81 |
'qyrr-code-rerender', |
| 82 |
QYRR_URL . '/assets/rerender.js', |
| 83 |
array( |
| 84 |
'wp-api', |
| 85 |
'wp-components', |
| 86 |
'wp-element', |
| 87 |
'wp-api-fetch', |
| 88 |
'wp-data', |
| 89 |
'wp-i18n' |
| 90 |
), |
| 91 |
QYRR_VERSION, |
| 92 |
true |
| 93 |
); |
| 94 |
} |
| 95 |
wp_enqueue_script( |
| 96 |
'qyrr-code-script', |
| 97 |
plugins_url( 'build/qr/index.js', __FILE__ ), |
| 98 |
$qr_asset_file['dependencies'], |
| 99 |
$qr_asset_file['version'] |
| 100 |
); |
| 101 |
wp_localize_script( 'qyrr-code-script', 'license', $script_args ); |
| 102 |
wp_enqueue_style( 'qyrr-code-style', plugins_url( 'build/qr/index.css', __FILE__ ) ); |
| 103 |
} |
| 104 |
$selector_asset_file = (include plugin_dir_path( __FILE__ ) . 'build/qr-selector/index.asset.php'); |
| 105 |
$uploads_dir = wp_upload_dir(); |
| 106 |
wp_enqueue_script( |
| 107 |
'qyrr-code-selector-script', |
| 108 |
plugins_url( 'build/qr-selector/index.js', __FILE__ ), |
| 109 |
$selector_asset_file['dependencies'], |
| 110 |
$selector_asset_file['version'] |
| 111 |
); |
| 112 |
wp_localize_script( 'qyrr-code-selector-script', 'qyrr_options', array( |
| 113 |
'baseurl' => $uploads_dir['baseurl'], |
| 114 |
) ); |
| 115 |
wp_enqueue_style( 'qyrr-code-selector-style', plugins_url( 'build/qr-selector/index.css', __FILE__ ) ); |
| 116 |
// Make the blocks translatable. |
| 117 |
if ( function_exists( 'wp_set_script_translations' ) ) { |
| 118 |
wp_set_script_translations( 'qyrr-code-script', 'qyrr-code', plugin_dir_path( __FILE__ ) . 'languages' ); |
| 119 |
wp_set_script_translations( 'qyrr-code-selector-script', 'qyrr-code', plugin_dir_path( __FILE__ ) . 'languages' ); |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
register_activation_hook( __FILE__, 'qyrr_activate' ); |
| 124 |
/** |
| 125 |
* Add a flag that will allow to flush the rewrite rules when needed. |
| 126 |
*/ |
| 127 |
function qyrr_activate() { |
| 128 |
if ( !get_option( 'qyrr_activated' ) ) { |
| 129 |
add_option( 'qyrr_activated', true ); |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
add_action( |
| 134 |
'upgrader_process_complete', |
| 135 |
'qyrr_upgrade', |
| 136 |
10, |
| 137 |
2 |
| 138 |
); |
| 139 |
/** |
| 140 |
* Add a flag that will allow to flush the rewrite rules when needed. |
| 141 |
* @throws Exception |
| 142 |
*/ |
| 143 |
function qyrr_upgrade( $upgrader_object, $options ) { |
| 144 |
$current_plugin_path_name = plugin_basename( __FILE__ ); |
| 145 |
if ( $options['action'] == 'update' && $options['type'] == 'plugin' ) { |
| 146 |
foreach ( $options['plugins'] as $each_plugin ) { |
| 147 |
if ( $each_plugin == $current_plugin_path_name ) { |
| 148 |
if ( !get_option( 'qyrr_activated' ) ) { |
| 149 |
add_option( 'qyrr_activated', true ); |
| 150 |
} |
| 151 |
} |
| 152 |
} |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
function qyrr_is_post_type() { |
| 157 |
// Check if this is the intended custom post type |
| 158 |
if ( is_admin() ) { |
| 159 |
global $pagenow; |
| 160 |
$typenow = ''; |
| 161 |
if ( 'post-new.php' === $pagenow ) { |
| 162 |
if ( isset( $_REQUEST['post_type'] ) && post_type_exists( $_REQUEST['post_type'] ) ) { |
| 163 |
$typenow = $_REQUEST['post_type']; |
| 164 |
} |
| 165 |
} elseif ( 'post.php' === $pagenow ) { |
| 166 |
if ( isset( $_GET['post'] ) && isset( $_POST['post_ID'] ) && (int) $_GET['post'] !== (int) $_POST['post_ID'] ) { |
| 167 |
// Do nothing |
| 168 |
} elseif ( isset( $_GET['post'] ) ) { |
| 169 |
$post_id = (int) $_GET['post']; |
| 170 |
} elseif ( isset( $_POST['post_ID'] ) ) { |
| 171 |
$post_id = (int) $_POST['post_ID']; |
| 172 |
} |
| 173 |
if ( $post_id ) { |
| 174 |
$post = get_post( $post_id ); |
| 175 |
$typenow = $post->post_type; |
| 176 |
} |
| 177 |
} |
| 178 |
if ( $typenow === 'qr' ) { |
| 179 |
return true; |
| 180 |
} |
| 181 |
} |
| 182 |
return false; |
| 183 |
} |
| 184 |
|
| 185 |
} |