Plugin Directory

Changeset 3216597


Ignore:
Timestamp:
01/03/2025 06:25:36 PM (15 months ago)
Author:
bhubbard
Message:

Composer Update

File:
1 edited

Legend:

Unmodified
Added
Removed
  • foreign-keys-pro/trunk/foreign-keys-pro.php

    r2416325 r3216597  
    66 */
    77
    8 /*
     8/**
    99 * Plugin Name: Foreign Keys Pro
    1010 * Plugin URI: https://github.com/bhubbard/foreign-keys-pro
    1111 * Description: A plugin to setup Foreign Keys in the WordPress Database.
    1212 * Text Domain: foreign-keys-pro
     13 * Tags: foreign keys, database, mysql, innodb
    1314 * Author: Hubbard Labs
    1415 * Author URI: https://brandonhubbard.com
    1516 * Contributors: bhubbard
     17 * Requires at least: 4.7
     18 * Tested up to: 6.7.1
     19 * Stable tag: 1.0.1
    1620 * License: GPLv3 or later
    1721 * License URI: https://www.gnu.org/licenses/gpl-3.0.en.html
    18  * Version: 1.0.0
     22 * Version: 1.0.1
    1923 */
    2024
     
    2832
    2933        /**
    30          * [__construct description]
     34         * Constructor.
    3135         */
    3236        public function __construct() {
     
    3438        }
    3539
    36 
    37 
    38         /**
    39          * [create_foreign_keys description]
    40          *
    41          * @return $results Results of Query.
     40        /**
     41         * Create foreign keys.
     42         *
     43         * @return array|WP_Error Results of query or WP_Error on failure.
    4244         */
    4345        public function create_foreign_keys() {
    4446
    45             if( true === $this->check_for_myisam() ) {
     47            if ( true === $this->check_for_myisam() ) {
    4648                return new WP_Error( 'db-myisam', __( 'Please update your database to InnoDB.', 'foreign-keys-pro' ) );
    4749            }
    4850
    49                 $results                       = array();
    50                 $results['usermeta']           = $this->foreign_key_usermeta();
    51                 $results['postmeta']           = $this->foreign_key_postmeta();
    52                 $results['termmeta']           = $this->foregin_key_termmeta();
    53                 $results['term_relationships'] = $this->foregin_key_term_relationships();
    54                 $results['term_taxonomy']      = $this->foregin_key_term_taxonomy();
    55                 $results['posts']              = $this->foregin_key_posts();
    56             //  $results['posts']              = $this->foreign_key_post_parent();
    57                 $results['comments']           = $this->foreign_keys_comments();
    58                 $results['commentmeta']        = $this->foreign_key_commentmeta();
    59             //  $results['comments_user']      = $this->foreign_keys_comments_user();
    60 
    61             //  var_dump( $results );
    62 
    63                 return $results;
    64         }
    65 
    66         /**
    67          * [check_for_myisam description]
    68          *
    69          * @return $results Results of Query.
     51            $results                       = array();
     52            $results['usermeta']           = $this->foreign_key_usermeta();
     53            $results['postmeta']           = $this->foreign_key_postmeta();
     54            $results['termmeta']           = $this->foregin_key_termmeta();
     55            $results['term_relationships'] = $this->foregin_key_term_relationships();
     56            $results['term_taxonomy']      = $this->foregin_key_term_taxonomy();
     57            $results['posts']              = $this->foregin_key_posts();
     58            // $results['posts']              = $this->foreign_key_post_parent();
     59            $results['comments']    = $this->foreign_keys_comments();
     60            $results['commentmeta'] = $this->foreign_key_commentmeta();
     61            // $results['comments_user']      = $this->foreign_keys_comments_user();
     62
     63            // var_dump( $results );
     64
     65            return $results;
     66        }
     67
     68        /**
     69         * Check for MyISAM tables.
     70         *
     71         * @return bool True if MyISAM tables exist, false otherwise.
    7072         */
    7173        public function check_for_myisam() {
     
    8082                return true;
    8183            }
    82 
    8384        }
    8485
    8586        /**
    8687         * Check for Current Constraints.
     88         *
    8789         * @return array Array of current constraint names.
    8890         */
     
    9193            global $wpdb;
    9294
    93             $current_constraints = $wpdb->get_results("SELECT CONSTRAINT_NAME FROM information_schema.TABLE_CONSTRAINTS WHERE information_schema.TABLE_CONSTRAINTS.CONSTRAINT_TYPE = 'FOREIGN KEY' ");
    94             $constraint_names = array();
    95 
    96             foreach( $current_constraints as $constraint ) {
     95            $current_constraints = $wpdb->get_results( "SELECT CONSTRAINT_NAME FROM information_schema.TABLE_CONSTRAINTS WHERE information_schema.TABLE_CONSTRAINTS.CONSTRAINT_TYPE = 'FOREIGN KEY' " );
     96            $constraint_names    = array();
     97
     98            foreach ( $current_constraints as $constraint ) {
    9799                $constraint_names[] = $constraint->CONSTRAINT_NAME;
    98100            }
    99101            return $constraint_names ?? array();
    100 
    101         }
    102 
    103         // TODO: Check MySQL Version.
     102        }
     103
     104        /**
     105         * Get MySQL Version.
     106         *
     107         * @return string MySQL version.
     108         */
    104109        public function get_mysql_version() {
    105110
    106111            global $wpdb;
    107112
    108             $version = $wpdb->get_results( "SELECT VERSION() as version" ) ?? false;
     113            $version = $wpdb->get_results( 'SELECT VERSION() as version' ) ?? false;
    109114
    110115            return $version[0]->version;
    111 
    112         }
    113 
    114         /**
    115          * [foreign_key_usermeta description]
    116          *
    117          * @param  array $args Arguments.
    118          * @return $results Results of Query.
    119          */
    120         public function foreign_key_usermeta( $args = array() ) {
    121 
    122             global $wpdb;
    123 
    124             $current_constraints = $this->get_current_constraints();
    125 
    126             if( in_array( 'user_id', $current_constraints ) ) {
     116        }
     117
     118        /**
     119         * Add foreign key to usermeta table.
     120         *
     121         * @return int|string Results of query or message.
     122         */
     123        public function foreign_key_usermeta() {
     124
     125            global $wpdb;
     126
     127            $current_constraints = $this->get_current_constraints();
     128
     129            if ( in_array( 'user_id', $current_constraints, true ) ) {
    127130                return __( 'User ID constraint already exists.', 'foreign-keys-pro' );
    128131            }
    129132
    130             $query   = "ALTER TABLE $wpdb->usermeta ADD CONSTRAINT `user_id` FOREIGN KEY (`user_id`) REFERENCES $wpdb->users (`ID`) ON DELETE CASCADE ON UPDATE CASCADE";
    131             $results = $wpdb->query( $query ) ?? false;
    132 
    133             return $results;
    134 
    135         }
    136 
    137 
    138         /**
    139          * [foreign_key_postmeta description]
    140          *
    141          * @param  array $args Arguments.
    142          * @return $results Results of Query.
    143          */
    144         public function foreign_key_postmeta( $args = array() ) {
    145 
    146             global $wpdb;
    147 
    148             $current_constraints = $this->get_current_constraints();
    149 
    150             if( in_array( 'post_id', $current_constraints ) ) {
     133            $query   = $wpdb->prepare(
     134                "ALTER TABLE $wpdb->usermeta ADD CONSTRAINT `user_id` FOREIGN KEY (`user_id`) REFERENCES $wpdb->users (`ID`) ON DELETE CASCADE ON UPDATE CASCADE"
     135            );
     136            $results = $wpdb->query( $query ) ?? false;
     137
     138            return $results;
     139        }
     140
     141
     142        /**
     143         * Add foreign key to postmeta table.
     144         *
     145         * @return int|string Results of query or message.
     146         */
     147        public function foreign_key_postmeta() {
     148
     149            global $wpdb;
     150
     151            $current_constraints = $this->get_current_constraints();
     152
     153            if ( in_array( 'post_id', $current_constraints, true ) ) {
    151154                return __( 'Post ID constraint already exists.', 'foreign-keys-pro' );
    152155            }
    153156
    154             $query   = "ALTER TABLE $wpdb->postmeta ADD CONSTRAINT `post_id` FOREIGN KEY (`post_id`) REFERENCES $wpdb->posts (`ID`) ON DELETE CASCADE ON UPDATE CASCADE";
    155             $results = $wpdb->query( $query ) ?? false;
    156 
    157             return $results;
    158 
    159         }
    160 
    161         /**
    162          * [foreign_key_post_parent description]
    163          * @param  array  $args [description]
    164          * @return [type]       [description]
    165          */
    166         public function foreign_key_post_parent( $args = array() ) {
    167 
    168             global $wpdb;
    169 
    170             $current_constraints = $this->get_current_constraints();
    171 
    172             if( in_array( 'post_parent', $current_constraints ) ) {
     157            $query   = $wpdb->prepare(
     158                "ALTER TABLE $wpdb->postmeta ADD CONSTRAINT `post_id` FOREIGN KEY (`post_id`) REFERENCES $wpdb->posts (`ID`) ON DELETE CASCADE ON UPDATE CASCADE"
     159            );
     160            $results = $wpdb->query( $query ) ?? false;
     161
     162            return $results;
     163        }
     164
     165        /**
     166         * Add foreign key to posts table for parent post.
     167         *
     168         * @return int|string Results of query or message.
     169         */
     170        public function foreign_key_post_parent() {
     171
     172            global $wpdb;
     173
     174            $current_constraints = $this->get_current_constraints();
     175
     176            if ( in_array( 'post_parent', $current_constraints, true ) ) {
    173177                return __( 'Post Parent ID constraint already exists.', 'foreign-keys-pro' );
    174178            }
    175179
    176             $query   = "ALTER TABLE $wpdb->posts ADD CONSTRAINT `parent_id` FOREIGN KEY (`post_parent`) REFERENCES $wpdb->posts (`ID`) ON DELETE CASCADE ON UPDATE CASCADE";
    177             $results = $wpdb->query( $query ) ?? false;
    178 
    179             return $results;
    180 
    181         }
    182 
    183         /**
    184          * [foregin_key_termmeta description]
    185          *
    186          * @param  array $args Arguments.
    187          * @return $results Results of Query.
    188          */
    189         public function foregin_key_termmeta( $args = array() ) {
    190 
    191             global $wpdb;
    192 
    193             $current_constraints = $this->get_current_constraints();
    194 
    195             if( in_array( 'term_id', $current_constraints ) ) {
     180            $query   = $wpdb->prepare(
     181                "ALTER TABLE $wpdb->posts ADD CONSTRAINT `parent_id` FOREIGN KEY (`post_parent`) REFERENCES $wpdb->posts (`ID`) ON DELETE CASCADE ON UPDATE CASCADE"
     182            );
     183            $results = $wpdb->query( $query ) ?? false;
     184
     185            return $results;
     186        }
     187
     188        /**
     189         * Add foreign key to termmeta table.
     190         *
     191         * @return int|string Results of query or message.
     192         */
     193        public function foregin_key_termmeta() {
     194
     195            global $wpdb;
     196
     197            $current_constraints = $this->get_current_constraints();
     198
     199            if ( in_array( 'term_id', $current_constraints, true ) ) {
    196200                return __( 'Term ID constraint already exists.', 'foreign-keys-pro' );
    197201            }
    198202
    199             $query   = "ALTER TABLE $wpdb->termmeta ADD CONSTRAINT `term_id` FOREIGN KEY (`term_id`) REFERENCES $wpdb->terms (`term_id`) ON DELETE CASCADE ON UPDATE CASCADE";
    200             $results = $wpdb->query( $query ) ?? false;
    201 
    202             return $results;
    203 
    204         }
    205 
    206         /**
    207          * [foregin_key_term_relationships description]
    208          *
    209          * @param  array $args Arguments.
    210          * @return $results Results of Query.
    211          */
    212         public function foregin_key_term_relationships( $args = array() ) {
    213 
    214             global $wpdb;
    215 
    216             $current_constraints = $this->get_current_constraints();
    217 
    218             if( in_array( 'term_taxonomy_id', $current_constraints ) ) {
     203            $query   = $wpdb->prepare(
     204                "ALTER TABLE $wpdb->termmeta ADD CONSTRAINT `term_id` FOREIGN KEY (`term_id`) REFERENCES $wpdb->terms (`term_id`) ON DELETE CASCADE ON UPDATE CASCADE"
     205            );
     206            $results = $wpdb->query( $query ) ?? false;
     207
     208            return $results;
     209        }
     210
     211        /**
     212         * Add foreign key to term_relationships table.
     213         *
     214         * @return int|string Results of query or message.
     215         */
     216        public function foregin_key_term_relationships() {
     217
     218            global $wpdb;
     219
     220            $current_constraints = $this->get_current_constraints();
     221
     222            if ( in_array( 'term_taxonomy_id', $current_constraints, true ) ) {
    219223                return __( 'Term Taxonomy ID constraint already exists.', 'foreign-keys-pro' );
    220224            }
    221225
    222             $query   = "ALTER TABLE $wpdb->term_relationships ADD CONSTRAINT `term_taxonomy_id` FOREIGN KEY (`term_taxonomy_id`) REFERENCES $wpdb->term_taxonomy (`term_taxonomy_id`) ON DELETE CASCADE ON UPDATE CASCADE";
    223             $results = $wpdb->query( $query ) ?? false;
    224 
    225             return $results;
    226 
    227         }
    228 
    229         /**
    230          * [foregin_key_term_taxonomy description]
    231          *
    232          * @param  array $args Arguments.
    233          * @return $results Results of Query.
    234          */
    235         public function foregin_key_term_taxonomy( $args = array() ) {
    236 
    237             global $wpdb;
    238 
    239             $current_constraints = $this->get_current_constraints();
    240 
    241             if( in_array( 'term_id', $current_constraints ) ) {
     226            $query   = $wpdb->prepare(
     227                "ALTER TABLE $wpdb->term_relationships ADD CONSTRAINT `term_taxonomy_id` FOREIGN KEY (`term_taxonomy_id`) REFERENCES $wpdb->term_taxonomy (`term_taxonomy_id`) ON DELETE CASCADE ON UPDATE CASCADE"
     228            );
     229            $results = $wpdb->query( $query ) ?? false;
     230
     231            return $results;
     232        }
     233
     234        /**
     235         * Add foreign key to term_taxonomy table.
     236         *
     237         * @return int|string Results of query or message.
     238         */
     239        public function foregin_key_term_taxonomy() {
     240
     241            global $wpdb;
     242
     243            $current_constraints = $this->get_current_constraints();
     244
     245            if ( in_array( 'term_id', $current_constraints, true ) ) {
    242246                return __( 'Term ID constraint already exists.', 'foreign-keys-pro' );
    243247            }
    244248
    245             $query   = "ALTER TABLE $wpdb->term_taxonomy ADD CONSTRAINT `term_id` FOREIGN KEY (`term_id`) REFERENCES $wpdb->terms (`term_id`) ON DELETE CASCADE ON UPDATE CASCADE";
    246             $results = $wpdb->query( $query ) ?? false;
    247 
    248             return $results;
    249 
    250         }
    251 
    252         /**
    253          * [foregin_key_posts description]
    254          *
    255          * @param  array $args Arguments.
    256          * @return $results Results of Query.
    257          */
    258         public function foregin_key_posts( $args = array() ) {
    259 
    260             global $wpdb;
    261 
    262             $current_constraints = $this->get_current_constraints();
    263 
    264             if( in_array( 'author_id', $current_constraints ) ) {
     249            $query   = $wpdb->prepare(
     250                "ALTER TABLE $wpdb->term_taxonomy ADD CONSTRAINT `term_id` FOREIGN KEY (`term_id`) REFERENCES $wpdb->terms (`term_id`) ON DELETE CASCADE ON UPDATE CASCADE"
     251            );
     252            $results = $wpdb->query( $query ) ?? false;
     253
     254            return $results;
     255        }
     256
     257        /**
     258         * Add foreign key to posts table for author.
     259         *
     260         * @return int|string Results of query or message.
     261         */
     262        public function foregin_key_posts() {
     263
     264            global $wpdb;
     265
     266            $current_constraints = $this->get_current_constraints();
     267
     268            if ( in_array( 'author_id', $current_constraints, true ) ) {
    265269                return __( 'Author ID constraint already exists.', 'foreign-keys-pro' );
    266270            }
    267271
    268 
    269             $query   = "ALTER TABLE $wpdb->posts ADD CONSTRAINT `author_id` FOREIGN KEY (`post_author`) REFERENCES $wpdb->users (`ID`) ON DELETE CASCADE ON UPDATE CASCADE";
    270             $results = $wpdb->query( $query ) ?? false;
    271 
    272             return $results;
    273 
    274         }
    275 
    276         /**
    277          * [foreign_keys_comments description]
    278          *
    279          * @param  array $args Arguments.
    280          * @return $results Results of Query.
    281          */
    282         public function foreign_keys_comments( $args = array() ) {
    283 
    284                 global $wpdb;
    285 
    286                 $current_constraints = $this->get_current_constraints();
    287 
    288                 if( in_array( 'comment_post_id', $current_constraints ) ) {
    289                     return __( 'Comment Post ID constraint already exists.', 'foreign-keys-pro' );
    290                 }
    291 
    292                 $query   = "ALTER TABLE $wpdb->comments ADD CONSTRAINT `comment_post_id` FOREIGN KEY (`comment_post_ID`) REFERENCES $wpdb->posts (`ID`) ON DELETE CASCADE ON UPDATE CASCADE";
    293                 $results = $wpdb->query( $query ) ?? false;
    294 
    295                 return $results;
    296 
    297         }
    298 
    299         /**
    300          * [foreign_keys_comments_user description]
    301          * @param  array  $args [description]
    302          * @return [type]       [description]
    303          */
    304         public function foreign_keys_comments_user( $args = array() ) {
    305 
    306                 global $wpdb;
    307 
    308                 $current_constraints = $this->get_current_constraints();
    309 
    310                 if( in_array( 'comments_user_id', $current_constraints ) ) {
    311                     return __( 'Comments User ID constraint already exists.', 'foreign-keys-pro' );
    312                 }
    313 
    314                 $query   = "ALTER TABLE $wpdb->comments ADD CONSTRAINT `comments_user_id` FOREIGN KEY (`user_id`) REFERENCES $wpdb->users (`ID`) ON DELETE CASCADE ON UPDATE CASCADE";
    315                 $results = $wpdb->query( $query ) ?? false;
    316 
    317                 return $results;
    318 
    319         }
    320 
    321         /**
    322          * [foreign_key_commentmeta description]
    323          *
    324          * @param  array $args Arguments.
    325          * @return $results Results of Query.
    326          */
    327         public function foreign_key_commentmeta( $args = array() ) {
    328 
    329             global $wpdb;
    330 
    331             $current_constraints = $this->get_current_constraints();
    332 
    333             if( in_array( 'comment_id', $current_constraints ) ) {
     272            $query   = $wpdb->prepare(
     273                "ALTER TABLE $wpdb->posts ADD CONSTRAINT `author_id` FOREIGN KEY (`post_author`) REFERENCES $wpdb->users (`ID`) ON DELETE CASCADE ON UPDATE CASCADE"
     274            );
     275            $results = $wpdb->query( $query ) ?? false;
     276
     277            return $results;
     278        }
     279
     280        /**
     281         * Add foreign key to comments table for post ID.
     282         *
     283         * @return int|string Results of query or message.
     284         */
     285        public function foreign_keys_comments() {
     286
     287            global $wpdb;
     288
     289            $current_constraints = $this->get_current_constraints();
     290
     291            if ( in_array( 'comment_post_id', $current_constraints, true ) ) {
     292                return __( 'Comment Post ID constraint already exists.', 'foreign-keys-pro' );
     293            }
     294
     295            $query   = $wpdb->prepare(
     296                "ALTER TABLE $wpdb->comments ADD CONSTRAINT `comment_post_id` FOREIGN KEY (`comment_post_ID`) REFERENCES $wpdb->posts (`ID`) ON DELETE CASCADE ON UPDATE CASCADE"
     297            );
     298            $results = $wpdb->query( $query ) ?? false;
     299
     300            return $results;
     301        }
     302
     303        /**
     304         * Add foreign key to comments table for user ID.
     305         *
     306         * @return int|string Results of query or message.
     307         */
     308        public function foreign_keys_comments_user() {
     309
     310            global $wpdb;
     311
     312            $current_constraints = $this->get_current_constraints();
     313
     314            if ( in_array( 'comments_user_id', $current_constraints, true ) ) {
     315                return __( 'Comments User ID constraint already exists.', 'foreign-keys-pro' );
     316            }
     317
     318            $query   = $wpdb->prepare(
     319                "ALTER TABLE $wpdb->comments ADD CONSTRAINT `comments_user_id` FOREIGN KEY (`user_id`) REFERENCES $wpdb->users (`ID`) ON DELETE CASCADE ON UPDATE CASCADE"
     320            );
     321            $results = $wpdb->query( $query ) ?? false;
     322
     323            return $results;
     324        }
     325
     326        /**
     327         * Add foreign key to commentmeta table.
     328         *
     329         * @return int|string Results of query or message.
     330         */
     331        public function foreign_key_commentmeta() {
     332
     333            global $wpdb;
     334
     335            $current_constraints = $this->get_current_constraints();
     336
     337            if ( in_array( 'comment_id', $current_constraints, true ) ) {
    334338                return __( 'Comment ID constraint already exists.', 'foreign-keys-pro' );
    335339            }
    336340
    337             $query   = "ALTER TABLE $wpdb->commentmeta ADD CONSTRAINT `comment_id` FOREIGN KEY (`comment_id`) REFERENCES $wpdb->comments (`comment_ID`) ON DELETE CASCADE ON UPDATE CASCADE";
    338             $results = $wpdb->query( $query ) ?? false;
    339 
    340             return $results;
    341 
    342         }
    343 
     341            $query   = $wpdb->prepare(
     342                "ALTER TABLE $wpdb->commentmeta ADD CONSTRAINT `comment_id` FOREIGN KEY (`comment_id`) REFERENCES $wpdb->comments (`comment_ID`) ON DELETE CASCADE ON UPDATE CASCADE"
     343            );
     344            $results = $wpdb->query( $query ) ?? false;
     345
     346            return $results;
     347        }
    344348    }
    345349
Note: See TracChangeset for help on using the changeset viewer.