| 1 |
<?php |
| 2 |
/** |
| 3 |
* Helper methods. |
| 4 |
* |
| 5 |
* @package Orderable/Classes |
| 6 |
*/ |
| 7 |
|
| 8 |
defined( 'ABSPATH' ) || exit; |
| 9 |
|
| 10 |
/** |
| 11 |
* Helpers class. |
| 12 |
*/ |
| 13 |
class Orderable_Helpers { |
| 14 |
/** |
| 15 |
* Get term ID by slug. |
| 16 |
* |
| 17 |
* @param string $slug |
| 18 |
* @param string $taxonomy |
| 19 |
* |
| 20 |
* @return int|bool |
| 21 |
*/ |
| 22 |
public static function get_term_id_by_slug( $slug, $taxonomy = 'product_cat' ) { |
| 23 |
global $wpdb; |
| 24 |
|
| 25 |
$results = $wpdb->get_var( $wpdb->prepare( |
| 26 |
"SELECT DISTINCT t.term_id |
| 27 |
FROM wp_term_taxonomy AS tt |
| 28 |
INNER JOIN wp_terms AS t ON tt.term_id = t.term_id |
| 29 |
WHERE t.slug = %s |
| 30 |
AND tt.taxonomy = %s", |
| 31 |
$slug, |
| 32 |
$taxonomy |
| 33 |
) ); |
| 34 |
|
| 35 |
return $results ? absint( $results ) : false; |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* Load classes. |
| 40 |
* |
| 41 |
* @param array $classes Key = filename / Value = Class name. |
| 42 |
* @param string $module Module folder name. |
| 43 |
*/ |
| 44 |
public static function load_classes( $classes, $module, $root = ORDERABLE_MODULES_PATH ) { |
| 45 |
foreach ( $classes as $file_name => $class_name ) { |
| 46 |
$path = $root . $module . '/class-' . $file_name . '.php'; |
| 47 |
|
| 48 |
if ( ! file_exists( $path ) ) { |
| 49 |
continue; |
| 50 |
} |
| 51 |
|
| 52 |
require_once $path; |
| 53 |
|
| 54 |
if ( ! method_exists( $class_name, 'run' ) ) { |
| 55 |
continue; |
| 56 |
} |
| 57 |
|
| 58 |
$class_name::run(); |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Get pro button. |
| 64 |
* |
| 65 |
* @param string $campaign |
| 66 |
* @param string $text |
| 67 |
* @param bool $lock_icon |
| 68 |
* |
| 69 |
* @return string |
| 70 |
*/ |
| 71 |
public static function get_pro_button( $campaign = '', $text = '', $lock_icon = true ) { |
| 72 |
$text = empty( $text ) ? __( 'Available in Pro', 'orderable' ) : $text; |
| 73 |
$campaign = ! empty( $campaign ) ? sprintf( '&utm_campaign=%s', $campaign ) : ''; |
| 74 |
$lock_icon = $lock_icon ? '<span class="dashicons dashicons-lock"></span>' : ''; |
| 75 |
|
| 76 |
return sprintf( '<a href="%s" class="orderable-admin-button orderable-admin-button--pro" target="_blank">%s %s</a>', esc_url( 'https://orderable.com/?utm_source=Orderable&utm_medium=Plugin' . $campaign ), $lock_icon, $text ); |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* Prepare post type labels. |
| 81 |
* |
| 82 |
* @param array $labels |
| 83 |
* |
| 84 |
* @return array|bool |
| 85 |
*/ |
| 86 |
public static function prepare_post_type_labels( $labels = array() ) { |
| 87 |
if ( empty( $labels ) ) { |
| 88 |
return false; |
| 89 |
} |
| 90 |
|
| 91 |
return array( |
| 92 |
'name' => $labels['plural'], |
| 93 |
'singular_name' => $labels['singular'], |
| 94 |
'menu_name' => $labels['plural'], |
| 95 |
'name_admin_bar' => $labels['singular'], |
| 96 |
'add_new' => __( 'Add New', 'orderable' ), |
| 97 |
'add_new_item' => sprintf( __( 'Add New %s', 'orderable' ), $labels['singular'] ), |
| 98 |
'new_item' => sprintf( __( 'New %s', 'orderable' ), $labels['singular'] ), |
| 99 |
'edit_item' => sprintf( __( 'Edit %s', 'orderable' ), $labels['singular'] ), |
| 100 |
'view_item' => sprintf( __( 'View %s', 'orderable' ), $labels['singular'] ), |
| 101 |
'all_items' => $labels['plural'], |
| 102 |
'search_items' => sprintf( __( 'Search %s', 'orderable' ), $labels['plural'] ), |
| 103 |
'parent_item_colon' => sprintf( __( 'Parent %s:', 'orderable' ), $labels['plural'] ), |
| 104 |
'not_found' => sprintf( __( 'No %s found.', 'orderable' ), strtolower( $labels['plural'] ) ), |
| 105 |
'not_found_in_trash' => sprintf( __( 'No %s found in trash.', 'orderable' ), strtolower( $labels['plural'] ) ), |
| 106 |
'featured_image' => sprintf( _x( '%s Featured Image', 'Overrides the “Featured Image” phrase for this post type. Added in 4.3', 'orderable' ), $labels['singular'] ), |
| 107 |
'set_featured_image' => _x( 'Set featured image', 'Overrides the “Set featured image” phrase for this post type. Added in 4.3', 'orderable' ), |
| 108 |
'remove_featured_image' => _x( 'Remove featured image', 'Overrides the “Remove featured image” phrase for this post type. Added in 4.3', 'orderable' ), |
| 109 |
'use_featured_image' => _x( 'Use as featured image', 'Overrides the “Use as featured image” phrase for this post type. Added in 4.3', 'orderable' ), |
| 110 |
'archives' => sprintf( _x( '%s archives', 'The post type archive label used in nav menus. Default “Post Archives”. Added in 4.4', 'orderable' ), $labels['plural'] ), |
| 111 |
'insert_into_item' => sprintf( _x( 'Insert into %s', 'Overrides the “Insert into post”/”Insert into page” phrase (used when inserting media into a post). Added in 4.4', 'orderable' ), strtolower( $labels['singular'] ) ), |
| 112 |
'uploaded_to_this_item' => sprintf( _x( 'Uploaded to this %s', 'Overrides the “Uploaded to this post”/”Uploaded to this page” phrase (used when viewing media attached to a post). Added in 4.4', 'orderable' ), strtolower( $labels['singular'] ) ), |
| 113 |
'filter_items_list' => sprintf( _x( 'Filter %s list', 'Screen reader text for the filter links heading on the post type listing screen. Default “Filter posts list”/”Filter pages list”. Added in 4.4', 'orderable' ), strtolower( $labels['plural'] ) ), |
| 114 |
'items_list_navigation' => sprintf( _x( '%s list navigation', 'Screen reader text for the pagination heading on the post type listing screen. Default “Posts list navigation”/”Pages list navigation”. Added in 4.4', 'orderable' ), $labels['plural'] ), |
| 115 |
'items_list' => sprintf( _x( '%s list', 'Screen reader text for the items list heading on the post type listing screen. Default “Posts list”/”Pages list”. Added in 4.4', 'orderable' ), $labels['plural'] ), |
| 116 |
); |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* Outputs the pro feature modal. |
| 121 |
*/ |
| 122 |
public static function orderable_pro_modal() { |
| 123 |
require_once ORDERABLE_TEMPLATES_PATH . 'admin/orderable-pro-modal.php'; |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* Custom kses method so we can also modify CSS properties allowed. |
| 128 |
* |
| 129 |
* @param string $content |
| 130 |
* @param string $context |
| 131 |
* |
| 132 |
* @return string |
| 133 |
*/ |
| 134 |
public static function kses( $content, $context = '' ) { |
| 135 |
if ( empty( $content ) ) { |
| 136 |
return $content; |
| 137 |
} |
| 138 |
|
| 139 |
// Modify allowed CSS. |
| 140 |
add_filter( 'safe_style_css', array( __CLASS__, 'safe_css' ) ); |
| 141 |
|
| 142 |
$content = wp_kses( $content, self::kses_allowed_html( $context ) ); |
| 143 |
|
| 144 |
// Disable CSS modification. |
| 145 |
remove_filter( 'safe_style_css', array( __CLASS__, 'safe_css' ) ); |
| 146 |
|
| 147 |
return $content; |
| 148 |
} |
| 149 |
|
| 150 |
/** |
| 151 |
* Sanitise output and allow form elements. |
| 152 |
* |
| 153 |
* @param string $context |
| 154 |
* |
| 155 |
* @return array |
| 156 |
*/ |
| 157 |
public static function kses_allowed_html( $context = '' ) { |
| 158 |
$allowed_html = wp_kses_allowed_html( 'post' ); |
| 159 |
|
| 160 |
if ( 'form' === $context ) { |
| 161 |
$allowed_attributes = array( |
| 162 |
'name' => array(), |
| 163 |
'class' => array(), |
| 164 |
'value' => array(), |
| 165 |
'type' => array(), |
| 166 |
'selected' => array(), |
| 167 |
); |
| 168 |
|
| 169 |
$allowed_html['span'] = $allowed_attributes; |
| 170 |
$allowed_html['select'] = $allowed_attributes; |
| 171 |
$allowed_html['option'] = $allowed_attributes; |
| 172 |
$allowed_html['input'] = $allowed_attributes; |
| 173 |
} |
| 174 |
|
| 175 |
return $allowed_html; |
| 176 |
} |
| 177 |
|
| 178 |
/** |
| 179 |
* Modify safe CSS values. |
| 180 |
* |
| 181 |
* @param array $safe_css |
| 182 |
* |
| 183 |
* @return array |
| 184 |
*/ |
| 185 |
public static function safe_css( $safe_css = array() ) { |
| 186 |
$safe_css[] = 'display'; |
| 187 |
|
| 188 |
return $safe_css; |
| 189 |
} |
| 190 |
|
| 191 |
/** |
| 192 |
* Delete Orderable transients. |
| 193 |
*/ |
| 194 |
public static function delete_orderable_transients() { |
| 195 |
global $wpdb; |
| 196 |
|
| 197 |
$wpdb->query(" |
| 198 |
DELETE FROM $wpdb->options |
| 199 |
WHERE option_name LIKE ('%%\_transient\_timeout\_orderable\_%%') |
| 200 |
OR option_name LIKE ('%%\_transient\_orderable\_%%') |
| 201 |
"); |
| 202 |
} |
| 203 |
|
| 204 |
/** |
| 205 |
* Has notices? |
| 206 |
* |
| 207 |
* @return bool |
| 208 |
*/ |
| 209 |
public static function has_notices() { |
| 210 |
return ! empty( WC()->session->get( 'wc_notices', array() ) ); |
| 211 |
} |
| 212 |
} |
| 213 |
|