setup( $view, $data ); return $the_view; } /** * Create a new instance of the $controller, which is stored in the "controllers" subfolder. * * @since 1.0.0 * * @param string $controller Name of the controller. * @return object Instance of the initialized controller. */ public static function load_controller( $controller ) { // Controller Base Class. self::load_file( 'class-controller.php', 'classes' ); // Make first letter uppercase for a better looking naming pattern. $uccontroller = ucfirst( $controller ); $the_controller = self::load_class( "TablePress_{$uccontroller}_Controller", "controller-{$controller}.php", 'controllers' ); return $the_controller; } /** * Generate the complete nonce string, from the nonce base, the action and an item, e.g. tablepress_delete_table_3. * * @since 1.0.0 * * @param string $action Action for which the nonce is needed. * @param string|bool $item Optional. Item for which the action will be performed, like "table". * @return string The resulting nonce string. */ public static function nonce( $action, $item = false ) { $nonce = "tablepress_{$action}"; if ( $item ) { $nonce .= "_{$item}"; } return $nonce; } /** * Check whether a nonce string is valid. * * @since 1.0.0 * * @param string $action Action for which the nonce should be checked. * @param string|bool $item Optional. Item for which the action should be performed, like "table". * @param string $query_arg Optional. Name of the nonce query string argument in $_POST. * @param bool $ajax Whether the nonce comes from an AJAX request. */ public static function check_nonce( $action, $item = false, $query_arg = '_wpnonce', $ajax = false ) { $nonce_action = self::nonce( $action, $item ); if ( $ajax ) { check_ajax_referer( $nonce_action, $query_arg ); } else { check_admin_referer( $nonce_action, $query_arg ); } } /** * Calculate the column index (number) of a column header string (example: A is 1, AA is 27, ...). * * For the opposite, @see number_to_letter(). * * @since 1.0.0 * * @param string $column Column string. * @return int $number Column number, 1-based. */ public static function letter_to_number( $column ) { $column = strtoupper( $column ); $count = strlen( $column ); $number = 0; for ( $i = 0; $i < $count; $i++ ) { $number += ( ord( $column[ $count - 1 - $i ] ) - 64 ) * pow( 26, $i ); } return $number; } /** * "Calculate" the column header string of a column index (example: 2 is B, AB is 28, ...). * * For the opposite, @see letter_to_number(). * * @since 1.0.0 * * @param int $number Column number, 1-based. * @return string $column Column string. */ public static function number_to_letter( $number ) { $column = ''; while ( $number > 0 ) { $column = chr( 65 + ( ( $number - 1 ) % 26 ) ) . $column; $number = floor( ( $number - 1 ) / 26 ); } return $column; } /** * Get a nice looking date and time string from the mySQL format of datetime strings for output. * * @since 1.0.0 * * @param string $datetime_string DateTime string, often in mySQL format.. * @param string $separator_or_format Optional. Separator between date and time, or format string. * @return string Nice looking string with the date and time. */ public static function format_datetime( $datetime_string, $separator_or_format = ' ' ) { $timezone = wp_timezone(); $datetime = date_create( $datetime_string, $timezone ); $timestamp = $datetime->getTimestamp(); switch ( $separator_or_format ) { case ' ': case '
': $date = wp_date( get_option( 'date_format' ), $timestamp, $timezone ); $time = wp_date( get_option( 'time_format' ), $timestamp, $timezone ); $output = "{$date}{$separator_or_format}{$time}"; break; default: $output = wp_date( $separator_or_format, $timestamp, $timezone ); break; } return $output; } /** * Get the name from a WP user ID (used to store information on last editor of a table). * * @since 1.0.0 * * @param int $user_id WP user ID. * @return string Nickname of the WP user with the $user_id. */ public static function get_user_display_name( $user_id ) { $user = get_userdata( $user_id ); return ( $user && isset( $user->display_name ) ) ? $user->display_name : sprintf( '%s', __( 'unknown', 'tablepress' ) ); } /** * Sanitizes a CSS class to ensure it only contains valid characters. * * Strips the string down to A-Z, a-z, 0-9, :, _, -. * This is an extension to WP's `sanitize_html_class()`, to also allow `:` which are used in some CSS frameworks. * * @since 1.11.0 * * @param string $class The CSS class name to be sanitized. * @return string The sanitized CSS class. */ public static function sanitize_css_class( $class ) { // Strip out any %-encoded octets. $sanitized_class = preg_replace( '|%[a-fA-F0-9][a-fA-F0-9]|', '', $class ); // Limit to A-Z, a-z, 0-9, ':', '_', and '-'. $sanitized_class = preg_replace( '/[^A-Za-z0-9:_-]/', '', $sanitized_class ); return $sanitized_class; } /** * Generate the action URL, to be used as a link within the plugin (e.g. in the submenu navigation or List of Tables). * * @since 1.0.0 * * @param array $params Optional. Parameters to form the query string of the URL. * @param bool $add_nonce Optional. Whether the URL shall be nonced by WordPress. * @param string $target Optional. Target File, e.g. "admin-post.php" for POST requests. * @return string The URL for the given parameters (already run through esc_url() with $add_nonce === true!). */ public static function url( array $params = array(), $add_nonce = false, $target = '' ) { // Default action is "list", if no action given. if ( ! isset( $params['action'] ) ) { $params['action'] = 'list'; } $nonce_action = $params['action']; if ( $target ) { $params['action'] = "tablepress_{$params['action']}"; } else { $params['page'] = 'tablepress'; // Top-level parent page needs special treatment for better action strings. if ( self::$controller->is_top_level_page ) { $target = 'admin.php'; if ( ! in_array( $params['action'], array( 'list', 'edit' ), true ) ) { $params['page'] = "tablepress_{$params['action']}"; } if ( ! in_array( $params['action'], array( 'edit' ), true ) ) { $params['action'] = false; } } else { $target = self::$controller->parent_page; } } // $default_params also determines the order of the values in the query string. $default_params = array( 'page' => false, 'action' => false, 'item' => false, ); $params = array_merge( $default_params, $params ); $url = add_query_arg( $params, admin_url( $target ) ); if ( $add_nonce ) { $url = wp_nonce_url( $url, self::nonce( $nonce_action, $params['item'] ) ); // wp_nonce_url() does esc_html() } return $url; } /** * Create a redirect URL from the $target_parameters and redirect the user. * * @since 1.0.0 * * @param array $params Optional. Parameters from which the target URL is constructed. * @param bool $add_nonce Optional. Whether the URL shall be nonced by WordPress. */ public static function redirect( array $params = array(), $add_nonce = false ) { $redirect = self::url( $params ); if ( $add_nonce ) { if ( ! isset( $params['item'] ) ) { $params['item'] = false; } // Don't use wp_nonce_url(), as that uses esc_html(). $redirect = add_query_arg( '_wpnonce', wp_create_nonce( self::nonce( $params['action'], $params['item'] ) ), $redirect ); } wp_redirect( $redirect ); exit; } /** * Show an error notice to admins, if TablePress's minimum requirements are not reached. * * @since 1.0.0 */ public static function show_minimum_requirements_error_notice() { // Message is not translated as it is shown on every admin screen, for which we don't want to load translations. echo '

' . 'Attention: ' . 'The installed version of WordPress is too old for the TablePress plugin! TablePress requires an up-to-date version! Please update your WordPress installation!' . "

\n"; } } // class TablePress