true]); }); register_activation_hook(__FILE__, function() { $role = get_role('administrator'); if ($role) { $role->add_cap('sportspress_can_control_score'); } }); register_deactivation_hook(__FILE__, function() { $role = get_role('administrator'); if ($role) { $role->remove_cap('sportspress_can_control_score'); } }); // Add meta box for scoreboard timeline add_action('add_meta_boxes', function() { add_meta_box('scoreboard_timeline', 'Scoreboard Timeline', 'render_scoreboard_meta_box', 'sp_event'); }); function render_scoreboard_meta_box($post) { $json = get_post_meta($post->ID, '_scoreboard_timeline', true); $json = $json ? esc_textarea($json) : '{}'; echo ''; } // Save scoreboard meta add_action('save_post_sp_event', function($post_id) { if (isset($_POST['scoreboard_timeline_json'])) { update_post_meta($post_id, '_scoreboard_timeline', wp_unslash($_POST['scoreboard_timeline_json'])); } }); add_action('wp_enqueue_scripts', function() { if (is_singular('sp_event')) { wp_enqueue_script('scoreboard-js', plugins_url('/js/scoreboard.js', __FILE__), ['jquery'], null, true); wp_enqueue_script('scoreboard-controls-js', plugins_url('/js/scoreboard-controls.js', __FILE__), ['jquery'], null, true); wp_localize_script('scoreboard-js', 'scoreboardData', [ 'ajaxUrl' => admin_url('admin-ajax.php'), 'eventId' => get_the_ID(), 'canEdit' => current_user_can('sportspress_can_control_score'), 'timeline' => get_post_meta(get_the_ID(), '_scoreboard_timeline', true) ?: '{}', 'templatesUrl' => plugins_url('/html/', __FILE__) ]); } }); // AJAX endpoint for saving scoreboard add_action('wp_ajax_update_scoreboard', function() { if (!current_user_can('sportspress_can_control_score')) { wp_send_json_error('Unauthorized'); } $event_id = intval($_POST['event_id']); $timeline = wp_unslash($_POST['timeline']); update_post_meta($event_id, '_scoreboard_timeline', $timeline); wp_send_json_success(); }); add_shortcode('live_score', function($atts) { ob_start(); ?>
Loading live score...
'sp_event', 'posts_per_page' => 1, 'meta_query' => [ [ 'key' => 'is_live', 'value' => '1', 'compare' => '=' ] ] ]; $live_event = get_posts($args); if (!$live_event) { wp_send_json_error('No live event'); } $event = $live_event[0]; $timeline = get_post_meta($event->ID, '_scoreboard_timeline', true); $timeline_data = json_decode($timeline, true); // Simplified for display: extract latest score entry $latest = end($timeline_data); wp_send_json_success([ 'event_id' => $event->ID, 'event_title' => get_the_title($event), 'score' => $latest ?? ['home' => 0, 'away' => 0] ]); } add_action('wp_enqueue_scripts', function() { wp_enqueue_script('live-score-script', plugins_url('/js/live-score.js', __FILE__), ['jquery'], null, true); wp_localize_script('live-score-script', 'LiveScoreAjax', [ 'ajaxUrl' => admin_url('admin-ajax.php'), ]); }); // function scoreboard_add_to_event_page($content) { // if (get_post_type() === 'sp_event') { // ob_start(); // render_scoreboard_timeline(); // $timeline_html = ob_get_clean(); // return $timeline_html . $content; // Prepend it // } // return $content; // } // add_filter('the_content', 'scoreboard_add_to_event_page'); // // Display scoreboard on match report // add_action('sportspress_after_event_content', 'render_scoreboard_timeline'); // function render_scoreboard_timeline() { // echo '
Scoreboard timeline loaded.
'; // if (current_user_can('scoreboard_can_control')) { // echo '
User CAN control scoreboard.
'; // //include plugin_dir_path(__FILE__) . 'templates/scoreboard-controls.php'; // } else { // echo '
User CANNOT control scoreboard.
'; // } // //include plugin_dir_path(__FILE__) . 'templates/scoreboard-timeline.php'; // }