234 lines
7.6 KiB
PHP
234 lines
7.6 KiB
PHP
<?php
|
|
/**
|
|
* @package cimba
|
|
* @version 1.7.4
|
|
/*
|
|
Plugin Name: cimba
|
|
*/
|
|
|
|
// Include other files here
|
|
require_once plugin_dir_path(__FILE__) . 'includes/functions.php';
|
|
|
|
|
|
|
|
class NewClass
|
|
{
|
|
public $metadata_before;
|
|
public $metadata_after;
|
|
public $post_before;
|
|
public $post_after;
|
|
public $sp_event_before;
|
|
public $sp_event_after;
|
|
public $sp_event_data_before;
|
|
public $sp_event_data_after;
|
|
|
|
function print_r_pre($data, $msg = "")
|
|
{
|
|
echo "<pre>\"$msg\"\n" . print_r($data, true) . "</pre>";
|
|
}
|
|
function onelevel_array(array $array)
|
|
{
|
|
$return = [];
|
|
array_walk_recursive($array, function ($a) use (&$return) {
|
|
$return[] = $a;
|
|
});
|
|
return $return;
|
|
}
|
|
|
|
function get_sp_event_data(SP_Event $sp_event)
|
|
{
|
|
$data = [
|
|
"results" => $sp_event->results(),
|
|
"main_results" => $sp_event->main_results(),
|
|
"outcome" => $sp_event->outcome(),
|
|
"status" => $sp_event->status(),
|
|
"format" => $sp_event->format(),
|
|
"status" => get_post_meta($sp_event->ID, "sp_status", false),
|
|
"venue" => get_the_terms($sp_event->ID, "sp_venue", true),
|
|
"date" => $sp_event->post->post_date,
|
|
"teams" => get_post_meta($sp_event->ID, "sp_team", false),
|
|
];
|
|
return $data;
|
|
}
|
|
|
|
public function __construct()
|
|
{
|
|
add_action("post_updated", [$this, "datachangecheck"], 10, 3);
|
|
}
|
|
|
|
/*
|
|
* datachangecheck
|
|
*
|
|
* This function is called upon hook post_updated.
|
|
* The function checks if data for updated post has been changed,
|
|
* and saves a file when data has been changed
|
|
*
|
|
* @param string $post_id id of post affected
|
|
* @param WP_Post $post_after WP_Post after post has been updated
|
|
* @param WP_Post $post_before WP_Post before post has been updated
|
|
* @return N/A
|
|
*
|
|
*/
|
|
function datachangecheck($post_id, $post_after, $post_before)
|
|
{
|
|
$this->post_before = $post_before;
|
|
$this->post_after = $post_after;
|
|
//Cast postobjects into arrays, so comparision is possible with builtin-php functions
|
|
$spf = (array) $post_before;
|
|
$spa = (array) $post_after;
|
|
|
|
//These would differ every update. so remove them for possible comparision
|
|
unset($spf["post_modified"]);
|
|
unset($spf["post_modified_gmt"]);
|
|
unset($spa["post_modified"]);
|
|
unset($spa["post_modified_gmt"]);
|
|
|
|
//Check if any difference between arrays (if empty no difference)
|
|
//If not empty, save file that tells plugin that data has been changed
|
|
$ard = array_diff($spf, $spa);
|
|
if (!empty($ard)) {
|
|
$this->datahaschanged_save($post_before, $post_after);
|
|
print_r_pre($this->sp_event_data_before);
|
|
} else {
|
|
//No change of post native data, check if any metapost data has been changed
|
|
//Remove edit_last and edit_lock because they could differ without data being changed
|
|
$this->metadata_before = get_post_meta($post_id);
|
|
$this->sp_event_before = new SP_Event($post_before);
|
|
$this->sp_event_data_before = $this->get_sp_event_data(
|
|
$this->sp_event_before
|
|
);
|
|
|
|
unset($this->metadata_before["_edit_last"]);
|
|
unset($this->metadata_before["_edit_lock"]);
|
|
add_action(
|
|
"updated_post_meta",
|
|
[$this, "checkmetadata_after"],
|
|
10,
|
|
2
|
|
);
|
|
}
|
|
return;
|
|
}
|
|
|
|
/*
|
|
* checkmetadata_after
|
|
*
|
|
* This function is called upon hook updated_post_meta when data has been update, but no change in native post data
|
|
* has been made and saves a file when data has been changed
|
|
*
|
|
* @param string $post_id id of post affected
|
|
* @param WP_Post $post_after WP_Post after post has been updated
|
|
* @param WP_Post $post_before WP_Post before post has been updated
|
|
* @return N/A
|
|
*
|
|
*/
|
|
function checkmetadata_after($meta_id, $post_id)
|
|
{
|
|
//Because updated_post_meta is used, now we can grab the actual updated values
|
|
//Remove edit_last and edit_lock because they could differ without data being changed
|
|
$this->metadata_after = get_post_meta($post_id);
|
|
$this->sp_event_after = new SP_Event($this->post_after);
|
|
$this->sp_event_data_after = $this->get_sp_event_data(
|
|
$this->sp_event_after
|
|
);
|
|
unset($this->metadata_after["_edit_last"]);
|
|
unset($this->metadata_after["_edit_lock"]);
|
|
|
|
//Make one-level index arrays of metadata
|
|
//so array_diff works correctly down below
|
|
$arr_mdb = $this->onelevel_array($this->metadata_before);
|
|
$arr_mda = $this->onelevel_array($this->metadata_after);
|
|
|
|
//Compare array with metapost values before and after
|
|
//If not empty, save file that tells plugin that data has been changed
|
|
$ard_metadata = array_diff($arr_mdb, $arr_mda);
|
|
if (!empty($ard_metadata)) {
|
|
$this->datahaschanged_save(
|
|
$this->metadata_before,
|
|
$this->metadata_after,
|
|
true
|
|
);
|
|
$this->metadata_before = [];
|
|
$this->metadata_after = [];
|
|
$this->post_before = [];
|
|
$this->post_after = [];
|
|
}
|
|
return;
|
|
}
|
|
|
|
// echo get_the_post_thumbnail_url( get_the_ID(), 'medium' );
|
|
|
|
function datahaschanged_save(
|
|
$before = null,
|
|
$after = null,
|
|
$metadata_haschange = false
|
|
) {
|
|
_log("metadata has changed");
|
|
// $this->print_r_pre($this->sp_event_data_before);
|
|
// $this->print_r_pre($this->sp_event_data_after['results']);
|
|
// _log($this->sp_event_data_before->main_results);
|
|
if (
|
|
$this->sp_event_data_before["main_results"] !=
|
|
$this->sp_event_data_after["main_results"]
|
|
) {
|
|
_log("Score has changed!");
|
|
_log($this->sp_event_data_after["results"]);
|
|
$message = [];
|
|
foreach (
|
|
$this->sp_event_data_after["outcome"]
|
|
as $team_id => $outcome
|
|
) {
|
|
// print_r_pre($outcome);
|
|
$team = get_post($team_id);
|
|
$runs = $this->sp_event_data_after["results"][$team_id]["r"];
|
|
// print_r_pre($team);
|
|
$message[] = "$team->post_title: $runs ($outcome)";
|
|
}
|
|
_log(join(", ", $message));
|
|
}
|
|
}
|
|
}
|
|
|
|
$_ = new NewClass();
|
|
|
|
// get_the_terms($sp_event->ID, 'sp_venue', true)
|
|
// array(1)
|
|
// 0:
|
|
// WP_Term
|
|
// term_id:
|
|
// 5
|
|
// name:
|
|
// "Southwest Park"
|
|
// slug:
|
|
// "southwest"
|
|
// term_group:
|
|
// 0
|
|
// term_taxonomy_id:
|
|
// 5
|
|
// taxonomy:
|
|
// "sp_venue"
|
|
// description:
|
|
// "Southwest Park spans 6.9 acres and was incorporated into the Park Ridge Park District in 1956. This public space offers various amenities, including a playground, ball fields, a football field, a basketball court, as well as practical features like drinking fountains and a bike rack. The park serves as a recreational hub for the community, accommodating diverse interests and activities."
|
|
// parent:
|
|
// 0
|
|
// count:
|
|
// 32
|
|
// filter:
|
|
|
|
function test_inline()
|
|
{
|
|
$id = sp_array_value($_POST, "post_id");
|
|
$wp_post = get_post($id);
|
|
if ("sp_event" == $wp_post->post_type) {
|
|
$old_post = new SP_Event($id);
|
|
$old_results = $old_post->main_results();
|
|
$new_results = sp_array_value($_POST, "results");
|
|
echo "hello";
|
|
}
|
|
}
|
|
|
|
add_action("wp_ajax_sp-save-inline-results", "test_inline");
|
|
|
|
|
|
?>
|