From b1ed7ee6c9aaa32ff2053cf19ae9c665e803ab0d Mon Sep 17 00:00:00 2001 From: Anthony Correa Date: Sun, 24 Dec 2023 09:27:07 -0600 Subject: [PATCH] initial commit --- Gruntfile.js | 56 ++++++++++ bin/install-wp-tests.sh | 181 +++++++++++++++++++++++++++++++ cimba.php | 233 ++++++++++++++++++++++++++++++++++++++++ distignore | 43 ++++++++ package.json | 17 +++ phpunit.xml.dist | 16 +++ tests/bootstrap.php | 38 +++++++ tests/test-sample.php | 20 ++++ 8 files changed, 604 insertions(+) create mode 100644 Gruntfile.js create mode 100755 bin/install-wp-tests.sh create mode 100644 cimba.php create mode 100644 distignore create mode 100644 package.json create mode 100644 phpunit.xml.dist create mode 100644 tests/bootstrap.php create mode 100644 tests/test-sample.php diff --git a/Gruntfile.js b/Gruntfile.js new file mode 100644 index 0000000..a367708 --- /dev/null +++ b/Gruntfile.js @@ -0,0 +1,56 @@ +module.exports = function( grunt ) { + + 'use strict'; + + // Project configuration + grunt.initConfig( { + + pkg: grunt.file.readJSON( 'package.json' ), + + addtextdomain: { + options: { + textdomain: 'test-plugin', + }, + update_all_domains: { + options: { + updateDomains: true + }, + src: [ '*.php', '**/*.php', '!\.git/**/*', '!bin/**/*', '!node_modules/**/*', '!tests/**/*' ] + } + }, + + wp_readme_to_markdown: { + your_target: { + files: { + 'README.md': 'readme.txt' + } + }, + }, + + makepot: { + target: { + options: { + domainPath: '/languages', + exclude: [ '\.git/*', 'bin/*', 'node_modules/*', 'tests/*' ], + mainFile: 'test-plugin.php', + potFilename: 'test-plugin.pot', + potHeaders: { + poedit: true, + 'x-poedit-keywordslist': true + }, + type: 'wp-plugin', + updateTimestamp: true + } + } + }, + } ); + + grunt.loadNpmTasks( 'grunt-wp-i18n' ); + grunt.loadNpmTasks( 'grunt-wp-readme-to-markdown' ); + grunt.registerTask( 'default', [ 'i18n','readme' ] ); + grunt.registerTask( 'i18n', ['addtextdomain', 'makepot'] ); + grunt.registerTask( 'readme', ['wp_readme_to_markdown'] ); + + grunt.util.linefeed = '\n'; + +}; diff --git a/bin/install-wp-tests.sh b/bin/install-wp-tests.sh new file mode 100755 index 0000000..c6f53dc --- /dev/null +++ b/bin/install-wp-tests.sh @@ -0,0 +1,181 @@ +#!/usr/bin/env bash + +if [ $# -lt 3 ]; then + echo "usage: $0 [db-host] [wp-version] [skip-database-creation]" + exit 1 +fi + +DB_NAME=$1 +DB_USER=$2 +DB_PASS=$3 +DB_HOST=${4-localhost} +WP_VERSION=${5-latest} +SKIP_DB_CREATE=${6-false} + +TMPDIR=${TMPDIR-/tmp} +TMPDIR=$(echo $TMPDIR | sed -e "s/\/$//") +WP_TESTS_DIR=${WP_TESTS_DIR-$TMPDIR/wordpress-tests-lib} +WP_CORE_DIR=${WP_CORE_DIR-$TMPDIR/wordpress} + +download() { + if [ `which curl` ]; then + curl -s "$1" > "$2"; + elif [ `which wget` ]; then + wget -nv -O "$2" "$1" + fi +} + +if [[ $WP_VERSION =~ ^[0-9]+\.[0-9]+\-(beta|RC)[0-9]+$ ]]; then + WP_BRANCH=${WP_VERSION%\-*} + WP_TESTS_TAG="branches/$WP_BRANCH" + +elif [[ $WP_VERSION =~ ^[0-9]+\.[0-9]+$ ]]; then + WP_TESTS_TAG="branches/$WP_VERSION" +elif [[ $WP_VERSION =~ [0-9]+\.[0-9]+\.[0-9]+ ]]; then + if [[ $WP_VERSION =~ [0-9]+\.[0-9]+\.[0] ]]; then + # version x.x.0 means the first release of the major version, so strip off the .0 and download version x.x + WP_TESTS_TAG="tags/${WP_VERSION%??}" + else + WP_TESTS_TAG="tags/$WP_VERSION" + fi +elif [[ $WP_VERSION == 'nightly' || $WP_VERSION == 'trunk' ]]; then + WP_TESTS_TAG="trunk" +else + # http serves a single offer, whereas https serves multiple. we only want one + download http://api.wordpress.org/core/version-check/1.7/ /tmp/wp-latest.json + grep '[0-9]+\.[0-9]+(\.[0-9]+)?' /tmp/wp-latest.json + LATEST_VERSION=$(grep -o '"version":"[^"]*' /tmp/wp-latest.json | sed 's/"version":"//') + if [[ -z "$LATEST_VERSION" ]]; then + echo "Latest WordPress version could not be found" + exit 1 + fi + WP_TESTS_TAG="tags/$LATEST_VERSION" +fi +set -ex + +install_wp() { + + if [ -d $WP_CORE_DIR ]; then + return; + fi + + mkdir -p $WP_CORE_DIR + + if [[ $WP_VERSION == 'nightly' || $WP_VERSION == 'trunk' ]]; then + mkdir -p $TMPDIR/wordpress-trunk + rm -rf $TMPDIR/wordpress-trunk/* + svn export --quiet https://core.svn.wordpress.org/trunk $TMPDIR/wordpress-trunk/wordpress + mv $TMPDIR/wordpress-trunk/wordpress/* $WP_CORE_DIR + else + if [ $WP_VERSION == 'latest' ]; then + local ARCHIVE_NAME='latest' + elif [[ $WP_VERSION =~ [0-9]+\.[0-9]+ ]]; then + # https serves multiple offers, whereas http serves single. + download https://api.wordpress.org/core/version-check/1.7/ $TMPDIR/wp-latest.json + if [[ $WP_VERSION =~ [0-9]+\.[0-9]+\.[0] ]]; then + # version x.x.0 means the first release of the major version, so strip off the .0 and download version x.x + LATEST_VERSION=${WP_VERSION%??} + else + # otherwise, scan the releases and get the most up to date minor version of the major release + local VERSION_ESCAPED=`echo $WP_VERSION | sed 's/\./\\\\./g'` + LATEST_VERSION=$(grep -o '"version":"'$VERSION_ESCAPED'[^"]*' $TMPDIR/wp-latest.json | sed 's/"version":"//' | head -1) + fi + if [[ -z "$LATEST_VERSION" ]]; then + local ARCHIVE_NAME="wordpress-$WP_VERSION" + else + local ARCHIVE_NAME="wordpress-$LATEST_VERSION" + fi + else + local ARCHIVE_NAME="wordpress-$WP_VERSION" + fi + download https://wordpress.org/${ARCHIVE_NAME}.tar.gz $TMPDIR/wordpress.tar.gz + tar --strip-components=1 -zxmf $TMPDIR/wordpress.tar.gz -C $WP_CORE_DIR + fi + + download https://raw.githubusercontent.com/markoheijnen/wp-mysqli/master/db.php $WP_CORE_DIR/wp-content/db.php +} + +install_test_suite() { + # portable in-place argument for both GNU sed and Mac OSX sed + if [[ $(uname -s) == 'Darwin' ]]; then + local ioption='-i.bak' + else + local ioption='-i' + fi + + # set up testing suite if it doesn't yet exist + if [ ! -d $WP_TESTS_DIR ]; then + # set up testing suite + mkdir -p $WP_TESTS_DIR + rm -rf $WP_TESTS_DIR/{includes,data} + svn export --quiet --ignore-externals https://develop.svn.wordpress.org/${WP_TESTS_TAG}/tests/phpunit/includes/ $WP_TESTS_DIR/includes + svn export --quiet --ignore-externals https://develop.svn.wordpress.org/${WP_TESTS_TAG}/tests/phpunit/data/ $WP_TESTS_DIR/data + fi + + if [ ! -f wp-tests-config.php ]; then + download https://develop.svn.wordpress.org/${WP_TESTS_TAG}/wp-tests-config-sample.php "$WP_TESTS_DIR"/wp-tests-config.php + # remove all forward slashes in the end + WP_CORE_DIR=$(echo $WP_CORE_DIR | sed "s:/\+$::") + sed $ioption "s:dirname( __FILE__ ) . '/src/':'$WP_CORE_DIR/':" "$WP_TESTS_DIR"/wp-tests-config.php + sed $ioption "s:__DIR__ . '/src/':'$WP_CORE_DIR/':" "$WP_TESTS_DIR"/wp-tests-config.php + sed $ioption "s/youremptytestdbnamehere/$DB_NAME/" "$WP_TESTS_DIR"/wp-tests-config.php + sed $ioption "s/yourusernamehere/$DB_USER/" "$WP_TESTS_DIR"/wp-tests-config.php + sed $ioption "s/yourpasswordhere/$DB_PASS/" "$WP_TESTS_DIR"/wp-tests-config.php + sed $ioption "s|localhost|${DB_HOST}|" "$WP_TESTS_DIR"/wp-tests-config.php + fi + +} + +recreate_db() { + shopt -s nocasematch + if [[ $1 =~ ^(y|yes)$ ]] + then + mysqladmin drop $DB_NAME -f --user="$DB_USER" --password="$DB_PASS"$EXTRA + create_db + echo "Recreated the database ($DB_NAME)." + else + echo "Leaving the existing database ($DB_NAME) in place." + fi + shopt -u nocasematch +} + +create_db() { + mysqladmin create $DB_NAME --user="$DB_USER" --password="$DB_PASS"$EXTRA +} + +install_db() { + + if [ ${SKIP_DB_CREATE} = "true" ]; then + return 0 + fi + + # parse DB_HOST for port or socket references + local PARTS=(${DB_HOST//\:/ }) + local DB_HOSTNAME=${PARTS[0]}; + local DB_SOCK_OR_PORT=${PARTS[1]}; + local EXTRA="" + + if ! [ -z $DB_HOSTNAME ] ; then + if [ $(echo $DB_SOCK_OR_PORT | grep -e '^[0-9]\{1,\}$') ]; then + EXTRA=" --host=$DB_HOSTNAME --port=$DB_SOCK_OR_PORT --protocol=tcp" + elif ! [ -z $DB_SOCK_OR_PORT ] ; then + EXTRA=" --socket=$DB_SOCK_OR_PORT" + elif ! [ -z $DB_HOSTNAME ] ; then + EXTRA=" --host=$DB_HOSTNAME --protocol=tcp" + fi + fi + + # create database + if [ $(mysql --user="$DB_USER" --password="$DB_PASS"$EXTRA --execute='show databases;' | grep ^$DB_NAME$) ] + then + echo "Reinstalling will delete the existing test database ($DB_NAME)" + read -p 'Are you sure you want to proceed? [y/N]: ' DELETE_EXISTING_DB + recreate_db $DELETE_EXISTING_DB + else + create_db + fi +} + +install_wp +install_test_suite +install_db diff --git a/cimba.php b/cimba.php new file mode 100644 index 0000000..226f501 --- /dev/null +++ b/cimba.php @@ -0,0 +1,233 @@ +\"$msg\"\n" . print_r($data, true) . ""; + } + 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"); + + +?> diff --git a/distignore b/distignore new file mode 100644 index 0000000..af58f8c --- /dev/null +++ b/distignore @@ -0,0 +1,43 @@ +# A set of files you probably don't want in your WordPress.org distribution +.babelrc +.deployignore +.distignore +.editorconfig +.eslintignore +.eslintrc +.git +.gitignore +.github +.gitlab-ci.yml +.travis.yml +.DS_Store +.*~ +Thumbs.db +behat.yml +bitbucket-pipelines.yml +bin +.circleci/config.yml +composer.json +composer.lock +dependencies.yml +Gruntfile.js +package.json +package-lock.json +phpunit.xml +phpunit.xml.dist +multisite.xml +multisite.xml.dist +.phpcs.xml +phpcs.xml +.phpcs.xml.dist +phpcs.xml.dist +README.md +webpack.config.js +wp-cli.local.yml +yarn.lock +tests +vendor +node_modules +*.sql +*.tar.gz +*.zip diff --git a/package.json b/package.json new file mode 100644 index 0000000..9a407d4 --- /dev/null +++ b/package.json @@ -0,0 +1,17 @@ + +{ + "name": "cimba", + "version": "0.1.0", + "main": "Gruntfile.js", + "author": "Anthony Correa", + "scripts" : { + "start" : "grunt default" , + "readme" : "grunt readme", + "i18n" : "grunt i18n" + }, + "devDependencies": { + "grunt": "~0.4.5", + "grunt-wp-i18n": "~0.5.0", + "grunt-wp-readme-to-markdown": "~1.0.0" + } +} diff --git a/phpunit.xml.dist b/phpunit.xml.dist new file mode 100644 index 0000000..50421ec --- /dev/null +++ b/phpunit.xml.dist @@ -0,0 +1,16 @@ + + + + + ./tests/ + ./tests/test-sample.php + + + diff --git a/tests/bootstrap.php b/tests/bootstrap.php new file mode 100644 index 0000000..ad1ea44 --- /dev/null +++ b/tests/bootstrap.php @@ -0,0 +1,38 @@ +assertTrue( true ); + } +}