Make WordPress Core

Ticket #39167: theme_mods_multi_theme.diff

File theme_mods_multi_theme.diff, 7.5 KB (added by georgestephanis, 8 years ago)
  • src/wp-includes/class-wp-theme.php

     
    708708        /**
    709709         * Mark up a theme header.
    710710         *
    711     * @since 3.4.0
     711        * @since 3.4.0
    712712         * @access private
    713713         *
    714714         * @staticvar string $comma
     
    14851485                // Don't mark up; Do translate.
    14861486                return strnatcasecmp( $a->display( 'Name', false, true ), $b->display( 'Name', false, true ) );
    14871487        }
     1488
     1489        /**
     1490         * Retrieve all theme modifications.
     1491         *
     1492         * @since 4.?.0
     1493         *
     1494         * @return array|void Theme modifications.
     1495         */
     1496        public function get_theme_mods() {
     1497                $mods = get_option( "theme_mods_{$this->stylesheet}" );
     1498                if ( false === $mods ) {
     1499                        $theme_name = $this->get('Name');
     1500                        $mods = get_option( "mods_$theme_name" ); // Deprecated location.
     1501                        if ( is_admin() && false !== $mods ) {
     1502                                update_option( "theme_mods_$theme_slug", $mods );
     1503                                delete_option( "mods_$theme_name" );
     1504                        }
     1505                }
     1506                return $mods;
     1507        }
     1508
     1509        /**
     1510         * Retrieve theme modification value for the current theme.
     1511         *
     1512         * If the modification name does not exist, then the $default will be passed
     1513         * through {@link https://secure.php.net/sprintf sprintf()} PHP function with the first
     1514         * string the template directory URI and the second string the stylesheet
     1515         * directory URI.
     1516         *
     1517         * @since 4.?.0
     1518         *
     1519         * @param string      $name    Theme modification name.
     1520         * @param bool|string $default
     1521         * @return string
     1522         */
     1523        public function get_theme_mod( $name, $default = false ) {
     1524                $mods = $this->get_theme_mods();
     1525
     1526                if ( isset( $mods[ $name ] ) ) {
     1527                        /**
     1528                         * Filters the theme modification, or 'theme_mod', value.
     1529                         *
     1530                         * The dynamic portion of the hook name, `$name`, refers to
     1531                         * the key name of the modification array. For example,
     1532                         * 'header_textcolor', 'header_image', and so on depending
     1533                         * on the theme options.
     1534                         *
     1535                         * @since 2.2.0
     1536                         *
     1537                         * @param string $current_mod The value of the current theme modification.
     1538                         */
     1539                        return apply_filters( "theme_mod_{$name}", $mods[ $name ] );
     1540                }
     1541
     1542                if ( is_string( $default ) ) {
     1543                        $default = sprintf( $default, get_template_directory_uri(), get_stylesheet_directory_uri() );
     1544                }
     1545
     1546                /** This filter is documented in wp-includes/class-wp-theme.php */
     1547                return apply_filters( "theme_mod_{$name}", $default );
     1548        }
     1549
     1550        /**
     1551         * Update theme modification value for the current theme.
     1552         *
     1553         * @since 4.?.0
     1554         *
     1555         * @param string $name  Theme modification name.
     1556         * @param mixed  $value Theme modification value.
     1557         */
     1558        public function set_theme_mod( $name, $value ) {
     1559                $mods = $this->get_theme_mods();
     1560                $old_value = isset( $mods[ $name ] ) ? $mods[ $name ] : false;
     1561
     1562                /**
     1563                 * Filters the theme mod value on save.
     1564                 *
     1565                 * The dynamic portion of the hook name, `$name`, refers to the key name of
     1566                 * the modification array. For example, 'header_textcolor', 'header_image',
     1567                 * and so on depending on the theme options.
     1568                 *
     1569                 * @since 3.9.0
     1570                 *
     1571                 * @param string $value     The new value of the theme mod.
     1572                 * @param string $old_value The current value of the theme mod.
     1573                 */
     1574                $mods[ $name ] = apply_filters( "pre_set_theme_mod_{$name}", $value, $old_value );
     1575
     1576                update_option( "theme_mods_{$this->stylesheet}", $mods );
     1577        }
     1578
     1579        /**
     1580         * Remove theme modification name from current theme list.
     1581         *
     1582         * If removing the name also removes all elements, then the entire option will
     1583         * be removed.
     1584         *
     1585         * @since 4.?.0
     1586         *
     1587         * @param string $name Theme modification name.
     1588         */
     1589        public function remove_theme_mod( $name ) {
     1590                $mods = $this->get_theme_mods();
     1591
     1592                if ( ! isset( $mods[ $name ] ) ) {
     1593                        return;
     1594                }
     1595
     1596                unset( $mods[ $name ] );
     1597
     1598                if ( empty( $mods ) ) {
     1599                        $this->remove_theme_mods();
     1600                        return;
     1601                }
     1602
     1603                update_option( "theme_mods_{$this->stylesheet}", $mods );
     1604        }
     1605
     1606        /**
     1607         * Remove theme modifications option for current theme.
     1608         *
     1609         * @since 4.?.0
     1610         */
     1611        public function remove_theme_mods() {
     1612                delete_option( "theme_mods_{$this->stylesheet}" );
     1613
     1614                // Old style.
     1615                delete_option( 'mods_' . $this->get('Name') );
     1616        }
    14881617}
  • src/wp-includes/theme.php

     
    823823 * @return array|void Theme modifications.
    824824 */
    825825function get_theme_mods() {
    826         $theme_slug = get_option( 'stylesheet' );
    827         $mods = get_option( "theme_mods_$theme_slug" );
    828         if ( false === $mods ) {
    829                 $theme_name = get_option( 'current_theme' );
    830                 if ( false === $theme_name )
    831                         $theme_name = wp_get_theme()->get('Name');
    832                 $mods = get_option( "mods_$theme_name" ); // Deprecated location.
    833                 if ( is_admin() && false !== $mods ) {
    834                         update_option( "theme_mods_$theme_slug", $mods );
    835                         delete_option( "mods_$theme_name" );
    836                 }
    837         }
    838         return $mods;
     826        return wp_get_theme()->get_theme_mods();
    839827}
    840828
    841829/**
     
    853841 * @return string
    854842 */
    855843function get_theme_mod( $name, $default = false ) {
    856         $mods = get_theme_mods();
    857 
    858         if ( isset( $mods[$name] ) ) {
    859                 /**
    860                  * Filters the theme modification, or 'theme_mod', value.
    861                  *
    862                  * The dynamic portion of the hook name, `$name`, refers to
    863                  * the key name of the modification array. For example,
    864                  * 'header_textcolor', 'header_image', and so on depending
    865                  * on the theme options.
    866                  *
    867                  * @since 2.2.0
    868                  *
    869                  * @param string $current_mod The value of the current theme modification.
    870                  */
    871                 return apply_filters( "theme_mod_{$name}", $mods[$name] );
    872         }
    873 
    874         if ( is_string( $default ) )
    875                 $default = sprintf( $default, get_template_directory_uri(), get_stylesheet_directory_uri() );
    876 
    877         /** This filter is documented in wp-includes/theme.php */
    878         return apply_filters( "theme_mod_{$name}", $default );
     844        return wp_get_theme()->get_theme_mod( $name, $default );
    879845}
    880846
    881847/**
     
    887853 * @param mixed  $value Theme modification value.
    888854 */
    889855function set_theme_mod( $name, $value ) {
    890         $mods = get_theme_mods();
    891         $old_value = isset( $mods[ $name ] ) ? $mods[ $name ] : false;
    892 
    893         /**
    894          * Filters the theme mod value on save.
    895          *
    896          * The dynamic portion of the hook name, `$name`, refers to the key name of
    897          * the modification array. For example, 'header_textcolor', 'header_image',
    898          * and so on depending on the theme options.
    899          *
    900          * @since 3.9.0
    901          *
    902          * @param string $value     The new value of the theme mod.
    903          * @param string $old_value The current value of the theme mod.
    904          */
    905         $mods[ $name ] = apply_filters( "pre_set_theme_mod_{$name}", $value, $old_value );
    906 
    907         $theme = get_option( 'stylesheet' );
    908         update_option( "theme_mods_$theme", $mods );
     856        wp_get_theme()->set_theme_mod( $name, $value );
    909857}
    910858
    911859/**
     
    919867 * @param string $name Theme modification name.
    920868 */
    921869function remove_theme_mod( $name ) {
    922         $mods = get_theme_mods();
    923 
    924         if ( ! isset( $mods[ $name ] ) )
    925                 return;
    926 
    927         unset( $mods[ $name ] );
    928 
    929         if ( empty( $mods ) ) {
    930                 remove_theme_mods();
    931                 return;
    932         }
    933         $theme = get_option( 'stylesheet' );
    934         update_option( "theme_mods_$theme", $mods );
     870        wp_get_theme()->remove_theme_mod( $name );
    935871}
    936872
    937873/**
     
    940876 * @since 2.1.0
    941877 */
    942878function remove_theme_mods() {
    943         delete_option( 'theme_mods_' . get_option( 'stylesheet' ) );
    944 
    945         // Old style.
    946         $theme_name = get_option( 'current_theme' );
    947         if ( false === $theme_name )
    948                 $theme_name = wp_get_theme()->get('Name');
    949         delete_option( 'mods_' . $theme_name );
     879        wp_get_theme()->remove_theme_mods();
    950880}
    951881
    952882/**