name: Test and Release

on:
  pull_request:
    paths-ignore:
      - "**.md"
  push:
    branches: [master]
    tags: ["*"]
    paths-ignore:
      - "**.md"

jobs:
  test:
    runs-on: ubuntu-latest
    services:
      mysql:
        image: mysql:8.0
        env:
          MYSQL_ALLOW_EMPTY_PASSWORD: true
          MYSQL_ROOT_PASSWORD: root
          MYSQL_DATABASE: wordpress_test
        ports:
          - 3306:3306
        options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3
    strategy:
      fail-fast: false
      matrix:
        php: ['8.4', '8.0']
        wp: ['trunk', '6.9', '6.7']
        multisite: [false, true]

    name: PHP ${{ matrix.php }} / WP ${{ matrix.wp }}${{ matrix.multisite && ' (multisite)' || '' }} Test
    steps:
      - uses: actions/checkout@v4

      - name: Setup PHP
        uses: shivammathur/setup-php@v2
        with:
          php-version: ${{ matrix.php }}
          extensions: mbstring, intl, mysqli
          coverage: none

      - name: Cache composer packages
        uses: actions/cache@v4
        with:
          path: vendor
          key: composer-${{ matrix.php }}-${{ hashFiles('composer.json') }}
          restore-keys: composer-${{ matrix.php }}-

      - name: Composer install
        run: composer update --optimize-autoloader --prefer-dist

      - name: Get cache key suffix
        id: cache-date
        run: echo "week=$(date +'%Y-W%V')" >> $GITHUB_OUTPUT

      - name: Cache WordPress test library
        uses: actions/cache@v4
        with:
          path: |
            /tmp/wordpress
            /tmp/wordpress-tests-lib
          key: wp-test-lib-${{ matrix.wp }}-${{ matrix.wp == 'trunk' && steps.cache-date.outputs.week || 'stable' }}

      - name: Install subversion
        run: |
          if ! command -v svn > /dev/null 2>&1 || [ ! -f /tmp/wordpress-tests-lib/includes/functions.php ]; then
            sudo apt-get update && sudo apt-get install -y subversion
          fi

      - name: Install WP Tests
        run: bash bin/install-wp-tests.sh wordpress_test root root 127.0.0.1 ${{ matrix.wp }} true

      - name: phpunit tests
        run: composer test
        env:
          WP_MULTISITE: ${{ matrix.multisite && '1' || '' }}

  lint:
    runs-on: ubuntu-latest
    name: PHP lint
    steps:
      - uses: actions/checkout@v4

      - name: Setup PHP
        uses: shivammathur/setup-php@v2
        with:
          php-version: 8.4
          extensions: mbstring, intl
          coverage: none

      - name: Cache composer packages
        uses: actions/cache@v4
        with:
          path: vendor
          key: composer-lint-${{ hashFiles('composer.json') }}
          restore-keys: composer-lint-

      - name: Composer install
        run: composer install --optimize-autoloader --prefer-dist

      - name: phpcs tests
        run: composer phpcs

  release:
    name: Release
    runs-on: ubuntu-latest
    needs: [test, lint]
    steps:
      - uses: actions/checkout@v4
      - name: Build
        if: contains(github.ref, 'tags/')
        run: bash ./bin/build.sh
      - name: WordPress Plugin Deploy
        if: contains(github.ref, 'tags/')
        uses: 10up/action-wordpress-plugin-deploy@2.3.0
        env:
          SVN_PASSWORD: ${{ secrets.SVN_PASSWORD }}
          SVN_USERNAME: ${{ secrets.SVN_USERNAME }}
          SLUG: custom-post-type-permalinks
