• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

ideasonpurpose / wp-data-model / #16

pending completion
#16

push

php-coveralls

joemaller
update lockfile for GitHub Actions run

211 of 234 relevant lines covered (90.17%)

2.73 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/Rename.php
1
<?php
2

3
namespace IdeasOnPurpose\WP;
4

5
use IdeasOnPurpose\DataModel;
6

7
abstract class Rename
8
{
9
    /**
10
     * __callStatic is a magic method which renames the invoked thing using a string in the first
11
     * index of $args. Built-in types must be referenced by a known short name, eg. 'post', 'page',
12
     * 'category', 'tag', etc. Tags can be either 'tag' or 'post_tag'.
13
     *
14
     * A simple example call which renames 'Pages' to 'Chapters' looks like this:
15
     *
16
     *     WP\Rename::page('chapter');
17
     *
18
     * Set the second argument to false to disable singular/plural inflection. Renaming 'Categories'
19
     * to 'Class' would look like this:
20
     *
21
     *     WP\Rename::category('class', false);
22
     *
23
     * Individual labels can be overridden by sending an array in the third argument:
24
     *
25
     *     WP\Rename::tag('flavors', true, ['popular_items' => 'Most delicious flavors']);
26
     *
27
     * Unknown types will be ignored. Overrides should be an associative array and can include some
28
     * or all available labels.
29
     *
30
     * @param String $name - The name of the invoked magic method, use this as the thing to rename
31
     * @param Array $args - An array of additional args. Breaks down like this:
32
     *                      [ $labelBase, $inflect, $overrides ]
33
     */
34
    public static function __callStatic($name, $args)
35
    {
36
        $labelBase = $args[0] ?? null;
×
37
        $inflect = !!($args[1] ?? true);
×
38
        $overrides = $args[2] ?? [];
×
39

40
        if (!$labelBase) {
×
41
            return new Error('A new name must be provided when renaming.');
×
42
        }
43
        if (!is_array($overrides)) {
×
44
            return new Error('Overrides must be an array.');
×
45
        }
46

47
        /**
48
         * This is a Special case for "Tags" since WordPress stores them as "post_tag". We just re-map it.
49
         */
50
        $name = strtolower($name) === 'tag' ? 'post_tag' : $name;
×
51

52
        self::update($name, $labelBase, $inflect, $overrides);
×
53
    }
54

55
    /**
56
     * Generates a set of type-specific labels based on $labelBase, then assigns those labels
57
     * to the Post_type or Taxonomy $object
58
     */
59
    protected static function update($object, $labelBase, $inflect = true, $overrides = [])
60
    {
61
        global $wp_post_types, $wp_taxonomies;
×
62

63
        /**
64
         * Assign new labels to native objects
65
         */
66
        if (array_key_exists($object, $wp_post_types)) {
×
67
            $wp_post_types[$object]->labels = DataModel::postTypeLabels(
×
68
                $labelBase,
×
69
                $inflect,
×
70
                $overrides
×
71
            );
×
72
        } elseif (array_key_exists($object, $wp_taxonomies)) {
×
73
            $wp_taxonomies[$object]->labels = DataModel::taxonomyLabels(
×
74
                $labelBase,
×
75
                $inflect,
×
76
                $overrides
×
77
            );
×
78
        } else {
79
            new Error("'{$object}' is not a known Post_type or Taxonomy. Unable to rename.");
×
80
        }
81
    }
82
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc