is_amp()) { echo "
"; } } /** * Enqueue assets */ public function add_assets() { // Include scripts on front end if (!is_admin() && !$this->is_amp()) { $js = $this->get_asset_vars('assets/js/onyx-poll.min.js'); wp_enqueue_script('acf-onyx-poll', $js->url, array(), $js->ver, false, true); if (!get_field('onyx_poll_css', 'options')) { $css = $this->get_asset_vars('assets/css/onyx-poll.min.css'); wp_enqueue_style('acf-onyx-poll', $css->url, array(), $css->ver); } wp_localize_script('acf-onyx-poll', 'onyxpoll', array( 'apiurl' => rest_url(), 'modaltime' => get_field('onyx_poll_modal_time', 'options'), 'labels' => array( 'vote' => __('Vote', 'acf-onyx-poll'), 'votes' => __('votes', 'acf-onyx-poll'), 'view' => __('Views result', 'acf-onyx-poll'), 'total' => __('Total votes', 'acf-onyx-poll'), 'success' => __('Vote was submitted successfully.', 'acf-onyx-poll'), 'error' => __('Poll vote error, try again.', 'acf-onyx-poll') ) ) ); } } /** * Some admin styles for better view */ public function admin_styles() { global $post_type; if ('onyxpolls' == $post_type) { $css = $this->get_asset_vars('assets/css/admin.min.css'); wp_enqueue_style('acf-onyx-poll-admin', $css->url, array(), $css->ver); } } /** * Get asset variables for enqueue * @param string $path required */ public function get_asset_vars($path = null) { if ($path) { $a = new stdClass(); $a->path = $path; $a->url = plugins_url($path, __FILE__); $a->ver = filemtime(plugin_dir_path(__FILE__) . $path); return $a; } return false; } /** * Extract shortcode * Just a simple shortcode method */ public function shortcode($atts) { extract(shortcode_atts(array( 'id' => '', 'class' => 'left', 'style' => '' ), $atts)); $poll = OnyxPolls::has_poll($id); if ($poll) { $html = "
"; } else { $html = "
". __('Invalid poll ID', 'acf-onyx-poll') ."
"; } return $html; } /** * initialize * * Sets up the Onyx Poll plugin. * * @param void * @return void */ public function initialize() { // Change ACF Local JSON save location to /acf folder inside this plugin // add_filter('acf/settings/save_json', function() { // return __DIR__ . '/acf'; // }); // Include the /acf folder in the places to look for ACF Local JSON files // add_filter('acf/settings/load_json', function($paths) { // $paths[] = __DIR__ . '/acf'; // return $paths; // }); // Load text domain language load_plugin_textdomain( 'acf-onyx-poll', false, dirname(plugin_basename(__FILE__)) . '/languages' ); // Verify if Advanced Custom Fields PRO is activated add_action('admin_init', function() { if (is_admin() && current_user_can('activate_plugins') && !is_plugin_active('advanced-custom-fields-pro/acf.php')) { add_action('admin_notices', function() { $notice = __('Sorry, but ACF Onyx Poll requires that ACF PRO is installed and active.', 'acf-onyx-poll'); echo "

$notice

"; }); deactivate_plugins(plugin_basename(__FILE__)); if (isset($_GET['activate'])) { unset($_GET['activate']); } } }); // Load ACF fields add_action('acf/init', function() { require(__DIR__ . '/acf/fields.php'); }); if(is_admin()) { // Create Poll Post Type require_once(__DIR__ . '/admin/poll-type.php'); } // Load widget require_once(__DIR__ . '/admin/poll-widget.php'); add_action('widgets_init', function() { register_widget('OnyxPollsWidget'); }); // Load Helper Methods require_once(__DIR__ . '/classes/poll-helpers.php'); // Create REST API for Onyx Poll require_once(__DIR__ . '/api/poll-api.php'); // Add footer html elements add_action('wp_footer', array($this, 'add_footer_elements'), 1); // Enqueue scripts and styles add_action('wp_footer', array($this, 'add_assets')); add_action('admin_head', array($this, 'admin_styles')); // Add onyx poll shortcode add_shortcode("onyx-poll", array($this, 'shortcode')); // Define cron event for expired polls add_action('onyx-poll-cron', array($this, 'cron_job')); register_activation_hook(__FILE__, function() { wp_schedule_event(time(), 'hourly', 'onyx-poll-cron'); }); register_deactivation_hook(__FILE__, function() { wp_clear_scheduled_hook('onyx-poll-cron'); }); } public function cron_job() { OnyxPolls::expire_polls(); } public function is_amp() { return function_exists('is_amp_endpoint') && is_amp_endpoint(); } } /** * Instantiate Onyx Poll */ $onyx_poll = new OnyxPollsInit(); $onyx_poll->initialize(); endif; // class_exists check ?>