| 1 |
<?php |
| 2 |
/** |
| 3 |
* Abstract Migration for Easy Invoice |
| 4 |
* |
| 5 |
* @package EasyInvoice |
| 6 |
* @subpackage Migration |
| 7 |
* @since 2.0.0 |
| 8 |
*/ |
| 9 |
|
| 10 |
namespace EasyInvoice\Migration\Src; |
| 11 |
|
| 12 |
/** |
| 13 |
* Abstract migration class. |
| 14 |
* |
| 15 |
* Provides common functionality for all migration classes. |
| 16 |
* |
| 17 |
* @since 2.0.0 |
| 18 |
*/ |
| 19 |
abstract class AbstractMigration implements MigrationInterface { |
| 20 |
|
| 21 |
/** |
| 22 |
* Migration version. |
| 23 |
* |
| 24 |
* @since 2.0.0 |
| 25 |
* @var string |
| 26 |
*/ |
| 27 |
protected $version = '2.0.0'; |
| 28 |
|
| 29 |
/** |
| 30 |
* Migration description. |
| 31 |
* |
| 32 |
* @since 2.0.0 |
| 33 |
* @var string |
| 34 |
*/ |
| 35 |
protected $description = ''; |
| 36 |
|
| 37 |
/** |
| 38 |
* Migration log. |
| 39 |
* |
| 40 |
* @since 2.0.0 |
| 41 |
* @var array |
| 42 |
*/ |
| 43 |
protected $log = []; |
| 44 |
|
| 45 |
/** |
| 46 |
* Get migration version. |
| 47 |
* |
| 48 |
* @since 2.0.0 |
| 49 |
* @return string |
| 50 |
*/ |
| 51 |
public function get_version(): string { |
| 52 |
return $this->version; |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Get migration description. |
| 57 |
* |
| 58 |
* @since 2.0.0 |
| 59 |
* @return string |
| 60 |
*/ |
| 61 |
public function get_description(): string { |
| 62 |
return $this->description; |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Log a message. |
| 67 |
* |
| 68 |
* @since 2.0.0 |
| 69 |
* @param string $message Message to log. |
| 70 |
* @param string $level Log level (info, warning, error). |
| 71 |
* @return void |
| 72 |
*/ |
| 73 |
protected function log( string $message, string $level = 'info' ): void { |
| 74 |
$this->log[] = [ |
| 75 |
'message' => $message, |
| 76 |
'level' => $level, |
| 77 |
'timestamp' => current_time( 'mysql' ), |
| 78 |
]; |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Get migration log. |
| 83 |
* |
| 84 |
* @since 2.0.0 |
| 85 |
* @return array |
| 86 |
*/ |
| 87 |
public function get_log(): array { |
| 88 |
return $this->log; |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* Create success result. |
| 93 |
* |
| 94 |
* @since 2.0.0 |
| 95 |
* @param string $message Success message. |
| 96 |
* @return array |
| 97 |
*/ |
| 98 |
protected function success_result( string $message ): array { |
| 99 |
return [ |
| 100 |
'success' => true, |
| 101 |
'message' => $message, |
| 102 |
'log' => $this->log, |
| 103 |
]; |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Create error result. |
| 108 |
* |
| 109 |
* @since 2.0.0 |
| 110 |
* @param string $message Error message. |
| 111 |
* @return array |
| 112 |
*/ |
| 113 |
protected function error_result( string $message ): array { |
| 114 |
return [ |
| 115 |
'success' => false, |
| 116 |
'message' => $message, |
| 117 |
'log' => $this->log, |
| 118 |
]; |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* Check if option exists. |
| 123 |
* |
| 124 |
* @since 2.0.0 |
| 125 |
* @param string $option_name Option name. |
| 126 |
* @return bool |
| 127 |
*/ |
| 128 |
protected function option_exists( string $option_name ): bool { |
| 129 |
return get_option( $option_name ) !== false; |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* Check if post type exists. |
| 134 |
* |
| 135 |
* @since 2.0.0 |
| 136 |
* @param string $post_type Post type name. |
| 137 |
* @return bool |
| 138 |
*/ |
| 139 |
protected function post_type_exists( string $post_type ): bool { |
| 140 |
return post_type_exists( $post_type ); |
| 141 |
} |
| 142 |
|
| 143 |
/** |
| 144 |
* Get posts by post type. |
| 145 |
* |
| 146 |
* @since 2.0.0 |
| 147 |
* @param string $post_type Post type name. |
| 148 |
* @param int $limit Number of posts to get. |
| 149 |
* @return array |
| 150 |
*/ |
| 151 |
protected function get_posts_by_type( string $post_type, int $limit = -1 ): array { |
| 152 |
return get_posts( [ |
| 153 |
'post_type' => $post_type, |
| 154 |
'numberposts' => $limit, |
| 155 |
'post_status' => 'any', |
| 156 |
] ); |
| 157 |
} |
| 158 |
|
| 159 |
/** |
| 160 |
* Count posts by post type. |
| 161 |
* |
| 162 |
* @since 2.0.0 |
| 163 |
* @param string $post_type Post type name. |
| 164 |
* @return int |
| 165 |
*/ |
| 166 |
protected function count_posts_by_type( string $post_type ): int { |
| 167 |
return wp_count_posts( $post_type )->publish; |
| 168 |
} |
| 169 |
|
| 170 |
/** |
| 171 |
* Update post meta safely. |
| 172 |
* |
| 173 |
* @since 2.0.0 |
| 174 |
* @param int $post_id Post ID. |
| 175 |
* @param string $meta_key Meta key. |
| 176 |
* @param mixed $meta_value Meta value. |
| 177 |
* @return bool |
| 178 |
*/ |
| 179 |
protected function update_post_meta_safe( int $post_id, string $meta_key, $meta_value ): bool { |
| 180 |
$result = update_post_meta( $post_id, $meta_key, $meta_value ); |
| 181 |
|
| 182 |
if ( $result ) { |
| 183 |
$this->log( sprintf( 'Updated post meta: %s for post %d', $meta_key, $post_id ) ); |
| 184 |
} else { |
| 185 |
$this->log( sprintf( 'Failed to update post meta: %s for post %d', $meta_key, $post_id ), 'warning' ); |
| 186 |
} |
| 187 |
|
| 188 |
return $result; |
| 189 |
} |
| 190 |
|
| 191 |
/** |
| 192 |
* Update option safely. |
| 193 |
* |
| 194 |
* @since 2.0.0 |
| 195 |
* @param string $option_name Option name. |
| 196 |
* @param mixed $option_value Option value. |
| 197 |
* @return bool |
| 198 |
*/ |
| 199 |
protected function update_option_safe( string $option_name, $option_value ): bool { |
| 200 |
$result = update_option( $option_name, $option_value ); |
| 201 |
|
| 202 |
if ( $result ) { |
| 203 |
$this->log( sprintf( 'Updated option: %s', $option_name ) ); |
| 204 |
} else { |
| 205 |
$this->log( sprintf( 'Failed to update option: %s', $option_name ), 'warning' ); |
| 206 |
} |
| 207 |
|
| 208 |
return $result; |
| 209 |
} |
| 210 |
|
| 211 |
/** |
| 212 |
* Delete option safely. |
| 213 |
* |
| 214 |
* @since 2.0.0 |
| 215 |
* @param string $option_name Option name. |
| 216 |
* @return bool |
| 217 |
*/ |
| 218 |
protected function delete_option_safe( string $option_name ): bool { |
| 219 |
$result = delete_option( $option_name ); |
| 220 |
|
| 221 |
if ( $result ) { |
| 222 |
$this->log( sprintf( 'Deleted option: %s', $option_name ) ); |
| 223 |
} else { |
| 224 |
$this->log( sprintf( 'Failed to delete option: %s', $option_name ), 'warning' ); |
| 225 |
} |
| 226 |
|
| 227 |
return $result; |
| 228 |
} |
| 229 |
|
| 230 |
} |
| 231 |
|