%s', esc_url( Connection::settings_url() ), esc_html__( 'Settings', 'ojelio' ) ) ); return $links; } // ---- What the page shows --------------------------------------------------------- /** The site's own defaults; an empty value takes the workspace's brand kit default. */ public static function defaults() { $saved = get_option( self::DEFAULTS ); return wp_parse_args( is_array( $saved ) ? $saved : array(), array( 'format' => '', 'language' => '', 'seconds' => '' ) ); } /** GET /options (what a video can ask for in the workspace), kept ten minutes. */ public static function options() { $cached = get_transient( self::OPTIONS_CACHE ); if ( is_array( $cached ) ) { return $cached; } $options = Connection::call( 'GET', '/options' ); if ( ! is_wp_error( $options ) ) { set_transient( self::OPTIONS_CACHE, $options, 10 * MINUTE_IN_SECONDS ); } return $options; } public static function format_label( $format ) { $labels = array( 'vertical' => __( 'Vertical (9:16)', 'ojelio' ), 'square' => __( 'Square (1:1)', 'ojelio' ), ); return isset( $labels[ $format ] ) ? $labels[ $format ] : $format; } public static function language_label( $language ) { $labels = array( 'en-US' => __( 'English', 'ojelio' ), 'pt-BR' => __( 'Portuguese (Brazil)', 'ojelio' ), ); return isset( $labels[ $language ] ) ? $labels[ $language ] : $language; } public static function seconds_label( $seconds ) { /* translators: %d: a number of seconds. */ return sprintf( _n( '%d second', '%d seconds', (int) $seconds, 'ojelio' ), (int) $seconds ); } public static function plan_label( $plan ) { return 'free' === $plan ? __( 'Free', 'ojelio' ) : ucfirst( $plan ); } // ---- Actions ----------------------------------------------------------------------- private static function check( $action ) { if ( ! current_user_can( self::CAPABILITY ) ) { wp_die( esc_html__( 'Only administrators can change the Ojelio settings.', 'ojelio' ), 403 ); } check_admin_referer( $action ); } /** A message for the next settings page load, for this user. */ private static function notice( $type, $message ) { set_transient( self::NOTICE . get_current_user_id(), array( 'type' => $type, 'message' => $message ), 5 * MINUTE_IN_SECONDS ); } public static function handle_connect() { self::check( 'ojelio_connect' ); // Ojelio's address is ours, not the site's: not wp_safe_redirect(). wp_redirect( Connection::begin() ); // phpcs:ignore WordPress.Security.SafeRedirect.wp_redirect_wp_redirect exit; } public static function handle_disconnect() { self::check( 'ojelio_disconnect' ); Connection::disconnect(); self::notice( 'success', __( 'This site is disconnected from Ojelio. Its videos stay in your Media Library and in the Ojelio workspace.', 'ojelio' ) ); wp_safe_redirect( Connection::settings_url() ); exit; } public static function handle_defaults() { self::check( 'ojelio_defaults' ); $options = self::options(); $lists = is_wp_error( $options ) ? array() : $options; $pick = static function ( $name, $allowed ) { $value = isset( $_POST[ $name ] ) ? sanitize_text_field( wp_unslash( $_POST[ $name ] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification -- checked in check(). return in_array( $value, array_map( 'strval', $allowed ), true ) ? $value : ''; }; update_option( self::DEFAULTS, array( 'format' => $pick( 'format', isset( $lists['formats'] ) ? $lists['formats'] : array() ), 'language' => $pick( 'language', isset( $lists['languages'] ) ? $lists['languages'] : array() ), 'seconds' => $pick( 'seconds', isset( $lists['seconds'] ) ? $lists['seconds'] : array() ), ), false ); self::notice( 'success', __( 'Defaults saved.', 'ojelio' ) ); wp_safe_redirect( Connection::settings_url() ); exit; } /** The browser back from Ojelio's approval screen, on the site's front end. */ public static function handle_return() { if ( ! isset( $_GET[ Connection::RETURN_PARAM ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification -- the state is the check. return; } if ( ! is_user_logged_in() ) { auth_redirect(); } if ( ! current_user_can( self::CAPABILITY ) ) { wp_die( esc_html__( 'Only administrators can connect this site to Ojelio.', 'ojelio' ), 403 ); } // phpcs:disable WordPress.Security.NonceVerification $query = array(); foreach ( array( 'code', 'state', 'error' ) as $name ) { if ( isset( $_GET[ $name ] ) ) { $query[ $name ] = sanitize_text_field( wp_unslash( $_GET[ $name ] ) ); } } // phpcs:enable $result = Connection::complete( $query ); if ( is_wp_error( $result ) ) { self::notice( 'access_denied' === substr( $result->get_error_code(), 7 ) ? 'warning' : 'error', $result->get_error_message() ); } else { self::notice( 'success', __( 'This site is connected to Ojelio. You can now make videos from your posts.', 'ojelio' ) ); } wp_safe_redirect( Connection::settings_url() ); exit; } // ---- The page ---------------------------------------------------------------------- public static function render() { if ( ! current_user_can( self::CAPABILITY ) ) { return; } $notice = get_transient( self::NOTICE . get_current_user_id() ); delete_transient( self::NOTICE . get_current_user_id() ); // Asked each time, so a site disconnected in Ojelio shows it at once. $status = Connection::get() ? Connection::call( 'GET', '/connection' ) : null; ?>

get_error_code() ) { // Disconnected in Ojelio since the last visit: the token is already forgotten. echo '

' . esc_html( $status->get_error_message() ) . '

'; $status = null; } if ( Connection::get() ) { self::render_connected( $status ); } else { self::render_connect(); } ?>

' . esc_html( Connection::site_url() ) . '' ); ?>

get_error_message() ); ?>

0 && $limit < 60 && ! function_exists( 'set_time_limit' ) ) { /* translators: %d: seconds. */ $problems[] = sprintf( __( 'PHP stops a task after %d seconds, which may be too short to download a long video.', 'ojelio' ), $limit ); } if ( ! $problems ) { echo esc_html__( 'Finished videos are saved to the Media Library in the background. This site looks able to save them.', 'ojelio' ); return; } echo '

' . esc_html__( 'Finished videos are saved to the Media Library in the background, but this may fail here:', 'ojelio' ) . '

' . esc_html__( 'A video that cannot be saved stays available in Ojelio, with a link to download it.', 'ojelio' ) . '

'; } private static function render_defaults() { $options = self::options(); ?>

get_error_message() ); ?>

array( __( 'Format', 'ojelio' ), $options['formats'], array( __CLASS__, 'format_label' ), $kit['format'] ), 'language' => array( __( 'Language', 'ojelio' ), $options['languages'], array( __CLASS__, 'language_label' ), $kit['language'] ), 'seconds' => array( __( 'Length', 'ojelio' ), $options['seconds'], array( __CLASS__, 'seconds_label' ), $kit['seconds'] ), ); ?>

list( $label, $values, $label_of, $kit_value ) ) : ?>